Android Open Source - TileArena Position






From Project

Back to project page TileArena.

License

The source code is released under:

MIT License

If you think the Android project TileArena listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package game.tile.arena.util;
/*from  w ww.j a  v a 2s .  c o  m*/
import game.tile.arena.Game;

public class Position {

    public final float x;
    public final float y;

    public Position(double x, double y) {
        this((float)x, (float)y);
    }

    public Position(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public Position(double dir) {
        this(Math.cos(dir), Math.sin(dir));
    }

    public Position scale(float scale) { return new Position(x*scale, y*scale); }
    public Position scale(double scale) { return scale((float)scale); }
    public Position subtract(Position pos) { return new Position(x-pos.x, y-pos.y); }
    public Position subtract(float nx, float ny) { return new Position(x-nx, y-ny); }
    public Position add(Position pos) { return new Position(x+pos.x, y+pos.y); }
    public Position add(float nx, float ny) { return new Position(x+nx, y+ny); }

    public boolean inRange(float dist, Position pos) {
        return (pos.x-x)*(pos.x-x) + (pos.y-y)*(pos.y-y) < dist*dist;
    }

    public Position limit(float dist, Position pos) {
        if (inRange(dist, pos))
            return pos;

        Position offset = new Position(x-pos.x, y-pos.y);
        float distance = (float)Math.sqrt(offset.x*offset.x + offset.y*offset.y);
        return this.subtract(offset.scale(dist / distance));
    }

    public Position addPolar(Position pos) {
        return new Position(x+pos.y*Math.cos(pos.x), y+pos.y*Math.sin(pos.x));
    }

    public boolean isZero() {
        return x==0 && y==0;
    }

    public double getDir() {
        return Math.atan2(y, x);
    }

    public double getDirTo(Position pos) {
        return Math.atan2(pos.y-y, pos.x-x);
    }

    public boolean inWorld(int padding) {
        return x>-padding && y>-padding && x<Game.WORLD.x+padding && y<Game.WORLD.y+padding;
    }

    public boolean inView(int pad) {
        float cx = Game.camera.position.x;
        float cy = Game.camera.position.y;

        float sx = Game.SCREEN.x/2;
        float sy = Game.SCREEN.y/2;

        return x>cx-sx-pad && y>cy-sy-pad && x<cx+sx+pad && y<cy+sy+pad;
    }

    public Position scaleY(double sy) {
        return new Position(x, y*sy);
    }

    public double magnitude() {
        return Math.sqrt(x*x + y*y);
    }

    @Override
    public String toString() {
        return x + " " + y;
    }

    public Position invertX() {
        return new Position(-x, y);
    }
}




Java Source Code List

game.tile.arena.Game.java
game.tile.arena.TileArena.java
game.tile.arena.android.AndroidLauncher.java
game.tile.arena.client.HtmlLauncher.java
game.tile.arena.desktop.DesktopLauncher.java
game.tile.arena.entity.Entity.java
game.tile.arena.entity.Player.java
game.tile.arena.entity.attack.AttackBow.java
game.tile.arena.entity.attack.AttackList.java
game.tile.arena.entity.attack.Attack.java
game.tile.arena.entity.enemy.Enemy.java
game.tile.arena.entity.enemy.ai.EnemyAI.java
game.tile.arena.entity.enemy.ai.EnemyDodgeAI.java
game.tile.arena.entity.enemy.ai.EnemyRandomAI.java
game.tile.arena.entity.projectile.LinearProjectile.java
game.tile.arena.entity.projectile.Projectile.java
game.tile.arena.sprite.EntitySprite.java
game.tile.arena.sprite.Image.java
game.tile.arena.util.MathHelper.java
game.tile.arena.util.Position.java
game.tile.arena.util.Preferences.java
game.tile.arena.util.controller.Joystick.java
game.tile.arena.util.controller.KeyboardController.java
game.tile.arena.util.controller.MouseAttackInput.java
game.tile.arena.util.controller.PlayerController.java
game.tile.arena.util.controller.TouchController.java
game.tile.arena.util.input.InputManager.java
game.tile.arena.util.input.JoystickInput.java
game.tile.arena.util.input.KeyboardMoveInput.java
game.tile.arena.util.input.WeaponSwitchInput.java
game.tile.arena.world.Tile.java
game.tile.arena.world.World.java