From b2b180730ad252b4a8d15d9bc59895b56c552c29 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 7 Feb 2009 21:34:37 -0500 Subject: Added tick-processing to AnimationType AnimationType is designed to assert some control over a PossibleEvent's Direction and AnimationStep. Previously, it could only allow or disallow the changing of one or both of those fields. Now, certain AnimationTypes (specifically CommonWithStepping, TurnLeft and TurnRight) can modify those fields as well every tick. --- src/com/fourisland/fourpuzzle/Direction.java | 5 +++ src/com/fourisland/fourpuzzle/Game.java | 2 +- .../fourisland/fourpuzzle/PuzzleApplication.java | 14 ++----- .../gamestate/mapview/event/AnimationType.java | 43 +++++++++++++-------- .../gamestate/mapview/event/PossibleEvent.java | 19 +-------- .../mapview/viewpoint/MovingViewpoint.java | 11 +++--- src/com/fourisland/fourpuzzle/util/Interval.java | 45 ++++++++++++++++++++++ 7 files changed, 88 insertions(+), 51 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/util/Interval.java diff --git a/src/com/fourisland/fourpuzzle/Direction.java b/src/com/fourisland/fourpuzzle/Direction.java index 948b024..4742521 100644 --- a/src/com/fourisland/fourpuzzle/Direction.java +++ b/src/com/fourisland/fourpuzzle/Direction.java @@ -15,6 +15,11 @@ public enum Direction { South, West; + /** + * 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 opposite direction diff --git a/src/com/fourisland/fourpuzzle/Game.java b/src/com/fourisland/fourpuzzle/Game.java index 432d8ff..d2e8943 100644 --- a/src/com/fourisland/fourpuzzle/Game.java +++ b/src/com/fourisland/fourpuzzle/Game.java @@ -17,7 +17,7 @@ public class Game { public static final int WIDTH = 320; public static final int HEIGHT = 240; - public static final int FPS = (1000 / 20); // 20 fps + public static final int FPS = (1000 / 30); // 30 fps private static SaveFile saveFile; public static SaveFile getSaveFile() diff --git a/src/com/fourisland/fourpuzzle/PuzzleApplication.java b/src/com/fourisland/fourpuzzle/PuzzleApplication.java index 80b6cd3..c8d4e5d 100644 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java @@ -6,6 +6,7 @@ package com.fourisland.fourpuzzle; import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; import com.fourisland.fourpuzzle.gamestate.mapview.ChipSet; +import com.fourisland.fourpuzzle.util.Interval; import java.awt.GraphicsEnvironment; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; @@ -108,14 +109,10 @@ public class PuzzleApplication extends Application { ChipSet.initalize(); Game.setGameState(new TitleScreenGameState()); - long iTickCount = System.currentTimeMillis(); - long iTickTrigger = iTickCount + Game.FPS; - + Interval in = Interval.createTickInterval(1); while (true) { - iTickCount = System.currentTimeMillis(); - - if ((iTickCount > iTickTrigger) && (!gameSleep)) + if ((debugSpeed || in.isElapsed()) && !gameSleep) { if (!Display.isTransitionRunning()) { @@ -128,11 +125,6 @@ public class PuzzleApplication extends Application { } Display.render(gameFrame); - - if (!debugSpeed) - { - iTickTrigger = iTickCount + Game.FPS; - } } } } catch (RuntimeException ex) { diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java index 20d6bfc..7824470 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AnimationType.java @@ -5,6 +5,8 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event; +import com.fourisland.fourpuzzle.util.Interval; + /** * * @author hatkirby @@ -14,35 +16,44 @@ public enum AnimationType { * The default AnimationType, it allows the Event to turn and to animate * while it walks, but it only animates while it moves. */ - CommonWithoutStepping(true, true, false), + CommonWithoutStepping(true, true), /** * An AnimationType which allows the Event to turn and to animate. It will * animate at all times, even while stationary. */ - CommonWithStepping(true, true, true), + CommonWithStepping(true, true) + { + Interval in = Interval.createTickInterval(2); + + @Override + public void tick(PossibleEvent pe) + { + if (in.isElapsed()) + { + if (pe.getAnimationStep() == 0) + { + pe.setAnimationStep(2); + } else { + pe.setAnimationStep(pe.getAnimationStep()-1); + } + } + } + }, /** * An AnimationType that allows the Event to turn, but not to animate. */ - WithoutStepping(true, false, false), + WithoutStepping(true, false), /** * An AnimationType that does not allow the Event to turn or animate. */ - FixedGraphic(false, false, false); + FixedGraphic(false, false); private boolean canTurn; private boolean canStep; - private boolean alwaysStepping; - private AnimationType(boolean canTurn, boolean canStep, boolean alwaysStepping) + private AnimationType(boolean canTurn, boolean canStep) { this.canTurn = canTurn; this.canStep = canStep; - - if (!canStep) - { - this.alwaysStepping = false; - } else { - this.alwaysStepping = alwaysStepping; - } } public boolean canTurn() @@ -55,9 +66,9 @@ public enum AnimationType { return canStep; } - public boolean isAlwaysStepping() + public void tick(PossibleEvent pe) { - return alwaysStepping; + // Do nothing } -} +} \ 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 931c6ca..f31dcaf 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java @@ -97,25 +97,10 @@ public class PossibleEvent { callback = builder.callback; } - private boolean aSLC = false; public EventGraphic getGraphic() { - if (animation.isAlwaysStepping()) - { - if (aSLC) - { - aSLC = false; - - if (animationStep == 0) - { - setAnimationStep(2); - } else { - setAnimationStep(animationStep-1); - } - } else { - aSLC = true; - } - } + animation.tick(this); + return graphic; } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java index eac4b49..e6db32b 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java @@ -6,6 +6,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; import com.fourisland.fourpuzzle.Game; +import com.fourisland.fourpuzzle.util.Interval; /** * @@ -20,10 +21,10 @@ public class MovingViewpoint implements Viewpoint { private int dx; private int dy; private double speed; - private int last; private int xdist; private int ydist; private Runnable callback; + private Interval in; public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback) { @@ -39,10 +40,10 @@ public class MovingViewpoint implements Viewpoint { this.dx = dx; this.dy = dy; this.speed = length / Game.FPS; - this.last = (int) System.currentTimeMillis(); this.xdist = dx - sx; this.ydist = dy - sy; this.callback = callback; + this.in = Interval.createMillisInterval((int) speed); } private void refresh() @@ -64,13 +65,11 @@ public class MovingViewpoint implements Viewpoint { { callback.run(); } - - last = (int) System.currentTimeMillis(); } public int getX() { - if (System.currentTimeMillis() + speed > last) + if (in.isElapsed()) { refresh(); } @@ -80,7 +79,7 @@ public class MovingViewpoint implements Viewpoint { public int getY() { - if (System.currentTimeMillis() + speed > last) + if (in.isElapsed()) { refresh(); } diff --git a/src/com/fourisland/fourpuzzle/util/Interval.java b/src/com/fourisland/fourpuzzle/util/Interval.java new file mode 100644 index 0000000..21a7a74 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/Interval.java @@ -0,0 +1,45 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.util; + +import com.fourisland.fourpuzzle.Game; + +/** + * + * @author hatkirby + */ +public class Interval { + + private int wait; + private Interval(int wait) + { + this.wait = wait; + } + + public static Interval createTickInterval(int ticks) + { + return new Interval(Game.FPS*ticks); + } + + public static Interval createMillisInterval(int millis) + { + return new Interval(millis); + } + + private long last = System.currentTimeMillis(); + public boolean isElapsed() + { + if (last+wait < System.currentTimeMillis()) + { + last = System.currentTimeMillis(); + + return true; + } + + return false; + } + +} -- cgit 1.4.1