Package game.actors

Source Code of game.actors.PlayerActor

package game.actors;

import engine.classes.Colour;
import engine.geometry.Rectangle;
import engine.geometry.Vector;
import engine.hierarchy.DefaultActor;
import engine.interfaces.RenderTarget;
import engine.utility.Space;
import game.habits.AnimationHabit;
import game.habits.DynamicHabit;
import game.habits.GravityHabit;
import game.habits.HurtHabit;
import game.habits.MagRayHabit;
import game.habits.PlatformCharacterAnimationHabit;
import game.habits.PlatformCharacterInputHabit;
import game.habits.PlatformCharacterMovementHabit;
import game.habits.PlatformCharacterStateHabit;
import game.scenes.LevelScene;

public final class PlayerActor extends DefaultActor {
    private static final int DUST_MAX = 1800; // 30 seconds worth
    private DynamicHabit dynamic;
    private Vector startPosition;
    private int dust;
    private Space space;
    private PlatformCharacterStateHabit state;

    public PlayerActor(Vector startPosition) {
        this.startPosition = startPosition;
        dust = DUST_MAX;
    }

    public void useDust(int amount) {
        dust -= amount;
        if (dust < 0) {
            dust = 0;
            kill();
        }
    }

    public void kill() {
        ((LevelScene) getScene()).restart();
    }

    @Override
    protected void onAdd() {
        AnimationHabit animation = addHabit(new AnimationHabit());
        dynamic = addHabit(new DynamicHabit(new Rectangle(startPosition, new Vector(24, 48)).toPolygon(), 60));
        addHabit(new GravityHabit(dynamic));
        state = addHabit(new PlatformCharacterStateHabit(dynamic));
        addHabit(new PlatformCharacterInputHabit(state));
        addHabit(new PlatformCharacterAnimationHabit("player", 52, animation, state));
        addHabit(new PlatformCharacterMovementHabit(state, dynamic, animation));
        addHabit(new MagRayHabit(dynamic));

        space = ((LevelScene) getScene()).getSpace();
        onAfterMove();
    }

    @Override
    protected void onRemove() {
        space.removeObject(this);
    }

    @Override
    protected void onAfterMove() {
        LevelScene ls = ((LevelScene) getScene());
        ls.viewFollow(dynamic.getPolygon().getMidX(), dynamic.getPolygon().getMidY(), 240, 180);

        HurtHabit h = ls.getSpace().findObject(dynamic.getPolygon(), HurtHabit.class);
        if (h != null) {
            useDust(30);
            state.hurt();
            h.hurt();
        }

        space.putObject(this, dynamic.getPolygon());
    }

    @Override
    protected void onDraw(RenderTarget target) {
        // HUD
        target.transformPush();
        target.transformIdentity();
        target.fillRectangle(new Rectangle(240, 24, 160, 16), new Colour(128, 128, 128, 128));
        target.fillRectangle(new Rectangle(240, 24, (160 * dust) / DUST_MAX, 16), new Colour(0, 128, 0, 128));
        target.transformPop();
    }
}
TOP

Related Classes of game.actors.PlayerActor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.