From cf2167710c31312355949d2a64698498fa5ca49f Mon Sep 17 00:00:00 2001 From: Andreas Jakobsson Date: Tue, 16 Dec 2008 23:15:36 +0100 Subject: [PATCH] MaImplemented landOn in PathSquare --- src/resources/levels.xml | 2 +- src/se/umu/cs/dit06ajnajs/ATDController.java | 72 ++++++++++++++-------- src/se/umu/cs/dit06ajnajs/ATDModel.java | 5 ++ src/se/umu/cs/dit06ajnajs/agent/Unit.java | 28 ++++++--- src/se/umu/cs/dit06ajnajs/map/Map.java | 67 +++++++++++++------- .../dit06ajnajs/map/MapSquarePrototypeFactory.java | 5 ++ src/se/umu/cs/dit06ajnajs/map/PathSquare.java | 2 +- 7 files changed, 119 insertions(+), 62 deletions(-) diff --git a/src/resources/levels.xml b/src/resources/levels.xml index 17267f0..93c68f3 100644 --- a/src/resources/levels.xml +++ b/src/resources/levels.xml @@ -7,7 +7,7 @@ - + diff --git a/src/se/umu/cs/dit06ajnajs/ATDController.java b/src/se/umu/cs/dit06ajnajs/ATDController.java index cc3eb65..c67e578 100644 --- a/src/se/umu/cs/dit06ajnajs/ATDController.java +++ b/src/se/umu/cs/dit06ajnajs/ATDController.java @@ -12,17 +12,19 @@ import se.umu.cs.dit06ajnajs.agent.Agent; import se.umu.cs.dit06ajnajs.agent.BasicTower; import se.umu.cs.dit06ajnajs.agent.Direction; import se.umu.cs.dit06ajnajs.agent.FootmanUnit; +import se.umu.cs.dit06ajnajs.agent.Unit; +import se.umu.cs.dit06ajnajs.map.GoalSquare; import se.umu.cs.dit06ajnajs.map.Map; import se.umu.cs.dit06ajnajs.map.MapSquare; public class ATDController { private static Logger logger = Logger.getLogger("AntiTD"); - + private final int FRAMES_PER_SECOND = 20; private ATDModel model; private ATDView view; - + private boolean running; private Thread animationThread; @@ -31,26 +33,24 @@ public class ATDController { model = new ATDModel(maps); view = new ATDView(model); - model.addAgent(new FootmanUnit(100,200, 20, 20, 1, Direction.UP, maps.get(0))); - model.addAgent(new FootmanUnit(140,200, 20, 20, 2, Direction.UP, maps.get(0))); - model.addAgent(new FootmanUnit(-20,50, 20, 20, 1, Direction.RIGHT, maps.get(0))); - model.addAgent(new FootmanUnit(-20,100, 20, 20, 4, Direction.RIGHT, maps.get(0))); - model.addAgent(new FootmanUnit(-20,150, 20, 20, 2, Direction.RIGHT, maps.get(0))); - - model.addTower(new BasicTower(20, 20, 10, (int) (AntiTD.SQUARE_SIZE * 1.5))); - model.addTower(new BasicTower(20, 20, 10, (int) (AntiTD.SQUARE_SIZE * 2.5))); + model.addAgent(new FootmanUnit(100,200, 20, 20, 3, Direction.UP, maps.get(0))); + //model.addAgent(new FootmanUnit(140,200, 20, 20, 2, Direction.UP, maps.get(0))); + + + //model.addTower(new BasicTower(20, 20, 10, (int) (AntiTD.SQUARE_SIZE * 1.5))); + //model.addTower(new BasicTower(20, 20, 10, (int) (AntiTD.SQUARE_SIZE * 2.5))); initGame(); running = true; animationThread = new Thread(new AnimationThread()); animationThread.start(); } - + public void initGame() { - view.addMapListener(new MapListener()); + view.addMapListener(new MapListener()); } - + private class AnimationThread implements Runnable { public void run() { // TODO Auto-generated method stub @@ -64,34 +64,52 @@ public class ATDController { agent.act(); } + // Remove units from goalsquares and count points + GoalSquare[] goalSquares = model.getGoalSquares(); + for (GoalSquare square : goalSquares) { + List goalUnits = square.getUnits(); + if(goalUnits != null) { + for (Unit unit : goalUnits) { + agents.remove(unit); + logger.info("Unit >" + unit.getClass().getSimpleName() + + "< has reached a goal and is now removed"); + //TODO count points + } + goalUnits.clear(); + } + + } + + + // Repaint all agents Graphics g = view.getGameGraphics(); for (Agent agent : agents) { ((Paintable) agent).paint(g); } - + // Refresh the game view view.repaintGame(); - + // Try to keep a given number of frames per second. try { - Thread.sleep(1000 / FRAMES_PER_SECOND); + Thread.sleep(1000 / FRAMES_PER_SECOND); } catch (InterruptedException e) { - System.err.println("Error in thread, exiting."); - return; + System.err.println("Error in thread, exiting."); + return; } } } } - + private class MapListener extends MouseAdapter { - public void mouseClicked(MouseEvent me) { - int x = me.getX(); - int y = me.getY(); - Map map = model.getMap(); - MapSquare square = map.getMapSquareAtPoint(x, y); - logger.info("Mouse clicked @ (" + x + ", " + y + ")"); - logger.info("MapSquare @ " + square); - } + public void mouseClicked(MouseEvent me) { + int x = me.getX(); + int y = me.getY(); + Map map = model.getMap(); + MapSquare square = map.getMapSquareAtPoint(x, y); + logger.info("Mouse clicked @ (" + x + ", " + y + ")"); + logger.info("MapSquare @ " + square); + } } } diff --git a/src/se/umu/cs/dit06ajnajs/ATDModel.java b/src/se/umu/cs/dit06ajnajs/ATDModel.java index 5174c68..3f27c03 100644 --- a/src/se/umu/cs/dit06ajnajs/ATDModel.java +++ b/src/se/umu/cs/dit06ajnajs/ATDModel.java @@ -9,6 +9,7 @@ import java.util.Observer; import se.umu.cs.dit06ajnajs.agent.Agent; import se.umu.cs.dit06ajnajs.agent.Tower; +import se.umu.cs.dit06ajnajs.map.GoalSquare; import se.umu.cs.dit06ajnajs.map.Map; import se.umu.cs.dit06ajnajs.map.MapBuilder; import se.umu.cs.dit06ajnajs.map.MapSquare; @@ -77,4 +78,8 @@ public class ATDModel { public Dimension getMapDimension() { return currentMap.getDimension(); } + + public GoalSquare[] getGoalSquares() { + return this.currentMap.getGoalSquares(); + } } diff --git a/src/se/umu/cs/dit06ajnajs/agent/Unit.java b/src/se/umu/cs/dit06ajnajs/agent/Unit.java index a6fe234..c91d4b5 100644 --- a/src/se/umu/cs/dit06ajnajs/agent/Unit.java +++ b/src/se/umu/cs/dit06ajnajs/agent/Unit.java @@ -6,9 +6,11 @@ import java.util.logging.Logger; import se.umu.cs.dit06ajnajs.AntiTD; import se.umu.cs.dit06ajnajs.Paintable; +import se.umu.cs.dit06ajnajs.map.GoalSquare; import se.umu.cs.dit06ajnajs.map.Map; import se.umu.cs.dit06ajnajs.map.MapSquare; import se.umu.cs.dit06ajnajs.map.PathSquare; +import se.umu.cs.dit06ajnajs.map.Traversable; public abstract class Unit implements Agent, Paintable{ private static Logger logger = Logger.getLogger("AntiTD"); @@ -40,37 +42,43 @@ public abstract class Unit implements Agent, Paintable{ } public void act() { + Point nextPos = getNextPosition(); // TODO check for collision on next position - move(); + move(nextPos); // Land on current square MapSquare currentSquare= map.getMapSquareAtPoint(xPos, yPos); - if (currentSquare instanceof PathSquare) { - ((PathSquare) currentSquare).landOn(this); + if (currentSquare instanceof Traversable) { + ((Traversable) currentSquare).landOn(this); } } - public void move() { - //TODO invoke landOn(this) on the square the unit lands on - // Nu måste unit fråga map vilken ruta som finns på detta x,y? + public Point getNextPosition() { + Point nextPos = null; switch (direction) { case UP: logger.fine("UP"); - yPos -= speed; + nextPos = new Point(xPos, yPos - speed); break; case DOWN: logger.fine("DOWN"); - yPos += speed; + nextPos = new Point(xPos, yPos + speed); break; case LEFT: logger.fine("LEFT"); - xPos -= speed; + nextPos = new Point(xPos - speed, yPos); break; case RIGHT: logger.fine("RIGHT"); - xPos += speed; + nextPos = new Point(xPos + speed, yPos); break; } + return nextPos; + } + + public void move(Point p) { + this.xPos = p.x; + this.yPos = p.y; } public int getXPos() { diff --git a/src/se/umu/cs/dit06ajnajs/map/Map.java b/src/se/umu/cs/dit06ajnajs/map/Map.java index c60a09b..cec6927 100644 --- a/src/se/umu/cs/dit06ajnajs/map/Map.java +++ b/src/se/umu/cs/dit06ajnajs/map/Map.java @@ -19,6 +19,7 @@ public class Map { private int numCol; private int numRow; private MapSquare[][] squareMatrix; + private GoalSquare[] goalSquares; public Map(String name, MapSquare[][] squareMatrix) { this.name = name; @@ -37,12 +38,14 @@ public class Map { // Set width and height for map this.width = squareSize * numCol; this.height = squareSize * numRow; + + this.goalSquares = extractGoalSquares(); } public MapSquare getMapSquareAtPoint(int x, int y) { - return getMapSquareAtPoint(new Point(x, y)); + return getMapSquareAtPoint(new Point(x, y)); } - + public MapSquare getMapSquareAtPoint(Point point) { //TODO testa algoritmen int x = point.x; @@ -65,9 +68,9 @@ public class Map { Graphics g = backgroundImage.getGraphics(); for (MapSquare[] row : squareMatrix) { - for (MapSquare square : row) { - square.paint(g); - } + for (MapSquare square : row) { + square.paint(g); + } } return backgroundImage; } @@ -75,11 +78,25 @@ public class Map { public Dimension getDimension() { return new Dimension(width, height); } + + private GoalSquare[] extractGoalSquares() { + List squares = new ArrayList(); + for (MapSquare[] row : squareMatrix) { + for (MapSquare square : row) { + if (square instanceof GoalSquare) { + squares.add((GoalSquare) square); + } + } + } + GoalSquare[] goalSquares = squares.toArray(new GoalSquare[squares.size()]); + //arr = list.toArray(new MyBean[list.size()]); + return goalSquares; + } public TowerSquare getRandomFreeTowerSquare() { - List squares = getTowerSquares(); + List squares = extractTowerSquares(); List freeSquares = new ArrayList(); - + for (TowerSquare square : squares) { if (square.isAvailable()) { freeSquares.add(square); @@ -92,8 +109,8 @@ public class Map { int index = (int) (freeSquares.size()*Math.random()); return freeSquares.get(index); } - - public List getTowerSquares() { + + public List extractTowerSquares() { // TODO What should happen if there are no towersquares? List squares = new ArrayList(); for (MapSquare[] row : squareMatrix) { @@ -105,28 +122,32 @@ public class Map { } return squares; } - + // TODO: test implementation. public List getNeighbours(MapSquare square, int range) { - List neighbours = new ArrayList(); - int xPos = square.getX(); - int yPos = square.getY(); - - int scanWidth = range * 2 + 1; - + List neighbours = new ArrayList(); + int xPos = square.getX(); + int yPos = square.getY(); + + int scanWidth = range * 2 + 1; + int colLeft = (xPos / AntiTD.SQUARE_SIZE) - range; int rowTop = (yPos / AntiTD.SQUARE_SIZE) - range; - + for (int tmpRow = rowTop; tmpRow < scanWidth; tmpRow++) { - for (int tmpCol = colLeft; tmpCol < scanWidth; tmpCol++) { - if (tmpCol < 0 || tmpCol > numCol - || tmpRow < 0 || tmpRow > numRow) { - neighbours.add(squareMatrix[tmpCol][tmpRow]); - } - } + for (int tmpCol = colLeft; tmpCol < scanWidth; tmpCol++) { + if (tmpCol < 0 || tmpCol > numCol + || tmpRow < 0 || tmpRow > numRow) { + neighbours.add(squareMatrix[tmpCol][tmpRow]); + } + } } return neighbours; } + + public GoalSquare[] getGoalSquares() { + return this.goalSquares; + } /** * Gets the name of this Map. diff --git a/src/se/umu/cs/dit06ajnajs/map/MapSquarePrototypeFactory.java b/src/se/umu/cs/dit06ajnajs/map/MapSquarePrototypeFactory.java index dc20ecc..263b2f3 100644 --- a/src/se/umu/cs/dit06ajnajs/map/MapSquarePrototypeFactory.java +++ b/src/se/umu/cs/dit06ajnajs/map/MapSquarePrototypeFactory.java @@ -57,6 +57,11 @@ public class MapSquarePrototypeFactory { // Create BlockedSquare Prototype url = this.getClass().getResource("/resources/effielTower.gif"); image = ImageIO.read(url); + squareMap.put("GoalSquare", new GoalSquare(-1, -1, image)); + + // Create BlockedSquare Prototype + url = this.getClass().getResource("/resources/effielTower.gif"); + image = ImageIO.read(url); squareMap.put("BlockedSquare", new BlockedSquare(-1, -1, image)); } catch (IOException e) { System.err.println("Couldn't find image. Exception: " diff --git a/src/se/umu/cs/dit06ajnajs/map/PathSquare.java b/src/se/umu/cs/dit06ajnajs/map/PathSquare.java index fb1f2c0..3de7e95 100644 --- a/src/se/umu/cs/dit06ajnajs/map/PathSquare.java +++ b/src/se/umu/cs/dit06ajnajs/map/PathSquare.java @@ -16,7 +16,7 @@ public class PathSquare extends MapSquare implements Traversable { public void landOn(Unit unit) { // DONE: Woha a unit is here, I should tell all my towers! - logger.info("Unit >" + unit.getClass().getName() + + logger.info("Unit >" + unit.getClass().getSimpleName() + "< landed on pathsquare. Notifying observers..."); setChanged(); notifyObservers(unit); -- 2.11.4.GIT