Package game.habits

Source Code of game.habits.PlatformCharacterMovementHabit

package game.habits;

import engine.geometry.Vector;
import engine.hierarchy.DefaultHabit;
import engine.interfaces.Clock;
import engine.interfaces.Keyboard;
import engine.interfaces.Mouse;
import game.habits.PlatformCharacterStateHabit.FacingDirection;
import game.habits.PlatformCharacterStateHabit.HorizontalState;
import game.habits.PlatformCharacterStateHabit.VerticalState;

public class PlatformCharacterMovementHabit extends DefaultHabit {
    private static final double MAX_RUN_SPEED = 3;
    private static final double MAX_WALK_SPEED = 1.5;
    private static final double RUN_IMPULSE = 10;
    private static final double WALK_IMPULSE = 10;
    private static final double JUMP_IMPULSE = 200;
    private static final double SKID_IMPULSE = 10;
    private static final int MAX_JUMP_SPEED = 5;
    private static final int MAX_FALL_SPEED = 5;

    PlatformCharacterStateHabit state;
    private DynamicHabit dynamic;
    private AnimationHabit animation;

    public PlatformCharacterMovementHabit(PlatformCharacterStateHabit state, DynamicHabit dynamic,
            AnimationHabit animation) {
        this.state = state;
        this.dynamic = dynamic;
        this.animation = animation;
    }

    @Override
    protected void onAdd() {
        animation.setPosition(dynamic.getPolygon().getMinX() - 14, dynamic.getPolygon().getMinY() - 4);
    }

    @Override
    protected void onBeforeMove(Keyboard keyboard, Mouse mouse, Clock clock) {
        if (state.getHorizontalState() == HorizontalState.RUN) {
            if (state.getFacingDirection() == FacingDirection.LEFT) {
                dynamic.applyImpulse(new Vector(-RUN_IMPULSE, 0));
            } else {
                dynamic.applyImpulse(new Vector(RUN_IMPULSE, 0));
            }
            if (dynamic.getVelocity().getX() > MAX_RUN_SPEED) {
                dynamic.getVelocity().setX(MAX_RUN_SPEED);
            }
            if (dynamic.getVelocity().getX() < -MAX_RUN_SPEED) {
                dynamic.getVelocity().setX(-MAX_RUN_SPEED);
            }
        } else if (state.getHorizontalState() == HorizontalState.WALK) {
            if (state.getFacingDirection() == FacingDirection.LEFT) {
                dynamic.applyImpulse(new Vector(-WALK_IMPULSE, 0));
            } else {
                dynamic.applyImpulse(new Vector(WALK_IMPULSE, 0));
            }
            if (dynamic.getVelocity().getX() > MAX_WALK_SPEED) {
                dynamic.getVelocity().setX(MAX_WALK_SPEED);
            }
            if (dynamic.getVelocity().getX() < -MAX_WALK_SPEED) {
                dynamic.getVelocity().setX(-MAX_WALK_SPEED);
            }
        } else {
            double x = dynamic.getVelocity().getX();
            if (x - SKID_IMPULSE / dynamic.getMass() > 0) {
                dynamic.applyImpulse(new Vector(-SKID_IMPULSE, 0));
            } else if (x + SKID_IMPULSE / dynamic.getMass() < 0) {
                dynamic.applyImpulse(new Vector(SKID_IMPULSE, 0));
            } else {
                dynamic.getVelocity().setX(0);
            }
        }

        if (dynamic.onGround()) {
            if (state.getVerticalState() == VerticalState.JUMP) {
                dynamic.applyImpulse(new Vector(0, -JUMP_IMPULSE));
                if (dynamic.getVelocity().getY() > MAX_JUMP_SPEED) {
                    dynamic.getVelocity().setY(MAX_JUMP_SPEED);
                }
                if (dynamic.getVelocity().getY() < -MAX_FALL_SPEED) {
                    dynamic.getVelocity().setY(-MAX_FALL_SPEED);
                }
            } else {
                state.stopJump();
            }
        } else {
            if (dynamic.getVelocity().getY() < 0) {
                state.jump();
            } else {
                state.fall();
            }
        }
    }

    @Override
    protected void onAfterMove() {
        animation.setPosition(dynamic.getPolygon().getMinX() - 14, dynamic.getPolygon().getMinY() - 4);
    }
}
TOP

Related Classes of game.habits.PlatformCharacterMovementHabit

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.