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

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

Introduction

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

Prototype

public float dst(float x, float y) 

Source Link

Usage

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;/*from  w w w .  j a  v a  2  s  .  com*/

    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.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

    super.touchDown(event, x, y, pointer, button);
    //      EditorLogger.debug("Touch Down - X: " + x + " Y: " + y);

    Scene scn = scnWidget.getScene();/* w  ww.ja v  a2s . com*/
    if (scn == null)
        return false;

    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    org.set(p);

    if (button == Buttons.LEFT) {
        selActor = scnWidget.getSelectedActor();

        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) { // Check
            // WALKZONE

            // CHECK WALKZONE VERTEXS
            Polygon wzPoly = scn.getPolygonalNavGraph().getWalkZone();
            float verts[] = wzPoly.getTransformedVertices();

            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_WALKZONE_POINT;
                    vertIndex = i;
                    return true;
                }
            }

            // CHECK FOR WALKZONE DRAGGING
            if (wzPoly.contains(p.x, p.y)) {
                draggingMode = DraggingModes.DRAGGING_WALKZONE;
                undoOrg.set(wzPoly.getX(), wzPoly.getY());
                return true;
            }

        }

        // SELACTOR VERTEXs DRAGGING
        if (selActor != null
                && (!(selActor instanceof SpriteActor) || !((SpriteActor) selActor).isBboxFromRenderer())
                && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {

            Polygon bbox = selActor.getBBox();
            float verts[] = bbox.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_BBOX_POINT;
                    vertIndex = i;
                    return true;
                }
            }
        }

        BaseActor a = scn.getActorAt(p.x, p.y); // CHECK FOR ACTORS

        if (a != null && a != selActor) {
            selActor = a;
            BaseActor da = Ctx.project.getActor(selActor.getId());
            Ctx.project.setSelectedActor(da);
        }

        if (a != null) {
            draggingMode = DraggingModes.DRAGGING_ACTOR;
            undoOrg.set(selActor.getX(), selActor.getY());
            return true;
        }

        // CHECK FOR DRAGGING DEPTH MARKERS
        Vector2 depthVector = scnWidget.getScene().getDepthVector();
        if (depthVector != null) {
            p.set(0, depthVector.x);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_0;
                return true;
            }

            p.set(0, depthVector.y);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_100;
                return true;
            }
        }

    }

    return true;
}

From source file:com.vlaaad.dice.game.actions.CreatureAction.java

License:Open Source License

protected final Array<Creature> creatures(World world, int x, int y, Function<Creature, Boolean> filter,
        float radius) {
    Vector2 creaturePos = tmpVector.set(x, y);
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : world) {
        if (!(object instanceof Creature))
            continue;
        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !filter.apply(check))
            continue;
        if (creaturePos.dst(check.getX(), check.getY()) > radius)
            continue;
        result.add(check);/*w  w w  . j  a va  2  s  . com*/
    }
    return result;
}

From source file:com.vlaaad.dice.game.actions.imp.ApplyToTarget.java

License:Open Source License

public static Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, float radius) {
    Vector2 creaturePos = tmp1.set(creature.getX(), creature.getY());
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : creature.world) {
        if (!(object instanceof Creature))
            continue;
        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !creature.inRelation(relation, check) || check == creature)
            continue;
        if (creaturePos.dst(check.getX(), check.getY()) > radius)
            continue;
        result.add(check);//from   ww  w . ja  v a2 s . co m
    }
    return result;
}

From source file:com.vlaaad.dice.game.actions.imp.Firestorm.java

License:Open Source License

@Override
public IFuture<IActionResult> apply(final Creature creature, World world) {
    int level = creature.getCurrentLevel();
    Vector2 position = tmp.set(creature.getX(), creature.getY());
    Array<Grid2D.Coordinate> available = new Array<Grid2D.Coordinate>();
    for (int i = creature.getX() - level; i <= creature.getX() + level; i++) {
        for (int j = creature.getY() - level; j <= creature.getY() + level; j++) {
            if (position.dst(i, j) <= level && world.level.exists(LevelElementType.tile, i, j)) {
                available.add(new Grid2D.Coordinate(i, j));
            }//from   ww  w  .j a va2s  . c om
        }
    }
    final Future<IActionResult> future = new Future<IActionResult>();
    world.getController(BehaviourController.class).get(creature)
            .request(BehaviourRequest.COORDINATE, new AbilityCoordinatesParams(creature, owner, available))
            .addListener(new IFutureListener<Grid2D.Coordinate>() {
                @Override
                public void onHappened(Grid2D.Coordinate coordinate) {
                    future.happen(calcResult(creature, coordinate));
                }
            });
    return future;
}

From source file:com.vlaaad.dice.game.actions.imp.Firestorm.java

License:Open Source License

