package game.habits;
import java.applet.Applet;
import java.util.HashMap;
import java.util.Map;
import engine.hierarchy.DefaultHabit;
import engine.interfaces.Image;
import engine.interfaces.Library;
import game.habits.PlatformCharacterStateHabit.FacingDirection;
import game.habits.PlatformCharacterStateHabit.HorizontalState;
import game.habits.PlatformCharacterStateHabit.OtherState;
import game.habits.PlatformCharacterStateHabit.VerticalState;
public final class PlatformCharacterAnimationHabit extends DefaultHabit {
private static final String[] TYPES = {"-stand", "-walk", "-run", "-skid", "-jump", "-fall", "-falldown", "-land",
"-turn", "-attack", "-hurt", "-dead"};
private static final String[] DIRECTIONS = {"-left", "-right"};
private static final int TURN_STEPS = 8;
private static final int LAND_STEPS = 8;
private static final int ATTACK_STEPS = 15;
private static final int HURT_STEPS = 30;
private static final int DEAD_STEPS = 60;
private static final int STEPS_PER_FRAME = 12;
private final String prefix;
private final int frameWidth;
private final Map<String, Image[]> animations;
private int landCount = LAND_STEPS;
private int turnCount = TURN_STEPS;
private int attackCount = ATTACK_STEPS;
private int hurtCount = HURT_STEPS;
private int deadCount = DEAD_STEPS;
private FacingDirection lastDirection;
private VerticalState lastV;
private OtherState lastO;
private PlatformCharacterStateHabit state;
private AnimationHabit animation;
public PlatformCharacterAnimationHabit(final String prefix, final int frameWidth, final AnimationHabit animation,
final PlatformCharacterStateHabit state) {
this.prefix = prefix;
this.frameWidth = frameWidth;
this.animation = animation;
this.state = state;
animations = new HashMap<String, Image[]>();
lastDirection = state.getFacingDirection();
lastV = state.getVerticalState();
lastO = state.getOtherState();
}
@Override
protected void onAdd() {
// Get animations from library
Library l = getStage().getLibrary();
for (String direction : DIRECTIONS) {
for (String type : TYPES) {
String end = type + direction;
animations.put(end, l.findImage(prefix + end).getFrames(frameWidth));
}
}
animation.setAnimation(animations.get("-stand-right"), STEPS_PER_FRAME);
}
@Override
protected void onAfterMove() {
// Get the current facing direction as a lower-case string
FacingDirection direction = state.getFacingDirection();
String directionString = "-" + direction.name().toLowerCase(((Applet) getStage()).getLocale());
HorizontalState h = state.getHorizontalState();
VerticalState v = state.getVerticalState();
OtherState o = state.getOtherState();
// Count how long since we turned
if (lastDirection != direction) {
turnCount = 0;
} else {
if (++turnCount >= TURN_STEPS) {
turnCount = TURN_STEPS;
}
}
// Count how long since we landed
if (lastV != v) {
landCount = 0;
} else {
if (++landCount >= LAND_STEPS) {
landCount = LAND_STEPS;
}
}
// Count how long since we attacked, got hurt, or died
if (lastO != o) {
if (o == OtherState.ATTACK) {
attackCount = 0;
hurtCount = HURT_STEPS;
deadCount = DEAD_STEPS;
} else if (o == OtherState.HURT) {
attackCount = ATTACK_STEPS;
hurtCount = 0;
deadCount = DEAD_STEPS;
} else if (o == OtherState.DEAD) {
attackCount = ATTACK_STEPS;
hurtCount = HURT_STEPS;
deadCount = 0;
}
} else {
if (++attackCount >= ATTACK_STEPS) {
attackCount = ATTACK_STEPS;
}
if (++hurtCount >= HURT_STEPS) {
hurtCount = HURT_STEPS;
}
if (++deadCount >= DEAD_STEPS) {
deadCount = DEAD_STEPS;
}
}
// Work out which animation we should be displaying
if (attackCount < ATTACK_STEPS) {
animation.setAnimation(animations.get("-attack" + directionString), STEPS_PER_FRAME);
} else if (hurtCount < HURT_STEPS) {
animation.setAnimation(animations.get("-hurt" + directionString), STEPS_PER_FRAME);
} else if (deadCount < DEAD_STEPS) {
animation.setAnimation(animations.get("-dead" + directionString), STEPS_PER_FRAME);
} else if (v == VerticalState.LAND) {
if (landCount < LAND_STEPS) {
animation.setAnimation(animations.get("-land" + directionString), STEPS_PER_FRAME);
} else if (turnCount < TURN_STEPS) {
animation.setAnimation(animations.get("-turn" + directionString), STEPS_PER_FRAME);
} else if (h == HorizontalState.STAND) {
animation.setAnimation(animations.get("-stand" + directionString), STEPS_PER_FRAME);
} else if (h == HorizontalState.SKID) {
animation.setAnimation(animations.get("-skid" + directionString), STEPS_PER_FRAME);
} else if (h == HorizontalState.WALK) {
animation.setAnimation(animations.get("-walk" + directionString), STEPS_PER_FRAME);
} else if (h == HorizontalState.RUN) {
animation.setAnimation(animations.get("-run" + directionString), STEPS_PER_FRAME);
}
} else if (v == VerticalState.JUMP) {
animation.setAnimation(animations.get("-jump" + directionString), STEPS_PER_FRAME);
} else if (v == VerticalState.FALL) {
if (h == HorizontalState.RUN || h == HorizontalState.WALK) {
animation.setAnimation(animations.get("-fall" + directionString), STEPS_PER_FRAME);
} else {
animation.setAnimation(animations.get("-falldown" + directionString), STEPS_PER_FRAME);
}
}
// Store the last state for variables we need to check for changes
lastDirection = direction;
lastV = v;
lastO = o;
state.otherHandled();
}
}