Java Code Examples for com.badlogic.gdx.math.Vector2#dst()

The following examples show how to use com.badlogic.gdx.math.Vector2#dst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Fireball.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
public static Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, int x, int y, World world, int distance) {
    Vector2 creaturePos = tmp1.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) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > distance)
            continue;
        result.add(check);
    }
    return result;
}
 
Example 2
Source File: OnscreenControls.java    From ud406 with MIT License 6 votes vote down vote up
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    Vector2 viewportPosition = viewport.unproject(new Vector2(screenX, screenY));

    if (viewportPosition.dst(shootCenter) < Constants.BUTTON_RADIUS) {
        gigaGal.shoot();
    } else if (viewportPosition.dst(jumpCenter) < Constants.BUTTON_RADIUS) {
        jumpPointer = pointer;
        gigaGal.jumpButtonPressed = true;
    } else if (viewportPosition.dst(moveLeftCenter) < Constants.BUTTON_RADIUS) {
        moveLeftPointer = pointer;
        gigaGal.leftButtonPressed = true;
    } else if (viewportPosition.dst(moveRightCenter) < Constants.BUTTON_RADIUS) {
        moveRightPointer = pointer;
        gigaGal.rightButtonPressed = true;
    }

    return super.touchDown(screenX, screenY, pointer, button);
}
 
Example 3
Source File: BouncingBall.java    From ud405 with MIT License 6 votes vote down vote up
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    Vector2 worldClick = viewport.unproject(new Vector2(screenX, screenY));

    if (worldClick.dst(position) < baseRadius * radiusMultiplier) {
        Gdx.app.log("Ball", "Click in the ball, starting flick.");
        flicking = true;
        flickStart = worldClick;
    } else {
        // TODO: Set the target position
        targetPosition = worldClick;
        // TODO: Set the following flag
        following = true;
    }


    return true;
}
 
Example 4
Source File: Antidote.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
private Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, int x, int y, World world, float distance) {
    Vector2 creaturePos = tmp1.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) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > distance)
            continue;
        if (!check.hasEffect(PoisonEffect.class))
            continue;
        result.add(check);
    }
    return result;
}
 
Example 5
Source File: Shot.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
public static Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, int x, int y, World world, float distance) {
    Vector2 creaturePos = tmp1.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) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > distance)
            continue;
        result.add(check);
    }
    return result;
}
 
Example 6
Source File: OnscreenControls.java    From ud406 with MIT License 6 votes vote down vote up
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    Vector2 viewportPosition = viewport.unproject(new Vector2(screenX, screenY));

    if (pointer == moveLeftPointer && viewportPosition.dst(moveRightCenter) < Constants.BUTTON_RADIUS) {
        gigaGal.leftButtonPressed = false;
        moveLeftPointer = 0;

        moveRightPointer = pointer;
        gigaGal.rightButtonPressed = true;
    }

    if (pointer == moveRightPointer && viewportPosition.dst(moveLeftCenter) < Constants.BUTTON_RADIUS) {
        gigaGal.rightButtonPressed = false;
        moveRightPointer = 0;

        moveLeftPointer = pointer;
        gigaGal.leftButtonPressed = true;
    }

    return super.touchDragged(screenX, screenY, pointer);
}
 
Example 7
Source File: AreaOfAttack.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
protected Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation) {
    Vector2 creaturePos = tmp1.set(creature.getX(), creature.getY());
    World world = creature.world;
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : world) {
        if (!(object instanceof Creature))
            continue;
        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > radius)
            continue;
        result.add(check);
    }
    return result;
}
 
Example 8
Source File: ChainLightning.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
public static Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, int x, int y, World world, int distance) {
    Vector2 creaturePos = tmp1.set(x, y);
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : world) {
        if (!(object instanceof Creature))
            continue;
        if (object.getX() == x && object.getY() == y)
            continue;

        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > distance)
            continue;
        result.add(check);
    }
    return result;
}
 
