From 000384f5921bad138b77870fb6381fea5ac4d38b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Fri, 30 Jan 2009 15:03:35 -0500 Subject: Implemented viewpoint-related Event actions Implemented FixViewpoint(), PanViewpoint() and ResetViewpoint() --- src/com/fourisland/fourpuzzle/Game.java | 1 + .../fourisland/fourpuzzle/PuzzleApplication.java | 5 +- .../gamestate/mapview/MapViewGameState.java | 12 +++ .../gamestate/mapview/event/SpecialEvent.java | 78 +++++++++++++++++++ .../mapview/viewpoint/AutomaticViewpoint.java | 2 +- .../mapview/viewpoint/FixedViewpoint.java | 33 ++++++++ .../mapview/viewpoint/MovingViewpoint.java | 91 ++++++++++++++++++++++ 7 files changed, 218 insertions(+), 4 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/FixedViewpoint.java create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/Game.java b/src/com/fourisland/fourpuzzle/Game.java index 0788a75..432d8ff 100644 --- a/src/com/fourisland/fourpuzzle/Game.java +++ b/src/com/fourisland/fourpuzzle/Game.java @@ -17,6 +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 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 6fbe3a0..0b0e0cb 100644 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java @@ -107,8 +107,7 @@ public class PuzzleApplication extends Application { Game.setGameState(new TitleScreenGameState()); long iTickCount = System.currentTimeMillis(); - int iTickDelay = (1000 / 20); // 20 fps - long iTickTrigger = iTickCount + iTickDelay; + long iTickTrigger = iTickCount + Game.FPS; while (true) { @@ -126,7 +125,7 @@ public class PuzzleApplication extends Application { if (!debugSpeed) { - iTickTrigger = iTickCount + iTickDelay; + iTickTrigger = iTickCount + Game.FPS; } } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 45eadc9..c4194d9 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -16,6 +16,7 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.EventCallTime; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; +import com.fourisland.fourpuzzle.gamestate.mapview.event.SpecialEvent; import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; @@ -43,6 +44,7 @@ public class MapViewGameState implements GameState { setCurrentMap(map); Game.getSaveFile().getHero().setLocation(x, y); currentViewpoint = new AutomaticViewpoint(currentMap); + SpecialEvent.setMapView(this); } public void initalize() @@ -212,5 +214,15 @@ public class MapViewGameState implements GameState { { return currentMap; } + + public Viewpoint getViewpoint() + { + return currentViewpoint; + } + + public void setViewpoint(Viewpoint viewpoint) + { + currentViewpoint = viewpoint; + } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 615396b..342b65f 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -9,6 +9,11 @@ import com.fourisland.fourpuzzle.*; import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEvent; import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; +import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; +import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.FixedViewpoint; +import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.MovingViewpoint; +import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; +import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.logging.Logger; @@ -17,6 +22,12 @@ import java.util.logging.Logger; * @author hatkirby */ public class SpecialEvent { + + protected static MapViewGameState mapView = null; + public static void setMapView(MapViewGameState mapView) + { + SpecialEvent.mapView = mapView; + } /** * Display a message on the screen. @@ -165,4 +176,71 @@ public class SpecialEvent { } } + /** + * Fixes the viewpoint in the current position + */ + public void FixViewpoint() + { + Viewpoint viewpoint = mapView.getViewpoint(); + mapView.setViewpoint(new FixedViewpoint(viewpoint.getX(),viewpoint.getY())); + } + + /** + * Pans the viewpoint the the specified tile location + * + * @param x The x coordinate of the tile to pan to + * @param y The y coordinate of the tile to pan to + * @param length How long (in milliseconds) it will take to pan + * @param block If true, the game will wait for the pan to complete + * before executing any more commands + */ + public void PanViewpoint(final int x, final int y, int length, final boolean block) + { + Viewpoint viewpoint = mapView.getViewpoint(); + final CountDownLatch blocker; + + if (block) + { + blocker = new CountDownLatch(1); + } else { + blocker = null; + } + + mapView.setViewpoint(new MovingViewpoint(viewpoint.getX(), viewpoint.getY(), x*16, y*16, new Runnable() { + public void run() + { + mapView.setViewpoint(new FixedViewpoint(x*16,y*16)); + + if (block) + { + blocker.countDown(); + } + } + }, length)); + + if (block) + { + try { + blocker.await(); + } catch (InterruptedException ex) { + Logger.getLogger(SpecialEvent.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + /** + * Resets the viewpoint from whatever state is is currently in + */ + public void ResetViewpoint() + { + Viewpoint viewpoint = mapView.getViewpoint(); + AutomaticViewpoint dest = new AutomaticViewpoint(mapView.getCurrentMap()); + + mapView.setViewpoint(new MovingViewpoint(viewpoint.getX(), viewpoint.getY(), dest.getX(), dest.getY(), new Runnable() { + public void run() { + mapView.setViewpoint(new AutomaticViewpoint(mapView.getCurrentMap())); + } + })); + } + } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java index 6f6d6b0..fbd7e03 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java @@ -27,7 +27,7 @@ public class AutomaticViewpoint implements Viewpoint { refresh(); } - public void refresh() + private void refresh() { int x,y; HeroEvent hero = Game.getHeroEvent(); diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/FixedViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/FixedViewpoint.java new file mode 100644 index 0000000..20a6dd0 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/FixedViewpoint.java @@ -0,0 +1,33 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; + +/** + * + * @author hatkirby + */ +public class FixedViewpoint implements Viewpoint { + + private int x; + private int y; + + public FixedViewpoint(int x, int y) + { + this.x = x; + this.y = y; + } + + public int getX() + { + return x; + } + + public int getY() + { + return y; + } + +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java new file mode 100644 index 0000000..eac4b49 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java @@ -0,0 +1,91 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; + +import com.fourisland.fourpuzzle.Game; + +/** + * + * @author hatkirby + */ +public class MovingViewpoint implements Viewpoint { + + private double x; + private double y; + private int sx; + private int sy; + private int dx; + private int dy; + private double speed; + private int last; + private int xdist; + private int ydist; + private Runnable callback; + + public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback) + { + this(sx, sy, dx, dy, callback, 1000); + } + + public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback, int length) + { + this.x = sx; + this.y = sy; + this.sx = sx; + this.sy = sy; + 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; + } + + private void refresh() + { + x += (xdist / speed); + y += (ydist / speed); + + if (((sx < dx) && (x > dx)) || ((sx > dx) && (x < dx))) + { + x = dx; + } + + if (((sy < dy) && (y > dy)) || ((sy > dy) && (y < dy))) + { + y = dy; + } + + if ((x == dx) && (y == dy)) + { + callback.run(); + } + + last = (int) System.currentTimeMillis(); + } + + public int getX() + { + if (System.currentTimeMillis() + speed > last) + { + refresh(); + } + + return (int) x; + } + + public int getY() + { + if (System.currentTimeMillis() + speed > last) + { + refresh(); + } + + return (int) y; + } + +} -- cgit 1.4.1