Example usage for com.badlogic.gdx.math Vector2 Vector2

List of usage examples for com.badlogic.gdx.math Vector2 Vector2

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 Vector2.

Prototype

public Vector2(float x, float y) 

Source Link

Document

Constructs a vector with the given components

Usage

From source file:com.bladecoder.engine.actions.LookAtAction.java

License:Apache License

@Override
public boolean run(VerbRunner cb) {

    EngineLogger.debug("LOOKAT ACTION");
    InteractiveActor a = (InteractiveActor) World.getInstance().getCurrentScene().getActor(actor, true);

    if (World.getInstance().getInventory().getItem(actor) == null) {
        CharacterActor player = World.getInstance().getCurrentScene().getPlayer();

        if (direction != null)
            player.lookat(direction.getDirection());
        else if (a != null && player != null) {
            Rectangle bbox = a.getBBox().getBoundingRectangle();
            player.lookat(new Vector2(bbox.x, bbox.y));
        }//from   ww w.  j  a  va2s  .c om
    }

    if (soundId != null) {
        if (a == null) {
            EngineLogger.debug("Tried to play a sound (" + soundId + "), but there is no actor defined");
        } else {
            a.playSound(soundId);
        }
    }

    if (text != null) {
        World.getInstance().getTextManager().addText(text, TextManager.POS_SUBTITLE, TextManager.POS_SUBTITLE,
                false, Text.Type.SUBTITLE, null, null, wait ? cb : null);

        return wait;
    }

    return false;
}

From source file:com.bladecoder.engine.actions.Param.java

License:Apache License

public static Vector2 parseVector2(String s) {

    if (s == null || s.isEmpty())
        return null;

    Vector2 v = null;//  w  w w.  java2 s  .  com

    int idx = s.indexOf(NUMBER_PARAM_SEPARATOR.charAt(0));

    if (idx != -1) {
        try {
            float x = Float.parseFloat(s.substring(0, idx));
            float y = Float.parseFloat(s.substring(idx + 1));

            v = new Vector2(x, y);
        } catch (Exception e) {

        }
    }

    return v;
}

From source file:com.bladecoder.engine.actions.PositionAnimAction.java

License:Apache License

@Override
public boolean run(VerbRunner cb) {

    float scale = EngineAssetManager.getInstance().getScale();

    BaseActor a = World.getInstance().getCurrentScene().getActor(actor, false);

    float x, y;/*  w w  w . j av a 2 s  . c o m*/

    if (target == null) {
        x = pos.x * scale;
        y = pos.y * scale;
    } else {
        BaseActor target = World.getInstance().getCurrentScene().getActor(this.target, false);
        x = target.getX();
        y = target.getY();
    }

    if (speed == 0 || !(a instanceof SpriteActor)) {
        a.setPosition(x, y);

        return false;
    } else {
        // WARNING: only spriteactors support animation
        float s;

        if (mode != null && mode == Mode.SPEED) {
            Vector2 p0 = new Vector2(a.getX(), a.getY());

            s = p0.dst(x, y) / (scale * speed);
        } else {
            s = speed;
        }

        ((SpriteActor) a).startPosAnimation(repeat, count, s, x, y, interpolation, wait ? cb : null);
    }

    return wait;
}

From source file:com.bladecoder.engine.model.BaseActor.java

License:Apache License

@Override
public void write(Json json) {
    if (SerializationHelper.getInstance().getMode() == Mode.MODEL) {
        json.writeValue("id", id);
        json.writeValue("bbox", bbox.getVertices());
    } else {//from  w w  w.  j a va2 s.  co  m

    }

    float worldScale = EngineAssetManager.getInstance().getScale();
    Vector2 scaledPos = new Vector2(bbox.getX() / worldScale, bbox.getY() / worldScale);
    json.writeValue("pos", scaledPos);

    json.writeValue("visible", visible);
}

From source file:com.bladecoder.engine.model.CharacterActor.java

License:Apache License

public void lookat(Vector2 p) {
    inAnim();/*from w ww  . j  a v a  2  s.co  m*/
    posTween = null;
    renderer.startAnimation(standAnim, Tween.Type.SPRITE_DEFINED, -1, null,
            new Vector2(bbox.getX(), bbox.getY()), p);
    outAnim();
}

From source file:com.bladecoder.engine.model.CharacterActor.java

License:Apache License

/**
 * Walking Support//w  w w.  j  av a 2 s .c om
 * 
 * @param pf
 *            Final position to walk
 * @param cb
 *            The action callback
 */