Example 9
Source File: DifficultyScreen.java    From ud405 with MIT License 6 votes vote down vote up
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    // TODO: Unproject the touch from the screen to the world
    Vector2 worldTouch = viewport.unproject(new Vector2(screenX, screenY));

    // TODO: Check if the touch was inside a button, and launch the icicles screen with the appropriate difficulty

    if (worldTouch.dst(Constants.EASY_CENTER) < Constants.DIFFICULTY_BUBBLE_RADIUS) {
        game.showIciclesScreen(Difficulty.EASY);
    }

    if (worldTouch.dst(Constants.MEDIUM_CENTER) < Constants.DIFFICULTY_BUBBLE_RADIUS) {
        game.showIciclesScreen(Difficulty.MEDIUM);
    }

    if (worldTouch.dst(Constants.HARD_CENTER) < Constants.DIFFICULTY_BUBBLE_RADIUS) {
        game.showIciclesScreen(Difficulty.HARD);
    }

    return true;
}
 
Example 10
Source File: IceStorm.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
@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));
            }
        }
    }
    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));
        }
    });
}
 
Example 11
Source File: OnscreenControls.java    From ud406 with MIT License 5 votes vote down vote up
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    Vector2 viewportPosition = viewport.unproject(new Vector2(screenX, screenY));

    if (pointer == moveLeftPointer && viewportPosition.dst(moveRightCenter) < Constants.BUTTON_RADIUS) {

        // TODO: Handle the case where the left button touch has been dragged to the right button
        // Inform GigaGal that the left button is no longer pressed
        gigaGal.leftButtonPressed = false;

        // Inform GigaGal that the right button is now pressed
        gigaGal.rightButtonPressed = true;

        // Zero moveLeftPointer
        moveLeftPointer = 0;

        // Save moveRightPointer
        moveRightPointer = pointer;

    }

    if (pointer == moveRightPointer && viewportPosition.dst(moveLeftCenter) < Constants.BUTTON_RADIUS) {

        // TODO: Handle the case where the right button touch has been dragged to the left button
        gigaGal.rightButtonPressed = false;
        gigaGal.leftButtonPressed = true;
        moveRightPointer = 0;
        moveLeftPointer = pointer;

    }

    return super.touchDragged(screenX, screenY, pointer);
}
 
Example 12
Source File: SoundEmitterHandler.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
protected void process(int entityId) {
  SoundEmitter soundEmitter = mSoundEmitter.get(entityId);
  if (!soundEmitter.sound.isLoaded()) return;
  Vector2 position = mPosition.get(entityId).position;
  Vector2 src = mPosition.get(Riiablo.game.player).position;
  float dst = src.dst(position);
  float volume = 1 - (dst / 20f);
  volume = Math.max(volume, 0);
  volume = soundEmitter.interpolator.apply(volume);
  soundEmitter.sound.setVolume(volume);
  // TODO: volume = volume / sound.initialVolume
}
 
Example 13
Source File: ApplyToTarget.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
    return result;
}
 
Example 14
Source File: GotoAction.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
/**
 * If 'player' is far from 'actor', we bring it close. If 'player' is closed
 * from 'actor' do nothing.
 * 
 * TODO: DOESN'T WORK NOW
 * 
 * @param player
 * @param actor
 */
@SuppressWarnings("unused")
private void goNear(CharacterActor player, BaseActor actor, ActionCallback cb) {
	Rectangle rdest = actor.getBBox().getBoundingRectangle();

	// Vector2 p0 = new Vector2(player.getSprite().getX(),
	// player.getSprite().getY());
	Vector2 p0 = new Vector2(player.getX(), player.getY());

	// calculamos el punto más cercano al objeto
	Vector2 p1 = new Vector2(rdest.x, rdest.y); // izquierda
	Vector2 p2 = new Vector2(rdest.x + rdest.width, rdest.y); // derecha
	Vector2 p3 = new Vector2(rdest.x + rdest.width / 2, rdest.y); // centro
	float d1 = p0.dst(p1);
	float d2 = p0.dst(p2);
	float d3 = p0.dst(p3);
	Vector2 pf;

	if (d1 < d2 && d1 < d3) {
		pf = p1;
	} else if (d2 < d1 && d2 < d3) {
		pf = p2;
	} else {
		pf = p3;
	}

	player.goTo(pf, cb, ignoreWalkZone);
}
 
Example 15
Source File: Firestorm.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
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);
                }
            }
        }
    }
    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);
}
 
