summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-02-16 13:03:07 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-02-16 13:03:07 -0500
commit0540ce566ddce97a60c3279c51d5c5ddf1645321 (patch)
treeded689d601e61d054c5e7c8d32802aafd0416665 /src
parent1db6402b9171fc13b377b01d49531659a4e1fc76 (diff)
downloadfourpuzzle-0540ce566ddce97a60c3279c51d5c5ddf1645321.tar.gz
fourpuzzle-0540ce566ddce97a60c3279c51d5c5ddf1645321.tar.bz2
fourpuzzle-0540ce566ddce97a60c3279c51d5c5ddf1645321.zip
Engine: Abstracted game state transitions
Previously, every game state transition was manually executed with a thread, some exception handling and Display. Now, Display has a method that takes care of it without leaving all of the bulk in random classes.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/Display.java33
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java18
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java18
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java23
4 files changed, 41 insertions, 51 deletions
diff --git a/src/com/fourisland/fourpuzzle/Display.java b/src/com/fourisland/fourpuzzle/Display.java index b29ab01..cf1cb2d 100755 --- a/src/com/fourisland/fourpuzzle/Display.java +++ b/src/com/fourisland/fourpuzzle/Display.java
@@ -5,6 +5,7 @@
5 5
6package com.fourisland.fourpuzzle; 6package com.fourisland.fourpuzzle;
7 7
8import com.fourisland.fourpuzzle.gamestate.GameState;
8import com.fourisland.fourpuzzle.transition.InTransition; 9import com.fourisland.fourpuzzle.transition.InTransition;
9import com.fourisland.fourpuzzle.transition.MultidirectionalTransition; 10import com.fourisland.fourpuzzle.transition.MultidirectionalTransition;
10import com.fourisland.fourpuzzle.transition.OutTransition; 11import com.fourisland.fourpuzzle.transition.OutTransition;
@@ -28,6 +29,8 @@ import java.io.InputStream;
28import java.util.List; 29import java.util.List;
29import java.util.concurrent.CopyOnWriteArrayList; 30import java.util.concurrent.CopyOnWriteArrayList;
30import java.util.concurrent.CountDownLatch; 31import java.util.concurrent.CountDownLatch;
32import java.util.concurrent.Executor;
33import java.util.concurrent.Executors;
31import java.util.logging.Level; 34import java.util.logging.Level;
32import java.util.logging.Logger; 35import java.util.logging.Logger;
33import org.jdesktop.application.ResourceMap; 36import org.jdesktop.application.ResourceMap;
@@ -204,6 +207,36 @@ public class Display {
204 } 207 }
205 } 208 }
206 209
210 private static Executor transitioner = Executors.newSingleThreadExecutor();
211 public static void transition(final Transition out, final GameState gameState, final Transition in, boolean thread)
212 {
213 Runnable transitionCall = new Runnable() {
214 public void run()
215 {
216 try {
217 Display.transition(out);
218 Game.setGameState(gameState);
219 Display.transition(in);
220 } catch (InterruptedException ex) {
221 Thread.currentThread().interrupt();
222 } catch (RuntimeException ex)
223 {
224 PuzzleApplication.INSTANCE.reportError(ex);
225 } catch (Error ex)
226 {
227 PuzzleApplication.INSTANCE.reportError(ex);
228 }
229 }
230 };
231
232 if (thread)
233 {
234 transitioner.execute(transitionCall);
235 } else {
236 transitionCall.run();
237 }
238 }
239
207 public static boolean isTransitionRunning() 240 public static boolean isTransitionRunning()
208 { 241 {
209 return transitionRunning; 242 return transitionRunning;
diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java index e87f80a..670d900 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java
@@ -38,23 +38,7 @@ public class GameOverGameState implements GameState {
38 { 38 {
39 Game.setSaveFile(new SaveFile()); 39 Game.setSaveFile(new SaveFile());
40 40
41 new Thread(new Runnable() { 41 Display.transition(Database.getTransition(Transitions.GameOverToTitle), new TitleScreenGameState(), Database.getTransition(Transitions.TitleEnter), true);
42 public void run() {
43 try {
44 Display.transition(Database.getTransition(Transitions.GameOverToTitle));
45 } catch (InterruptedException ex) {
46 Thread.currentThread().interrupt();
47 }
48
49 Game.setGameState(new TitleScreenGameState());
50
51 try {
52 Display.transition(Database.getTransition(Transitions.TitleEnter));
53 } catch (InterruptedException ex) {
54 Thread.currentThread().interrupt();
55 }
56 }
57 }).start();
58 } 42 }
59 } 43 }
60 44
diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index d40b3f9..a72a22a 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java
@@ -56,23 +56,7 @@ public class TitleScreenGameState implements GameState {
56 { 56 {
57 Game.setSaveFile(new SaveFile()); 57 Game.setSaveFile(new SaveFile());
58 58
59 new Thread(new Runnable() { 59 Display.transition(Database.getTransition(Transitions.TitleExit), new MapViewGameState("TestMap", 1, 2), Database.getTransition(Transitions.TitleToMap), true);
60 public void run() {
61 try {
62 Display.transition(Database.getTransition(Transitions.TitleExit));
63 } catch (InterruptedException ex) {
64 Thread.currentThread().interrupt();
65 }
66
67 Game.setGameState(new MapViewGameState("TestMap", 1, 2));
68
69 try {
70 Display.transition(Database.getTransition(Transitions.TitleToMap));
71 } catch (InterruptedException ex) {
72 Thread.currentThread().interrupt();
73 }
74 }
75 }).start();
76 } else if (choices.getSelected().equals(Database.getVocab(Vocabulary.LoadGame))) 60 } else if (choices.getSelected().equals(Database.getVocab(Vocabulary.LoadGame)))
77 { 61 {
78 // Do nothing, yet 62 // Do nothing, yet
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 9336616..ebb0e27 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java
@@ -155,12 +155,9 @@ public class SpecialEvent {
155 */ 155 */
156 public void GameOver() throws InterruptedException 156 public void GameOver() throws InterruptedException
157 { 157 {
158 Display.transition(Database.getTransition(Transitions.MapToGameOver));
159
160 Audio.stopMusic(); 158 Audio.stopMusic();
161 Game.setGameState(new GameOverGameState()); 159
162 160 Display.transition(Database.getTransition(Transitions.MapToGameOver), new GameOverGameState(), Database.getTransition(Transitions.GameOverEnter), false);
163 Display.transition(Database.getTransition(Transitions.GameOverEnter));
164 } 161 }
165 162
166 /** 163 /**
@@ -170,12 +167,9 @@ public class SpecialEvent {
170 */ 167 */
171 public void TitleScreen() throws InterruptedException 168 public void TitleScreen() throws InterruptedException
172 { 169 {
173 Display.transition(Database.getTransition(Transitions.MapToTitle));
174
175 Audio.stopMusic(); 170 Audio.stopMusic();
176 Game.setGameState(new TitleScreenGameState());
177 171
178 Display.transition(Database.getTransition(Transitions.TitleEnter)); 172 Display.transition(Database.getTransition(Transitions.MapToTitle), new TitleScreenGameState(), Database.getTransition(Transitions.TitleEnter), false);
179 } 173 }
180 174
181 private boolean startedTransition = false; 175 private boolean startedTransition = false;
@@ -217,14 +211,9 @@ public class SpecialEvent {
217 { 211 {
218 if (!startedTransition) 212 if (!startedTransition)
219 { 213 {
220 Display.transition(Database.getTransition(Transitions.MapExit)); 214 Display.transition(Database.getTransition(Transitions.MapExit), new MapViewGameState(map, x, y), Database.getTransition(Transitions.MapEnter), false);
221 } 215 } else {
222 216 Game.setGameState(new MapViewGameState(map, x, y));
223 Game.setGameState(new MapViewGameState(map, x, y));
224
225 if (!startedTransition)
226 {
227 Display.transition(Database.getTransition(Transitions.MapEnter));
228 } 217 }
229 } 218 }
230 219