From bbc04275a73aec87d83f264fcf760407363d5c5b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 27 Jan 2009 15:22:34 -0500 Subject: Added parentMap data to Event StepMoveEvent has an issue where an event could walk off the screen under its influence because it preformed no collision checking, due to the fact that the parent map had to be accessable to preform collision checking. Now, both event styles (unified with an AbstractEvent class that handles functions common to both styles) carries information about its parent map, provided by EventList which is in turn provided by the parent map itself. --- .../fourpuzzle/gamestate/mapview/Map.java | 14 ++- .../gamestate/mapview/MapViewGameState.java | 9 +- .../gamestate/mapview/event/AbstractEvent.java | 122 +++++++++++++++++++++ .../fourpuzzle/gamestate/mapview/event/Event.java | 7 ++ .../gamestate/mapview/event/EventList.java | 28 ++++- .../gamestate/mapview/event/HeroEvent.java | 94 ++-------------- .../gamestate/mapview/event/LayerEvent.java | 103 +++-------------- 7 files changed, 195 insertions(+), 182 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java index 62270bf..bc0073e 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java @@ -6,7 +6,9 @@ package com.fourisland.fourpuzzle.gamestate.mapview; import com.fourisland.fourpuzzle.*; +import com.fourisland.fourpuzzle.gamestate.mapview.event.Event; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; +import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; import java.awt.Dimension; import java.util.HashMap; @@ -41,7 +43,7 @@ public abstract class Map { } } - private EventList events = new EventList(); + private EventList events = new EventList(this); public EventList getEvents() { return events; @@ -59,8 +61,11 @@ public abstract class Map { return null; } - public boolean checkForCollision(int x, int y, Direction toMove) + public boolean checkForCollision(Event ev, Direction toMove) { + int x = ev.getLocation().x; + int y = ev.getLocation().y; + if ((toMove == Direction.North) && (y == 0)) { return true; @@ -75,6 +80,11 @@ public abstract class Map { return true; } + if ((ev instanceof HeroEvent) && (((MapViewGameState) Game.getGameState()).debugWalkthrough)) + { + return false; + } + if ((toMove == Direction.North) && (checkForEventCollision(x, y-1))) { return true; diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 48653c0..0604dd0 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -29,7 +29,7 @@ import java.util.logging.Logger; */ public class MapViewGameState implements GameState { - boolean debugWalkthrough = false; + public boolean debugWalkthrough = false; boolean processInput = true; Map currentMap; @@ -99,12 +99,7 @@ public class MapViewGameState implements GameState { if (letsMove) { - if (debugWalkthrough || (!currentMap.checkForCollision(hero.getLocation().x, hero.getLocation().y, toMove))) - { - hero.startMoving(toMove); - } else { - hero.setDirection(toMove); - } + hero.startMoving(toMove); } if ((Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) || (Game.getKey().getKeyCode() == KeyEvent.VK_SPACE)) diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java new file mode 100644 index 0000000..e5334c8 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java @@ -0,0 +1,122 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.event; + +import com.fourisland.fourpuzzle.Direction; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; +import com.fourisland.fourpuzzle.util.Functions; +import java.awt.Point; + +/** + * + * @author hatkirby + */ +public abstract class AbstractEvent implements Event { + + private Point location = new Point(); + public Point getLocation() + { + return location; + } + public void setLocation(Point location) + { + this.location = location; + } + public void setLocation(int x, int y) + { + location.setLocation(x, y); + } + + private boolean moving = false; + public boolean isMoving() + { + return moving; + } + public void setMoving(boolean moving) + { + this.moving = moving; + } + + /* TODO Remove the moveDirection field. As direction itself is + * always the same as moveDirection when moveDirection is needed, + * it will do fine without having to complicated access modifiers + */ + + protected int moveTimer; + protected Direction moveDirection; + public void startMoving(Direction toMove) + { + setDirection(toMove); + + if (!getParentMap().checkForCollision(this, toMove)) + { + setAnimationStep(2); + moveTimer = 4; + setMoving(true); + moveDirection = toMove; + } + } + + public void processMoving() + { + if (isMoving()) + { + moveTimer--; + if (moveTimer == 2) + { + setAnimationStep(0); + } else if (moveTimer == 0) + { + setAnimationStep(1); + moving = false; + + if (moveDirection == Direction.North) + { + setLocation(getLocation().x,getLocation().y-1); + } else if (moveDirection == Direction.West) + { + setLocation(getLocation().x-1,getLocation().y); + } else if (moveDirection == Direction.South) + { + setLocation(getLocation().x,getLocation().y+1); + } else if (moveDirection == Direction.East) + { + setLocation(getLocation().x+1,getLocation().y); + } + } + } + } + + public boolean isOccupyingSpace(int x, int y) + { + if (getLocation().equals(new Point(x,y))) + { + return true; + } + + if (Functions.isMovingTo(this, x, y)) + { + return true; + } + + return false; + } + + private Map parentMap = null; + public Map getParentMap() + { + if (parentMap == null) + { + throw new NullPointerException(); + } + + return parentMap; + } + public void setParentMap(Map parentMap) + { + this.parentMap = parentMap; + } +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java index 71b507f..d6219b3 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java @@ -7,6 +7,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event; import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.Layer; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; import java.awt.Graphics; import java.awt.Point; @@ -34,4 +35,10 @@ public interface Event { public Layer getLayer(); public boolean isOccupyingSpace(int x, int y); + + public void setAnimationStep(int animStep); + public int getAnimationStep(); + + public void setParentMap(Map parentMap); + public Map getParentMap(); } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventList.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventList.java index a10aadc..8de4e3c 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventList.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventList.java @@ -5,6 +5,7 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; import java.util.Vector; /** @@ -13,6 +14,11 @@ import java.util.Vector; */ public class EventList extends Vector { + public EventList(Map parentMap) + { + setParentMap(parentMap); + } + @Override public boolean add(LayerEvent o) { @@ -27,7 +33,7 @@ public class EventList extends Vector { return false; } - for (Event ev : this) + for (LayerEvent ev : this) { if (ev.getLabel().equals(o.getLabel())) { @@ -36,7 +42,27 @@ public class EventList extends Vector { } } + if (parentMap != null) + { + o.setParentMap(parentMap); + } + return super.add(o); } + + private Map parentMap = null; + public Map getParentMap() + { + return parentMap; + } + public void setParentMap(Map parentMap) + { + this.parentMap = parentMap; + + for (LayerEvent ev : this) + { + ev.setParentMap(parentMap); + } + } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java index d488e7d..17140b9 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/HeroEvent.java @@ -11,18 +11,18 @@ import java.awt.Point; import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.Game; import com.fourisland.fourpuzzle.GameCharacter; -import com.fourisland.fourpuzzle.gamestate.mapview.CharSet; -import com.fourisland.fourpuzzle.util.Functions; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; +import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; /** * * @author hatkirby */ -public class HeroEvent implements Event { +public class HeroEvent extends AbstractEvent implements Event { public HeroEvent() { - location = new Point(); + setLocation(new Point()); } public String getLabel() @@ -30,26 +30,12 @@ public class HeroEvent implements Event { return "Hero"; } - private Point location; - public Point getLocation() - { - return location; - } - public void setLocation(Point location) - { - this.location = location; - } - public void setLocation(int x, int y) - { - location.setLocation(x, y); - } - public void render(Graphics g) { - int x = (location.x * 16) - 4; - int y = (location.y * 16) - 16; + int x = (getLocation().x * 16) - 4; + int y = (getLocation().y * 16) - 16; - if (moving) + if (isMoving()) { if (moveDirection == Direction.North) { @@ -75,57 +61,6 @@ public class HeroEvent implements Event { } } - - private boolean moving = false; - public boolean isMoving() - { - return moving; - } - public void setMoving(boolean moving) - { - this.moving = moving; - } - - private int moveTimer; - private Direction moveDirection; - public void startMoving(Direction toMove) - { - setDirection(toMove); - setAnimationStep(2); - moveTimer = 4; - moving = true; - moveDirection = toMove; - } - public void processMoving() - { - if (moving) - { - moveTimer--; - if (moveTimer == 2) - { - setAnimationStep(0); - } else if (moveTimer == 0) - { - setAnimationStep(1); - moving = false; - - if (moveDirection == Direction.North) - { - setLocation(getLocation().x,getLocation().y-1); - } else if (moveDirection == Direction.West) - { - setLocation(getLocation().x-1,getLocation().y); - } else if (moveDirection == Direction.South) - { - setLocation(getLocation().x,getLocation().y+1); - } else if (moveDirection == Direction.East) - { - setLocation(getLocation().x+1,getLocation().y); - } - } - } - } - private Direction direction = Direction.South; public Direction getDirection() { @@ -151,19 +86,10 @@ public class HeroEvent implements Event { return Layer.Middle; } - public boolean isOccupyingSpace(int x, int y) + @Override + public Map getParentMap() { - if (getLocation().equals(new Point(x,y))) - { - return true; - } - - if (Functions.isMovingTo(this, x, y)) - { - return true; - } - - return false; + return ((MapViewGameState) Game.getGameState()).getCurrentMap(); } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index f42cdf0..6be72b7 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java @@ -7,17 +7,16 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event; import com.fourisland.fourpuzzle.Layer; import java.awt.Graphics; -import java.awt.Point; import java.util.ArrayList; import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.gamestate.mapview.Map; -import com.fourisland.fourpuzzle.util.Functions; +import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic; /** * * @author hatkirby */ -public class LayerEvent implements Event { +public class LayerEvent extends AbstractEvent implements Event { /** Create a new Event instance * @@ -26,7 +25,7 @@ public class LayerEvent implements Event { */ public LayerEvent(int x, int y) { - location = new Point(x,y); + setLocation(x,y); events = new ArrayList(); label = "Unlabelled"; } @@ -49,20 +48,6 @@ public class LayerEvent implements Event { return label; } - private Point location; - public Point getLocation() - { - return location; - } - public void setLocation(Point location) - { - this.location = location; - } - public void setLocation(int x, int y) - { - location.setLocation(x, y); - } - private ArrayList events; public void addEvent(PossibleEvent pe) { @@ -92,10 +77,10 @@ public class LayerEvent implements Event { public void render(Graphics g) { - int x = (location.x * 16) - 4; - int y = (location.y * 16) - 16; + int x = (getLocation().x * 16) - 4; + int y = (getLocation().y * 16) - 16; - if (moving) + if (isMoving()) { if (moveDirection == Direction.North) { @@ -113,72 +98,19 @@ public class LayerEvent implements Event { } PossibleEvent toDraw = getPossibleEvent(); - if (!toDraw.getGraphic().equals("blank")) + if (!toDraw.getGraphic().equals(new BlankEventGraphic())) { g.drawImage(toDraw.getImage(), x, y, null); } } - - private boolean moving = false; - public boolean isMoving() - { - return moving; - } - public void setMoving(boolean moving) - { - this.moving = moving; - } - - private int moveTimer; - private Direction moveDirection; public void startMoving(Map map) { Direction toMove = getPossibleEvent().getMovement().nextMovement(); if (toMove != null) { - if (!map.checkForCollision(getLocation().x, getLocation().y, toMove)) - { - startMoving(toMove); - } - } - } - public void startMoving(Direction toMove) - { - getPossibleEvent().setDirection(toMove); - getPossibleEvent().setAnimationStep(2); - moveTimer = 4; - moving = true; - moveDirection = toMove; - } - public void processMoving() - { - if (moving) - { - moveTimer--; - if (moveTimer == 2) - { - getPossibleEvent().setAnimationStep(0); - } else if (moveTimer == 0) - { - getPossibleEvent().setAnimationStep(1); - moving = false; - - if (moveDirection == Direction.North) - { - setLocation(getLocation().x,getLocation().y-1); - } else if (moveDirection == Direction.West) - { - setLocation(getLocation().x-1,getLocation().y); - } else if (moveDirection == Direction.South) - { - setLocation(getLocation().x,getLocation().y+1); - } else if (moveDirection == Direction.East) - { - setLocation(getLocation().x+1,getLocation().y); - } - } + startMoving(toMove); } } @@ -209,20 +141,15 @@ public class LayerEvent implements Event { public void setLabel(String string) { this.label = string; } + + public void setAnimationStep(int animStep) + { + getPossibleEvent().setAnimationStep(animStep); + } - public boolean isOccupyingSpace(int x, int y) + public int getAnimationStep() { - if (getLocation().equals(new Point(x,y))) - { - return true; - } - - if (Functions.isMovingTo(this, x, y)) - { - return true; - } - - return false; + return getPossibleEvent().getAnimationStep(); } } \ No newline at end of file -- cgit 1.4.1