Enemy Class Inheritance.

Android Programming Tutorial.

Android Code.

Cycle 3 enemy class expands from two other classes. Starting at BaseGroup.java expanding LibGDX’s Group Class, then BaseEnemy.java extends BaseGroup.java and finally Enemy.java expanding BaseEnemy.java.

At the lowest level only collision Rectangle and center coordinates.


/**
 * Created by Oin on 2/17/2016.
 *
 */
public class BaseGroup extends Group {

    protected Rectangle collisionRect = new Rectangle();

    protected float halfWidth, halfHeight;

    @Override
    public void setPosition(float x, float y) {
        super.setPosition(x, y);
        collisionRect.x = getX();
        collisionRect.y = getY();
    }

    public float getCenterX(){
        return getX() + halfWidth;
    }

    public float getCenterY(){
        return getY() + halfHeight;
    }

}

Abstract class BaseEnemy.java implements LibGDX Poolable and specifies enemy life, state, point value and speed.


/**
 * Created by Oin on 2/17/2016.
 *
 */
public abstract class BaseEnemy extends BaseGroup implements Pool.Poolable {

    protected enum STATE { DEAD, DYING, ALIVE}
    protected STATE state = STATE.DEAD;

    protected int life;
    protected float speed = 0;

    public BaseEnemy(){

    }

    protected abstract int getValue();

    protected abstract void takeAction();

    @Override
    public void act(float delta) {
        super.act(delta);

        if (state == STATE.ALIVE) {

        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
    }

    protected void manifest(

    }


    public void speedUp(){

    }

    protected void hit(){

    }

    private void hurt(){

    }

    /**
     * If direction is given, the enemy will dye opposite
     * from direction. If omitted then it will dye with a
     * random horizontal shift (Player Killed).
     *
     * @param direction = bullet direction.
     */
    protected void kill(Vector2 direction){

        state = STATE.DYING;        
    }

    protected abstract void recycle();

    protected abstract int getFullLife();

    @Override
    public void reset() {

        state = STATE.DEAD;
    }
}

Finally, Enemy.java with the divided textures (BODY, ARMOR, and HIGHLIGHT).


/**
 * Created by Oin on 2/17/2016.
 *
 */
public class Kamikaze extends BaseEnemy {

    protected BaseGroupActor BODY;
    protected BaseGroupActor ARMOR;
    protected BaseGroupActor HIGHLIGHT;

    public Kamikaze () {
        super();

        BODY = new BaseGroupActor(RM.get().TR.get(ST.TEXTURE.S_E_A_BODY), 0, 0);
        HIGHLIGHT = new BaseGroupActor(RM.get().TR.get(ST.TEXTURE.S_E_A_HIGHLIGHT), 0, 0);
        ARMOR = new BaseGroupActor(RM.get().TR.get(ST.TEXTURE.S_E_A_ARMOR), 0, 0);

        addActor(HIGHLIGHT);
        addActor(BODY);
        addActor(ARMOR);

        setOrigin(BODY.halfWidth, BODY.halfHeight);
        setBounds(0, 0, BODY.getWidth(), BODY.getHeight());

        collisionRect.width = BODY.getWidth();
        collisionRect.height = BODY.getHeight();
    }

    @Override
    protected int getFullLife() {

    }

    @Override
    protected int getValue() {
        return VALUE;
    }

    @Override
    protected void takeAction() {

    }

    @Override
    protected void recycle() {

    }
}