summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-01-28 15:52:30 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-01-28 15:52:30 -0500
commit8f46c097bf0b3cf314af62a66428e04043d0a61f (patch)
tree8e2ebbb3698e2a36fdb261a3bb651a4cd34a1b6e /src
parentc29a8870d46edd635963d54b3ada87db10352b76 (diff)
downloadfourpuzzle-8f46c097bf0b3cf314af62a66428e04043d0a61f.tar.gz
fourpuzzle-8f46c097bf0b3cf314af62a66428e04043d0a61f.tar.bz2
fourpuzzle-8f46c097bf0b3cf314af62a66428e04043d0a61f.zip
Created a manager for SpecialEvent action threads
EventHandler controls when they are executed and allows MapViewGameState to poll it to see if it is currently managing any action threads. If it is, MapViewGameState should be able to prevent certain actions from occuring (unless the action thread is ParallelProcess) such as keyboard input.
Diffstat (limited to 'src')
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java17
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java15
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventHandler.java54
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java9
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java6
5 files changed, 95 insertions, 6 deletions
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 0604dd0..65d2d2d 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
@@ -13,6 +13,7 @@ import com.fourisland.fourpuzzle.Game;
13import com.fourisland.fourpuzzle.Layer; 13import com.fourisland.fourpuzzle.Layer;
14import com.fourisland.fourpuzzle.PuzzleApplication; 14import com.fourisland.fourpuzzle.PuzzleApplication;
15import com.fourisland.fourpuzzle.gamestate.mapview.event.EventCallTime; 15import com.fourisland.fourpuzzle.gamestate.mapview.event.EventCallTime;
16import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler;
16import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; 17import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList;
17import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; 18import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
18import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; 19import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread;
@@ -72,7 +73,7 @@ public class MapViewGameState implements GameState {
72 debugWalkthrough = false; 73 debugWalkthrough = false;
73 } 74 }
74 75
75 if (!hero.isMoving() && !MoveEventThread.isHeroMoving()) 76 if (!hero.isMoving() && !MoveEventThread.isHeroMoving() && !EventHandler.isRunningEvent())
76 { 77 {
77 Direction toMove = null; 78 Direction toMove = null;
78 Boolean letsMove = false; 79 Boolean letsMove = false;
@@ -113,12 +114,12 @@ public class MapViewGameState implements GameState {
113 if (Functions.isFacing(hero, ev)) 114 if (Functions.isFacing(hero, ev))
114 { 115 {
115 ev.setDirection(Functions.oppositeDirection(hero.getDirection())); 116 ev.setDirection(Functions.oppositeDirection(hero.getDirection()));
116 ev.getCallback().activate(); 117 ev.getCallback().activate(ev.getCalltime());
117 } 118 }
118 } else { 119 } else {
119 if (ev.getLocation().equals(hero.getLocation())) 120 if (ev.getLocation().equals(hero.getLocation()))
120 { 121 {
121 ev.getCallback().activate(); 122 ev.getCallback().activate(ev.getCalltime());
122 } 123 }
123 } 124 }
124 } 125 }
@@ -144,11 +145,19 @@ public class MapViewGameState implements GameState {
144 { 145 {
145 if (!MoveEventThread.isOtherMoving(ev)) 146 if (!MoveEventThread.isOtherMoving(ev))
146 { 147 {
147 ev.startMoving(currentMap); 148 if (!EventHandler.isRunningEvent())
149 {
150 ev.startMoving(currentMap);
151 }
148 } 152 }
149 } else { 153 } else {
150 ev.processMoving(); 154 ev.processMoving();
151 } 155 }
156
157 if (ev.getCalltime() == EventCallTime.ParallelProcess)
158 {
159 ev.getCallback().activate(ev.getCalltime());
160 }
152 } 161 }
153 } 162 }
154 163
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java index 3bbde17..64ca592 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java
@@ -5,6 +5,8 @@
5 5
6package com.fourisland.fourpuzzle.gamestate.mapview.event; 6package com.fourisland.fourpuzzle.gamestate.mapview.event;
7 7
8import java.util.concurrent.Future;
9
8/** 10/**
9 * 11 *
10 * @author hatkirby 12 * @author hatkirby
@@ -22,9 +24,18 @@ public abstract class EventCall extends SpecialEvent implements Runnable {
22 24
23 public abstract void run(); 25 public abstract void run();
24 26
25 public void activate() 27 private Future isRunning = null;
28 public void activate(EventCallTime calltime)
26 { 29 {
27 new Thread(this, "Special Event").start(); 30 if ((isRunning == null) || (isRunning.isDone()))
31 {
32 if (calltime == EventCallTime.ParallelProcess)
33 {
34 isRunning = EventHandler.runParallel(this);
35 } else {
36 isRunning = EventHandler.runEvent(this);
37 }
38 }
28 } 39 }
29 40
30} 41}
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventHandler.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventHandler.java new file mode 100644 index 0000000..05997e0 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventHandler.java
@@ -0,0 +1,54 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.gamestate.mapview.event;
7
8import java.util.concurrent.ExecutorService;
9import java.util.concurrent.Executors;
10import java.util.concurrent.Future;
11
12
13/**
14 *
15 * @author hatkirby
16 */
17public class EventHandler {
18
19 private static ExecutorService eventExecutorService = Executors.newSingleThreadExecutor();
20 private static ExecutorService parallelExecutorService = Executors.newCachedThreadPool();
21 private static Future eventAction = null;
22
23 static {
24 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
25 public void run()
26 {
27 eventExecutorService.shutdownNow();
28 parallelExecutorService.shutdownNow();
29 }
30 }));
31 }
32
33 public static Future runEvent(EventCall callback)
34 {
35 eventAction = eventExecutorService.submit(callback);
36 return eventAction;
37 }
38
39 public static Future runParallel(EventCall callback)
40 {
41 return parallelExecutorService.submit(callback);
42 }
43
44 public static boolean isRunningEvent()
45 {
46 if (eventAction == null)
47 {
48 return false;
49 }
50
51 return (!eventAction.isDone());
52 }
53
54}
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java index b7a6ec7..8b9e3fb 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java
@@ -33,6 +33,15 @@ public class HeroEvent extends AbstractEvent implements Event {
33 public void render(Graphics g) 33 public void render(Graphics g)
34 { 34 {
35 GameCharacter toDraw = Game.getSaveFile().getParty().getLeader(); 35 GameCharacter toDraw = Game.getSaveFile().getParty().getLeader();
36
37 /* TODO Replace below condition with an instanceof check to
38 * see if toDraw.getGraphic() is an instance of BlankEventGraphic.
39 * The current way does not, in fact, work because an EventGraphic
40 * never be equal to a String. This current way only exists because
41 * HeroEvent's graphic used to be stored as a filename/offset combo
42 * instead of as an EventGraphic.
43 */
44
36 if (!toDraw.getGraphic().equals("blank")) 45 if (!toDraw.getGraphic().equals("blank"))
37 { 46 {
38 toDraw.getGraphic().setDirection(direction); 47 toDraw.getGraphic().setDirection(direction);
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index 1fac317..6201fc8 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java
@@ -78,6 +78,12 @@ public class LayerEvent extends AbstractEvent implements Event {
78 public void render(Graphics g) 78 public void render(Graphics g)
79 { 79 {
80 PossibleEvent toDraw = getPossibleEvent(); 80 PossibleEvent toDraw = getPossibleEvent();
81
82 /* TODO Replace below condition with an instanceof check to
83 * see if toDraw.getGraphic() is an instance of BlankEventGraphic.
84 * The current way requires BlankEventGraphic() to be instantated
85 * many times for no reason. */
86
81 if (!toDraw.getGraphic().equals(new BlankEventGraphic())) 87 if (!toDraw.getGraphic().equals(new BlankEventGraphic()))
82 { 88 {
83 g.drawImage(toDraw.getImage(), getRenderX(), getRenderY(), null); 89 g.drawImage(toDraw.getImage(), getRenderX(), getRenderY(), null);