public void goTo(Vector2 pf, ActionCallback cb) {
    EngineLogger.debug(MessageFormat.format("GOTO {0},{1}", pf.x, pf.y));

    Vector2 p0 = new Vector2(bbox.getX(), bbox.getY());

    ArrayList<Vector2> walkingPath = null;

    //
    if (p0.dst(pf) < 2.0f) {
        setPosition(pf.x, pf.y);

        // call the callback
        if (cb != null)
            ActionCallbackQueue.add(cb);

        return;
    }

    if (scene.getPolygonalNavGraph() != null) {
        walkingPath = scene.getPolygonalNavGraph().findPath(p0.x, p0.y, pf.x, pf.y);
    }

    if (walkingPath == null || walkingPath.size() == 0) {
        // call the callback even when the path is empty
        if (cb != null)
            ActionCallbackQueue.add(cb);

        return;
    }

    posTween = new WalkTween();

    ((WalkTween) posTween).start(this, walkingPath, walkingSpeed, cb);
}

From source file:com.bladecoder.engine.model.Sprite3DRenderer.java

License:Apache License

public Vector2 getSpriteSize() {
    return new Vector2(width, height);
}

From source file:com.bladecoder.engine.polygonalpathfinder.NavPathPolygonal.java

License:Apache License

@Override
public void fill(NavNodePolygonal startNode, NavNodePolygonal targetNode) {

    // TODO Ineficcient add in index 0 and new

    resultPath.clear();//ww w  .j ava  2s  .  c om

    NavNodePolygonal current = targetNode;
    while (current != startNode) {
        resultPath.add(0, new Vector2(current.getX(), current.getY()));
        current = (NavNodePolygonal) current.parent;
    }

    resultPath.add(0, new Vector2(current.getX(), current.getY()));
}

From source file:com.bladecoder.engine.polygonalpathfinder.PolygonalNavGraph.java

License:Apache License

public ArrayList<Vector2> findPath(float sx, float sy, float tx, float ty) {
    resultPath.clear();/*from   w  w w.  j a  va2  s  .c  o m*/

    Vector2 source = new Vector2(sx, sy);
    Vector2 target = new Vector2(tx, ty);

    // 1. First verify if both the start and target points of the path are
    // inside the polygon. If the end point is outside the polygon clamp it
    // back inside.
    if (!PolygonUtils.isPointInside(walkZone, sx, sy, true)) {
        EngineLogger.debug("PolygonalPathFinder: Source not in polygon!");
        return resultPath.getPath();
    }

    if (!PolygonUtils.isPointInside(walkZone, tx, ty, true)) {
        PolygonUtils.getClampedPoint(walkZone, tx, ty, target);

        //         if (!PolygonUtils.isPointInside(walkZone, target.x, target.y, true)) {
        //            EngineLogger.debug("PolygonalPathFinder: CLAMPED FAILED!!");
        //            return resultPath.getPath();
        //         }
    }

    for (Polygon o : obstacles) {
        if (PolygonUtils.isPointInside(o, target.x, target.y, false)) {
            PolygonUtils.getClampedPoint(o, target.x, target.y, target);

            // If the clamped point is not in the walkzone 
            // we search for the first vertex inside
            if (!PolygonUtils.isPointInside(walkZone, target.x, target.y, true)) {
                getFirstVertexInsideWalkzone(o, target);
                // We exit after processing the first polygon with the point inside. 
                // Overlaped obstacles are not supported
                break;
            }
        }
    }

    // 2. Then start by checking if both points are in line-of-sight. If
    // they are, theres no need for pathfinding, just walk there!
    if (inLineOfSight(source.x, source.y, target.x, target.y)) {
        EngineLogger.debug("PolygonalPathFinder: Direct path found");

        resultPath.getPath().add(source);
        resultPath.getPath().add(target);

        return resultPath.getPath();
    }

    // 3. Otherwise, add the start and end points of your path as new
    // temporary nodes to the graph.
    // AND Connect them to every other node that they can see on the graph.
    addStartEndNodes(source.x, source.y, target.x, target.y);

    // 5. Run your A* implementation on the graph to get your path. This
    // path is guaranteed to be as direct as possible!
    pathfinder.findPath(null, startNode, targetNode, resultPath);

    return resultPath.getPath();
}

From source file:com.bladecoder.engine.ui.defaults.DefaultSceneScreen.java

License:Apache License

private void sceneClick(int button) {
    World w = World.getInstance();/*from ww  w .ja va2 s.co m*/

    w.getSceneCamera().getInputUnProject(viewport, unprojectTmp);

    Scene s = w.getCurrentScene();

    if (currentActor != null) {

        if (EngineLogger.debugMode()) {
            EngineLogger.debug(currentActor.toString());
        }

        actorClick(currentActor, button);
    } else if (s.getPlayer() != null) {
        if (s.getPlayer().getVerb("goto") != null) {
            runVerb(s.getPlayer(), "goto", null);
        } else {
            Vector2 pos = new Vector2(unprojectTmp.x, unprojectTmp.y);

            if (recorder.isRecording()) {
                recorder.add(pos);
            }

            s.getPlayer().goTo(pos, null);
        }
    }
}