Example 16
Source File: RaycastCollisionDetector.java    From riiablo with Apache License 2.0 5 votes vote down vote up
public boolean collides(Ray<Vector2> ray, int flags, int size) {
  Vector2 start = ray.start;
  Vector2 end = ray.end;

  sample.set(start);
  delta.set(end).sub(start).setLength(DELTA);
  float add = delta.len();
  for (float curDist = 0, maxDist = start.dst(end); curDist < maxDist; curDist += add, sample.add(delta)) {
    if (map.flags(sample) != 0) return true;
  }

  return map.flags(end) != 0;
}
 
Example 17
Source File: DistanceJointDef.java    From tilt-game-android with MIT License 5 votes vote down vote up
/**
 * Initialize the bodies, anchors, and length using the world anchors.
 */
public void initialize (Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB) {
	this.bodyA = bodyA;
	this.bodyB = bodyB;
	this.localAnchorA.set(bodyA.getLocalPoint(anchorA));
	this.localAnchorB.set(bodyB.getLocalPoint(anchorB));
	this.length = anchorA.dst(anchorB);
}
 
Example 18
Source File: CursorMovementSystem.java    From riiablo with Apache License 2.0 4 votes vote down vote up
private void updateLeft() {
  int src = renderer.getSrc();
  boolean pressed = Gdx.input.isButtonPressed(Input.Buttons.LEFT);
  if (pressed && !requireRelease) {
    Item cursor = Riiablo.cursor.getItem();
    if (cursor != null) {
      itemController.cursorToGround();
      requireRelease = true;
      return;
    }

    // exiting dialog should block all input until button is released to prevent menu from closing the following frame
    if (dialogManager.getDialog() != null) {
      dialogManager.setDialog(null);
      requireRelease = true;
      return;
    } else if (menuManager.getMenu() != null) {
      menuManager.setMenu(null, Engine.INVALID_ENTITY);
    }

    // set target entity -- unsets and interacts when within range
    boolean touched = touchDown(src);
    if (!touched) {
      iso.agg(tmpVec2.set(Gdx.input.getX(), Gdx.input.getY())).unproject().toWorld();
      setTarget(src, tmpVec2);
    }
  } else if (!pressed) {
    //pathfinder.findPath(src, null);
    requireRelease = false;
    Target target = mTarget.get(src);
    if (target != null) {
      int targetId = target.target;
      Vector2 srcPos = mPosition.get(src).position;
      Vector2 targetPos = mPosition.get(targetId).position;
      // not interactable -> attacking? check weapon range to auto attack or cast spell
      Interactable interactable = mInteractable.get(targetId);
      if (interactable != null && srcPos.dst(targetPos) <= interactable.range) {
        setTarget(src, Engine.INVALID_ENTITY);
        interactable.interactor.interact(src, targetId);
      }
    }
  }
}
 
Example 19
Source File: BouncingBall.java    From ud405 with MIT License 4 votes vote down vote up
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    Vector2 worldClick = viewport.unproject(new Vector2(screenX, screenY));

    if (worldClick.dst(position) < baseRadius * radiusMultiplier) {
        Gdx.app.log("Ball", "Click in the ball, starting flick.");
        flicking = true;
        flickStart = worldClick;
    } else {
        // TODO: Set the target position

        // TODO: Set the following flag

    }


    return true;
}
 
Example 20
Source File: BouncingBall.java    From ud405 with MIT License 3 votes vote down vote up
/**
 * TODO: Check out what happens when a touch starts
 *
 * When a touch starts, we first need to translate the point that the user touched from screen
 * coordinates to world coordinates. Since the viewport handles the projection from world
 * coordinates to screen coordinates, it also has an unproject() method that does the opposite.
 *
 * Next we use the Vector2.dst() method to see if the distance between the touch and the
 * position of the ball is smaller than the ball's radius. If the touch is inside the radius,
 * then we start a flick, and save the world coordinates of the touch.
 */
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector2 worldClick = viewport.unproject(new Vector2(screenX, screenY));
    if (worldClick.dst(position) < baseRadius * radiusMultiplier) {
        flicking = true;
        flickStart = worldClick;
    }
    return true;
}