Android Open Source - TileArena Entity






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.entity;
/*from  w w w  . j a  va 2 s  . co m*/
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.concurrent.atomic.AtomicInteger;

import game.tile.arena.Game;
import game.tile.arena.entity.projectile.Projectile;
import game.tile.arena.sprite.EntitySprite;
import game.tile.arena.util.Position;

public abstract class Entity implements Comparable<Entity> {

    private static final int COLOR_INCREMENTS = 500;
    private static final int COLOR_MAX = COLOR_INCREMENTS*5-1;

    private static AtomicInteger idCounter = new AtomicInteger();
    public final int id;
    public final boolean orientation;

    public Position pos;

    private Position dpos = new Position(0, 0);
    protected EntitySprite sprite;
    protected boolean moving;
    protected int dir = 1;
    protected int lastDir;

    protected float speed = 8f;
    protected boolean hit = false;
    protected int colorCounter = 0;
    protected int health;

    protected Entity(String filePrefix, Position pos, int animSpeed, boolean orientation, int health, int speed) {
        id = idCounter.getAndIncrement();
        sprite = new EntitySprite(filePrefix, animSpeed, 64);
        this.pos = pos;
        this.orientation = orientation;
        this.health = health;
        this.speed = speed;
    }

    public abstract boolean update(double delta);

    public void render(SpriteBatch batch, double delta) {
        if (colorCounter >= 0)
            colorCounter -= delta*Game.FPS;
        sprite.setDirection(dir);
        if (pos.inView(16)) {
            if ((colorCounter / COLOR_INCREMENTS) % 2 == 0)
                batch.setColor(1f, 1f, 1f, 1f);
            else
                batch.setColor(0.5f, 0f, 0f, 1f);
            sprite.render(batch, (int)dpos.magnitude(), pos);
        }
        hit = false;
    }

    public boolean isCollision(Projectile p) {
        return sprite.isCollision(p, pos);
    }

    public void hit(int damage) {
        health -= damage;
        hit = true;
        colorCounter = COLOR_MAX;
    }

    @Override
    public int compareTo(Entity other) {
        return -Float.valueOf(pos.y).compareTo(other.pos.y);
    }

    public void updatePosition(Position dp, double delta) {
        dpos = dp.scale(delta*speed);
        pos = pos.add(dpos);
        moving = true;

        if (dpos.x == 0 && dpos.y == 0) {
            moving = false;
            dir = lastDir;
            sprite.notMoving();
        } else if (Math.abs(dpos.x) > Math.abs(dpos.y)) {
            if (dpos.x > 0)
                dir = 0;
            else
                dir = 2;
        } else {
            if (dpos.y > 0)
                dir = 1;
            else
                dir = 3;
        }

        lastDir = dir;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof Entity))
            return false;
        return ((Entity)o).id == id;
    }
}




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