From 9ca53b553cfaf488f7e8e678721bf9e655fa377e Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 27 Jan 2009 14:28:43 -0500 Subject: Fixed "walk-thru-me" bug Previously, Map's checkForCollision did not properly check collision and would allow an event to initiate movement to a location another event was already moving to (but wasn't at yet). --- .../fourpuzzle/gamestate/mapview/Map.java | 67 ++++++++++------------ .../gamestate/mapview/MapViewGameState.java | 8 +++ .../fourpuzzle/gamestate/mapview/event/Event.java | 2 + .../gamestate/mapview/event/HeroEvent.java | 20 +++++++ .../gamestate/mapview/event/LayerEvent.java | 16 ++++++ .../gamestate/mapview/event/SpecialEvent.java | 14 +++-- .../mapview/event/movement/MovementType.java | 1 + .../mapview/event/specialmove/MoveEventThread.java | 18 +++++- src/com/fourisland/fourpuzzle/util/Functions.java | 30 ++++++++++ 9 files changed, 133 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java index 3e9c717..62270bf 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java @@ -20,7 +20,7 @@ public abstract class Map { public abstract void initalize(); - public void initalize(Dimension size) + protected void initalize(Dimension size) { setSize(size); mapData = new Vector>(); @@ -31,7 +31,7 @@ public abstract class Map { { return size; } - public void setSize(Dimension size) + private void setSize(Dimension size) { if ((size.width < 20) || (size.height < 15)) { @@ -58,7 +58,7 @@ public abstract class Map { return null; } - + public boolean checkForCollision(int x, int y, Direction toMove) { if ((toMove == Direction.North) && (y == 0)) @@ -75,48 +75,22 @@ public abstract class Map { return true; } - for (LayerEvent ev : events) - { - if (ev.getLayer() == Layer.Middle) - { - if ((ev.getLocation().y == (y - 1)) && (ev.getLocation().x == x) && (toMove == Direction.North)) - { - return true; - } - - if ((ev.getLocation().x == (x - 1)) && (ev.getLocation().y == y) && (toMove == Direction.West)) - { - return true; - } - - if ((ev.getLocation().y == (y + 1)) && (ev.getLocation().x == x) && (toMove == Direction.South)) - { - return true; - } - - if ((ev.getLocation().x == (x + 1)) && (ev.getLocation().y == y) && (toMove == Direction.East)) - { - return true; - } - } - } - - if ((Game.getHeroEvent().getLocation().y == (y - 1)) && (Game.getHeroEvent().getLocation().x == x) && (toMove == Direction.North)) + if ((toMove == Direction.North) && (checkForEventCollision(x, y-1))) { return true; } - - if ((Game.getHeroEvent().getLocation().x == (x - 1)) && (Game.getHeroEvent().getLocation().y == y) && (toMove == Direction.West)) + + if ((toMove == Direction.West) && (checkForEventCollision(x-1, y))) { return true; } - - if ((Game.getHeroEvent().getLocation().y == (y + 1)) && (Game.getHeroEvent().getLocation().x == x) && (toMove == Direction.South)) + + if ((toMove == Direction.South) && (checkForEventCollision(x, y+1))) { return true; } - - if ((Game.getHeroEvent().getLocation().x == (x + 1)) && (Game.getHeroEvent().getLocation().y == y) && (toMove == Direction.East)) + + if ((toMove == Direction.East) && (checkForEventCollision(x+1, y))) { return true; } @@ -156,6 +130,27 @@ public abstract class Map { return false; } + + private boolean checkForEventCollision(int x, int y) + { + for (LayerEvent ev : events) + { + if (ev.getLayer() == Layer.Middle) + { + if (ev.isOccupyingSpace(x, y)) + { + return true; + } + } + } + + if (Game.getHeroEvent().isOccupyingSpace(x, y)) + { + return true; + } + + return false; + } private String chipSet; public String getChipSet() { diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 3d8d15d..48653c0 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -159,6 +159,14 @@ public class MapViewGameState implements GameState { public void render(Graphics2D g) { + /* TODO Add code that checks to see if the map has been switched + * or if the hero has moved in such a way so as to move the + * viewpoint. If one or more of these conditions are filled, the + * already existing below code should run along with a snippet that + * saves a cache of the current viewpoint. If not, the previously + * mentioned cache should be displayed rather than re-rendering the + * map. + */ ChipSet chipSet = ChipSet.getChipSet(currentMap.getChipSet()); int i,x,y; for (i=0;i events = new Vector(); + + static volatile List events = new Vector(); + static volatile CountDownLatch moveEventWait = new CountDownLatch(0); Event ev; MoveEvent[] actions; @@ -51,6 +52,10 @@ public class MoveEventThread implements Runnable { moveEventWait.countDown(); } + /* TODO Rename the two following methods (isHeroMoving and isOtherMoving) + * to isHeroActive and isOtherActive respectively. + */ + public static boolean isHeroMoving() { return (events.contains(Game.getHeroEvent())); @@ -60,5 +65,14 @@ public class MoveEventThread implements Runnable { { return (events.contains(event)); } + + public static void moveAll() + { + try { + moveEventWait.await(); + } catch (InterruptedException ex) { + Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex); + } + } } diff --git a/src/com/fourisland/fourpuzzle/util/Functions.java b/src/com/fourisland/fourpuzzle/util/Functions.java index 038ca46..9966a2f 100644 --- a/src/com/fourisland/fourpuzzle/util/Functions.java +++ b/src/com/fourisland/fourpuzzle/util/Functions.java @@ -48,6 +48,36 @@ public class Functions { return false; } + + public static boolean isMovingTo(Event ev, int x, int y) + { + if (ev.isMoving() == false) + { + return false; + } + + if ((ev.getDirection() == Direction.North) && ((ev.getLocation().y-1) == y) && (ev.getLocation().x == x)) + { + return true; + } + + if ((ev.getDirection() == Direction.West) && (ev.getLocation().y == y) && ((ev.getLocation().x-1) == x)) + { + return true; + } + + if ((ev.getDirection() == Direction.South) && ((ev.getLocation().y+1) == y) && (ev.getLocation().x == x)) + { + return true; + } + + if ((ev.getDirection() == Direction.East) && (ev.getLocation().y == y) && ((ev.getLocation().x+1) == x)) + { + return true; + } + + return false; + } public static Direction oppositeDirection(Direction dir) { -- cgit 1.4.1