diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-01-30 15:03:35 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-01-30 15:03:35 -0500 |
commit | 000384f5921bad138b77870fb6381fea5ac4d38b (patch) | |
tree | ecabd4bd7900c812c3f55b3d02474e4d35226971 /src/com/fourisland | |
parent | 0526f49e9b06d77f979d3e3ae67ce4f8b827a362 (diff) | |
download | fourpuzzle-000384f5921bad138b77870fb6381fea5ac4d38b.tar.gz fourpuzzle-000384f5921bad138b77870fb6381fea5ac4d38b.tar.bz2 fourpuzzle-000384f5921bad138b77870fb6381fea5ac4d38b.zip |
Implemented viewpoint-related Event actions
Implemented FixViewpoint(), PanViewpoint() and ResetViewpoint()
Diffstat (limited to 'src/com/fourisland')
7 files changed, 218 insertions, 4 deletions
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 { | |||
17 | 17 | ||
18 | public static final int WIDTH = 320; | 18 | public static final int WIDTH = 320; |
19 | public static final int HEIGHT = 240; | 19 | public static final int HEIGHT = 240; |
20 | public static final int FPS = (1000 / 20); // 20 fps | ||
20 | 21 | ||
21 | private static SaveFile saveFile; | 22 | private static SaveFile saveFile; |
22 | public static SaveFile getSaveFile() | 23 | 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 { | |||
107 | Game.setGameState(new TitleScreenGameState()); | 107 | Game.setGameState(new TitleScreenGameState()); |
108 | 108 | ||
109 | long iTickCount = System.currentTimeMillis(); | 109 | long iTickCount = System.currentTimeMillis(); |
110 | int iTickDelay = (1000 / 20); // 20 fps | 110 | long iTickTrigger = iTickCount + Game.FPS; |
111 | long iTickTrigger = iTickCount + iTickDelay; | ||
112 | 111 | ||
113 | while (true) | 112 | while (true) |
114 | { | 113 | { |
@@ -126,7 +125,7 @@ public class PuzzleApplication extends Application { | |||
126 | 125 | ||
127 | if (!debugSpeed) | 126 | if (!debugSpeed) |
128 | { | 127 | { |
129 | iTickTrigger = iTickCount + iTickDelay; | 128 | iTickTrigger = iTickCount + Game.FPS; |
130 | } | 129 | } |
131 | } | 130 | } |
132 | } | 131 | } |
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; | |||
16 | import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler; | 16 | import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler; |
17 | import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; | 17 | import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; |
18 | import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; | 18 | import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; |
19 | import com.fourisland.fourpuzzle.gamestate.mapview.event.SpecialEvent; | ||
19 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; | 20 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; |
20 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; | 21 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; |
21 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; | 22 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; |
@@ -43,6 +44,7 @@ public class MapViewGameState implements GameState { | |||
43 | setCurrentMap(map); | 44 | setCurrentMap(map); |
44 | Game.getSaveFile().getHero().setLocation(x, y); | 45 | Game.getSaveFile().getHero().setLocation(x, y); |
45 | currentViewpoint = new AutomaticViewpoint(currentMap); | 46 | currentViewpoint = new AutomaticViewpoint(currentMap); |
47 | SpecialEvent.setMapView(this); | ||
46 | } | 48 | } |
47 | 49 | ||
48 | public void initalize() | 50 | public void initalize() |
@@ -212,5 +214,15 @@ public class MapViewGameState implements GameState { | |||
212 | { | 214 | { |
213 | return currentMap; | 215 | return currentMap; |
214 | } | 216 | } |
217 | |||
218 | public Viewpoint getViewpoint() | ||
219 | { | ||
220 | return currentViewpoint; | ||
221 | } | ||
222 | |||
223 | public void setViewpoint(Viewpoint viewpoint) | ||
224 | { | ||
225 | currentViewpoint = viewpoint; | ||
226 | } | ||
215 | 227 | ||
216 | } | 228 | } |
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.*; | |||
9 | import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; | 9 | import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; |
10 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEvent; | 10 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEvent; |
11 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; | 11 | import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; |
12 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; | ||
13 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.FixedViewpoint; | ||
14 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.MovingViewpoint; | ||
15 | import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; | ||
16 | import java.util.concurrent.CountDownLatch; | ||
12 | import java.util.logging.Level; | 17 | import java.util.logging.Level; |
13 | import java.util.logging.Logger; | 18 | import java.util.logging.Logger; |
14 | 19 | ||
@@ -17,6 +22,12 @@ import java.util.logging.Logger; | |||
17 | * @author hatkirby | 22 | * @author hatkirby |
18 | */ | 23 | */ |
19 | public class SpecialEvent { | 24 | public class SpecialEvent { |
25 | |||
26 | protected static MapViewGameState mapView = null; | ||
27 | public static void setMapView(MapViewGameState mapView) | ||
28 | { | ||
29 | SpecialEvent.mapView = mapView; | ||
30 | } | ||
20 | 31 | ||
21 | /** | 32 | /** |
22 | * Display a message on the screen. | 33 | * Display a message on the screen. |
@@ -165,4 +176,71 @@ public class SpecialEvent { | |||
165 | } | 176 | } |
166 | } | 177 | } |
167 | 178 | ||
179 | /** | ||
180 | * Fixes the viewpoint in the current position | ||
181 | */ | ||
182 | public void FixViewpoint() | ||
183 | { | ||
184 | Viewpoint viewpoint = mapView.getViewpoint(); | ||
185 | mapView.setViewpoint(new FixedViewpoint(viewpoint.getX(),viewpoint.getY())); | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * Pans the viewpoint the the specified tile location | ||
190 | * | ||
191 | * @param x The x coordinate of the tile to pan to | ||
192 | * @param y The y coordinate of the tile to pan to | ||
193 | * @param length How long (in milliseconds) it will take to pan | ||
194 | * @param block If true, the game will wait for the pan to complete | ||
195 | * before executing any more commands | ||
196 | */ | ||
197 | public void PanViewpoint(final int x, final int y, int length, final boolean block) | ||
198 | { | ||
199 | Viewpoint viewpoint = mapView.getViewpoint(); | ||
200 | final CountDownLatch blocker; | ||
201 | |||
202 | if (block) | ||
203 | { | ||
204 | blocker = new CountDownLatch(1); | ||
205 | } else { | ||
206 | blocker = null; | ||
207 | } | ||
208 | |||
209 | mapView.setViewpoint(new MovingViewpoint(viewpoint.getX(), viewpoint.getY(), x*16, y*16, new Runnable() { | ||
210 | public void run() | ||
211 | { | ||
212 | mapView.setViewpoint(new FixedViewpoint(x*16,y*16)); | ||
213 | |||
214 | if (block) | ||
215 | { | ||
216 | blocker.countDown(); | ||
217 | } | ||
218 | } | ||
219 | }, length)); | ||
220 | |||
221 | if (block) | ||
222 | { | ||
223 | try { | ||
224 | blocker.await(); | ||
225 | } catch (InterruptedException ex) { | ||
226 | Logger.getLogger(SpecialEvent.class.getName()).log(Level.SEVERE, null, ex); | ||
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * Resets the viewpoint from whatever state is is currently in | ||
233 | */ | ||
234 | public void ResetViewpoint() | ||
235 | { | ||
236 | Viewpoint viewpoint = mapView.getViewpoint(); | ||
237 | AutomaticViewpoint dest = new AutomaticViewpoint(mapView.getCurrentMap()); | ||
238 | |||
239 | mapView.setViewpoint(new MovingViewpoint(viewpoint.getX(), viewpoint.getY(), dest.getX(), dest.getY(), new Runnable() { | ||
240 | public void run() { | ||
241 | mapView.setViewpoint(new AutomaticViewpoint(mapView.getCurrentMap())); | ||
242 | } | ||
243 | })); | ||
244 | } | ||
245 | |||
168 | } | 246 | } |
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 { | |||
27 | refresh(); | 27 | refresh(); |
28 | } | 28 | } |
29 | 29 | ||
30 | public void refresh() | 30 | private void refresh() |
31 | { | 31 | { |
32 | int x,y; | 32 | int x,y; |
33 | HeroEvent hero = Game.getHeroEvent(); | 33 | 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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; | ||
7 | |||
8 | /** | ||
9 | * | ||
10 | * @author hatkirby | ||
11 | */ | ||
12 | public class FixedViewpoint implements Viewpoint { | ||
13 | |||
14 | private int x; | ||
15 | private int y; | ||
16 | |||
17 | public FixedViewpoint(int x, int y) | ||
18 | { | ||
19 | this.x = x; | ||
20 | this.y = y; | ||
21 | } | ||
22 | |||
23 | public int getX() | ||
24 | { | ||
25 | return x; | ||
26 | } | ||
27 | |||
28 | public int getY() | ||
29 | { | ||
30 | return y; | ||
31 | } | ||
32 | |||
33 | } | ||
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 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.Game; | ||
9 | |||
10 | /** | ||
11 | * | ||
12 | * @author hatkirby | ||
13 | */ | ||
14 | public class MovingViewpoint implements Viewpoint { | ||
15 | |||
16 | private double x; | ||
17 | private double y; | ||
18 | private int sx; | ||
19 | private int sy; | ||
20 | private int dx; | ||
21 | private int dy; | ||
22 | private double speed; | ||
23 | private int last; | ||
24 | private int xdist; | ||
25 | private int ydist; | ||
26 | private Runnable callback; | ||
27 | |||
28 | public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback) | ||
29 | { | ||
30 | this(sx, sy, dx, dy, callback, 1000); | ||
31 | } | ||
32 | |||
33 | public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback, int length) | ||
34 | { | ||
35 | this.x = sx; | ||
36 | this.y = sy; | ||
37 | this.sx = sx; | ||
38 | this.sy = sy; | ||
39 | this.dx = dx; | ||
40 | this.dy = dy; | ||
41 | this.speed = length / Game.FPS; | ||
42 | this.last = (int) System.currentTimeMillis(); | ||
43 | this.xdist = dx - sx; | ||
44 | this.ydist = dy - sy; | ||
45 | this.callback = callback; | ||
46 | } | ||
47 | |||
48 | private void refresh() | ||
49 | { | ||
50 | x += (xdist / speed); | ||
51 | y += (ydist / speed); | ||
52 | |||
53 | if (((sx < dx) && (x > dx)) || ((sx > dx) && (x < dx))) | ||
54 | { | ||
55 | x = dx; | ||
56 | } | ||
57 | |||
58 | if (((sy < dy) && (y > dy)) || ((sy > dy) && (y < dy))) | ||
59 | { | ||
60 | y = dy; | ||
61 | } | ||
62 | |||
63 | if ((x == dx) && (y == dy)) | ||
64 | { | ||
65 | callback.run(); | ||
66 | } | ||
67 | |||
68 | last = (int) System.currentTimeMillis(); | ||
69 | } | ||
70 | |||
71 | public int getX() | ||
72 | { | ||
73 | if (System.currentTimeMillis() + speed > last) | ||
74 | { | ||
75 | refresh(); | ||
76 | } | ||
77 | |||
78 | return (int) x; | ||
79 | } | ||
80 | |||
81 | public int getY() | ||
82 | { | ||
83 | if (System.currentTimeMillis() + speed > last) | ||
84 | { | ||
85 | refresh(); | ||
86 | } | ||
87 | |||
88 | return (int) y; | ||
89 | } | ||
90 | |||
91 | } | ||