From 8f46c097bf0b3cf314af62a66428e04043d0a61f Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Wed, 28 Jan 2009 15:52:30 -0500 Subject: 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. --- .../gamestate/mapview/MapViewGameState.java | 17 +++++-- .../gamestate/mapview/event/EventCall.java | 15 +++++- .../gamestate/mapview/event/EventHandler.java | 54 ++++++++++++++++++++++ .../gamestate/mapview/event/HeroEvent.java | 9 ++++ .../gamestate/mapview/event/LayerEvent.java | 6 +++ 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventHandler.java (limited to 'src/com/fourisland') 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; import com.fourisland.fourpuzzle.Layer; import com.fourisland.fourpuzzle.PuzzleApplication; 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.specialmove.MoveEventThread; @@ -72,7 +73,7 @@ public class MapViewGameState implements GameState { debugWalkthrough = false; } - if (!hero.isMoving() && !MoveEventThread.isHeroMoving()) + if (!hero.isMoving() && !MoveEventThread.isHeroMoving() && !EventHandler.isRunningEvent()) { Direction toMove = null; Boolean letsMove = false; @@ -113,12 +114,12 @@ public class MapViewGameState implements GameState { if (Functions.isFacing(hero, ev)) { ev.setDirection(Functions.oppositeDirection(hero.getDirection())); - ev.getCallback().activate(); + ev.getCallback().activate(ev.getCalltime()); } } else { if (ev.getLocation().equals(hero.getLocation())) { - ev.getCallback().activate(); + ev.getCallback().activate(ev.getCalltime()); } } } @@ -144,11 +145,19 @@ public class MapViewGameState implements GameState { { if (!MoveEventThread.isOtherMoving(ev)) { - ev.startMoving(currentMap); + if (!EventHandler.isRunningEvent()) + { + ev.startMoving(currentMap); + } } } else { ev.processMoving(); } + + if (ev.getCalltime() == EventCallTime.ParallelProcess) + { + ev.getCallback().activate(ev.getCalltime()); + } } } 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 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event; +import java.util.concurrent.Future; + /** * * @author hatkirby @@ -22,9 +24,18 @@ public abstract class EventCall extends SpecialEvent implements Runnable { public abstract void run(); - public void activate() + private Future isRunning = null; + public void activate(EventCallTime calltime) { - new Thread(this, "Special Event").start(); + if ((isRunning == null) || (isRunning.isDone())) + { + if (calltime == EventCallTime.ParallelProcess) + { + isRunning = EventHandler.runParallel(this); + } else { + isRunning = EventHandler.runEvent(this); + } + } } } 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 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.event; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + + +/** + * + * @author hatkirby + */ +public class EventHandler { + + private static ExecutorService eventExecutorService = Executors.newSingleThreadExecutor(); + private static ExecutorService parallelExecutorService = Executors.newCachedThreadPool(); + private static Future eventAction = null; + + static { + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + public void run() + { + eventExecutorService.shutdownNow(); + parallelExecutorService.shutdownNow(); + } + })); + } + + public static Future runEvent(EventCall callback) + { + eventAction = eventExecutorService.submit(callback); + return eventAction; + } + + public static Future runParallel(EventCall callback) + { + return parallelExecutorService.submit(callback); + } + + public static boolean isRunningEvent() + { + if (eventAction == null) + { + return false; + } + + return (!eventAction.isDone()); + } + +} 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 { public void render(Graphics g) { GameCharacter toDraw = Game.getSaveFile().getParty().getLeader(); + + /* TODO Replace below condition with an instanceof check to + * see if toDraw.getGraphic() is an instance of BlankEventGraphic. + * The current way does not, in fact, work because an EventGraphic + * never be equal to a String. This current way only exists because + * HeroEvent's graphic used to be stored as a filename/offset combo + * instead of as an EventGraphic. + */ + if (!toDraw.getGraphic().equals("blank")) { 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 { public void render(Graphics g) { PossibleEvent toDraw = getPossibleEvent(); + + /* TODO Replace below condition with an instanceof check to + * see if toDraw.getGraphic() is an instance of BlankEventGraphic. + * The current way requires BlankEventGraphic() to be instantated + * many times for no reason. */ + if (!toDraw.getGraphic().equals(new BlankEventGraphic())) { g.drawImage(toDraw.getImage(), getRenderX(), getRenderY(), null); -- cgit 1.4.1