From 3724f4ecbe61e6621d4a7e993cd80ab75ee41266 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 7 Feb 2009 22:03:22 -0500 Subject: Added TurnLeft and TurnRight AnimationTypes Also implemented opposite(), left() and right() functions for Direction which return the opposite, counterclockwise and clockwise directions respectively. I don't exactly like the current Direction implementation as it's not very elegant. Hopefully there is a way to make this prettier. Also added a setter to PossibleEvent that allows LayerEvent to notify it when it starts or stops moving. This is necessary because the TurnLeft and TurnRight AnimationTypes constantly attempt to rotate the Event and if the Direction of the Event is changed while it is moving, it will move to a different space and animation will look strange. Thus, setDirection() has been modified so it will disallow direction change if the Event is moving. Also fixed a small encapsulation bug in AbstractEvent. PossibleEvent's notification of movement was made possible through the overriding of the setMoving() function, which is used by AbstractEvent to set if the Event is currently moving. However, while it used setMoving() to turn on moving, previously it did not use it to turn off moving, it simply modified the field. This has been fixed. --- src/com/fourisland/fourpuzzle/Direction.java | 100 ++++++++++++++++----- .../gamestate/mapview/MapViewGameState.java | 2 +- .../gamestate/mapview/event/AbstractEvent.java | 2 +- .../gamestate/mapview/event/AnimationType.java | 40 ++++++++- .../gamestate/mapview/event/LayerEvent.java | 7 ++ .../gamestate/mapview/event/PossibleEvent.java | 8 +- 6 files changed, 133 insertions(+), 26 deletions(-) diff --git a/src/com/fourisland/fourpuzzle/Direction.java b/src/com/fourisland/fourpuzzle/Direction.java index 4742521..4fa0908 100644 --- a/src/com/fourisland/fourpuzzle/Direction.java +++ b/src/com/fourisland/fourpuzzle/Direction.java @@ -10,30 +10,90 @@ package com.fourisland.fourpuzzle; * @author hatkirby */ public enum Direction { - North, - East, - South, - West; + North + { + public Direction opposite() + { + return Direction.South; + } + + public Direction left() + { + return Direction.West; + } + + public Direction right() + { + return Direction.East; + } + }, + East + { + public Direction opposite() + { + return Direction.West; + } + + public Direction left() + { + return Direction.North; + } + + public Direction right() + { + return Direction.South; + } + }, + South + { + public Direction opposite() + { + return Direction.North; + } + + public Direction left() + { + return Direction.East; + } + + public Direction right() + { + return Direction.West; + } + }, + West + { + public Direction opposite() + { + return Direction.East; + } + + public Direction left() + { + return Direction.South; + } + + public Direction right() + { + return Direction.North; + } + }; /** - * TODO Find a way to serve the inverse of a Direction without using a - * switch, which is apparently bad practice + * Returns the direction opposite from the current one + * @return A Direction representing the wanted direction */ + public abstract Direction opposite(); /** - * Returns the direction opposite from the current one - * @return A Direction representing the opposite direction + * Returns the direction counterclockwise from the current one + * @return A Direction representing the wanted direction */ - public Direction oppositeDirection() - { - switch (this) - { - case North: return Direction.South; - case West: return Direction.East; - case South: return Direction.North; - case East: return Direction.West; - } - - return null; - } + public abstract Direction left(); + + /** + * Returns the direction clockwise from the current one + * @return A Direction representing the wanted direction + */ + public abstract Direction right(); } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 3283da5..3acfff4 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -134,7 +134,7 @@ public class MapViewGameState implements GameState { { if (Functions.isFacing(hero, ev)) { - ev.setDirection(hero.getDirection().oppositeDirection()); + ev.setDirection(hero.getDirection().opposite()); ev.getCallback().activate(ev.getCalltime()); } } else { diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java index 483ce49..2933fff 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java @@ -78,7 +78,7 @@ public abstract class AbstractEvent implements Event { } else if (moveTimer == 0) { setAnimationStep(1); - moving = false; + setMoving(false); if (getDirection() == Direction.North) { diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java index 7824470..4c3aeb9 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java @@ -46,10 +46,44 @@ public enum AnimationType { /** * An AnimationType that does not allow the Event to turn or animate. */ - FixedGraphic(false, false); + FixedGraphic(false, false), + /** + * An AnimationType that is identical to CommonWithoutStepping except that + * it causes the Event in question to continually rotate counterclockwise. + */ + TurnLeft(true, true) + { + Interval in = Interval.createTickInterval(2); + + @Override + public void tick(PossibleEvent pe) + { + if (in.isElapsed()) + { + pe.setDirection(pe.getDirection().left()); + } + } + }, + /** + * An AnimationType that is identical to CommonWithoutStepping except that + * it causes the Event in question to continually rotate clockwise. + */ + TurnRight(true, true) + { + Interval in = Interval.createTickInterval(2); + + @Override + public void tick(PossibleEvent pe) + { + if (in.isElapsed()) + { + pe.setDirection(pe.getDirection().right()); + } + } + }; - private boolean canTurn; - private boolean canStep; + private final boolean canTurn; + private final boolean canStep; private AnimationType(boolean canTurn, boolean canStep) { this.canTurn = canTurn; diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index e57d7f8..3c826cb 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java @@ -132,4 +132,11 @@ public class LayerEvent extends AbstractEvent implements Event { return getPossibleEvent().getAnimationStep(); } + @Override + public void setMoving(boolean moving) + { + super.setMoving(moving); + getPossibleEvent().setMoving(moving); + } + } \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java index f31dcaf..a8c3a51 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java @@ -119,6 +119,12 @@ public class PossibleEvent { return movement; } + private boolean moving = false; + void setMoving(boolean moving) + { + this.moving = moving; + } + Direction getDirection() { return direction; @@ -126,7 +132,7 @@ public class PossibleEvent { void setDirection(Direction direction) { - if (animation.canTurn()) + if (animation.canTurn() && !moving) { this.direction = direction; graphic.setDirection(direction); -- cgit 1.4.1