private IActionResult calcResult(Creature creature, Grid2D.Coordinate cell) {
    Vector2 position = tmp.set(cell.x(), cell.y());
    Array<Creature> underAttack = new Array<Creature>();
    Array<Creature> killed = new Array<Creature>();
    ObjectIntMap<Creature> expResults = new ObjectIntMap<Creature>();
    for (int i = cell.x() - MathUtils.ceil(radius); i <= cell.x() + radius; i++) {
        for (int j = cell.y() - MathUtils.ceil(radius); j <= cell.y() + radius; j++) {
            if (position.dst(i, j) <= radius) {
                WorldObject object = creature.world.get(i, j);
                if (object instanceof Creature && ((Creature) object).get(Attribute.canBeSelected)) {
                    underAttack.add((Creature) object);
                }// w ww  . j av a2 s  . c  o  m
            }
        }
    }
    for (Creature c : underAttack) {
        int attackLevel = (c.getX() == cell.x() && c.getY() == cell.y()) ? this.epicenterAttackLevel
                : this.attackLevel;
        int defenceLevel = c.get(Attribute.defenceFor(attackType));
        if (attackLevel > defenceLevel) {
            killed.add(c);
            if (creature.inRelation(Creature.CreatureRelation.enemy, c)) {
                expResults.getAndIncrement(creature, 0, ExpHelper.expForKill(creature, c));
            }
        } else {
            if (creature.inRelation(Creature.CreatureRelation.enemy, c)) {
                expResults.put(c, ExpHelper.expForDefence(creature, c));
            } else {
                expResults.put(c, ExpHelper.MIN_EXP);
            }
        }
    }
    return new FirestormResult(creature, owner, underAttack, killed, expResults, cell);
}

From source file:com.vlaaad.dice.game.actions.imp.IceStorm.java

License:Open Source License

@Override
public IFuture<? extends IActionResult> apply(final Creature creature, World world) {
    int level = 5;
    Vector2 position = tmp.set(creature.getX(), creature.getY());
    Array<Grid2D.Coordinate> available = new Array<Grid2D.Coordinate>();
    for (int i = creature.getX() - level; i <= creature.getX() + level; i++) {
        for (int j = creature.getY() - level; j <= creature.getY() + level; j++) {
            if (position.dst(i, j) <= level && world.level.exists(LevelElementType.tile, i, j)) {
                available.add(new Grid2D.Coordinate(i, j));
            }//  www.ja v  a 2 s  .c  om
        }
    }
    return withCoordinate(creature, available,
            new Function<Grid2D.Coordinate, IFuture<? extends IActionResult>>() {
                @Override
                public IFuture<? extends IActionResult> apply(Grid2D.Coordinate coordinate) {
                    return Future.completed(calcResult(creature, coordinate));
                }
            });
}

From source file:com.vlaaad.dice.game.actions.imp.IceStorm.java

License:Open Source License

private IActionResult calcResult(Creature creature, Grid2D.Coordinate cell) {
    Vector2 position = tmp.set(cell.x(), cell.y());
    ObjectIntMap<Creature> targets = new ObjectIntMap<Creature>();
    for (int i = cell.x() - MathUtils.ceil(radius); i <= cell.x() + radius; i++) {
        for (int j = cell.y() - MathUtils.ceil(radius); j <= cell.y() + radius; j++) {
            if (position.dst(i, j) <= radius) {
                WorldObject object = creature.world.get(i, j);
                if (object instanceof Creature && ((Creature) object).get(Attribute.canBeSelected)
                        && !((Creature) object).get(Attribute.frozen)) {
                    targets.put((Creature) object, i == cell.x() && j == cell.y() ? epicenterTurns : turns);
                }/* www. ja v  a  2 s.  c  om*/
            }
        }
    }

    return new IceStormResult(owner, creature, cell, targets);
}

From source file:com.vlaaad.dice.game.actions.imp.Teleport.java

License:Open Source License

public static Array<Grid2D.Coordinate> gatherCoordinates(Creature creature, float radius) {
    Vector2 position = tmp.set(creature.getX(), creature.getY());
    Array<Grid2D.Coordinate> available = new Array<Grid2D.Coordinate>();
    for (int i = creature.getX() - MathUtils.ceil(radius); i <= creature.getX() + MathUtils.ceil(radius); i++) {
        for (int j = creature.getY() - MathUtils.ceil(radius); j <= creature.getY()
                + MathUtils.ceil(radius); j++) {
            boolean sameCoordinate = i == creature.getX() && j == creature.getY();
            boolean canStep = creature.world.canStepTo(i, j);
            boolean inRadius = position.dst(i, j) <= radius;
            if (inRadius && (sameCoordinate || canStep))
                available.add(new Grid2D.Coordinate(i, j));
        }// w  w  w.  j  a  v  a 2s  . co m
    }
    return available;
}

From source file:org.ams.testapps.paintandphysics.physicspuzzle.PhysicsPuzzle.java

License:Open Source License

/** Check all active blocks and restore them if they are too far away. */
private void restoreLostBlocks() {
    float maxDst = (rows + columns) * blockDim * 2;
    for (PPPolygon block : activeBlocks) {
        Vector2 pos = block.getPhysicsThing().getBody().getPosition();

        float dst = pos.dst(0, 0);
        if (dst > maxDst) {
            block.setPosition(0, rows * blockDim * 0.5f + blockDim * 3);
            block.getPhysicsThing().getBody().setLinearVelocity(0, 0);
        }/*from w w  w .j a  v  a 2  s. co m*/
    }
}