summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event
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/com/fourisland/fourpuzzle/gamestate/mapview/event
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/com/fourisland/fourpuzzle/gamestate/mapview/event')
-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
4 files changed, 82 insertions, 2 deletions
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);