From 36a31ce990779f46767b554a76f5061376541943 Mon Sep 17 00:00:00 2001 From: Andreas Jakobsson Date: Fri, 9 Jan 2009 22:41:14 +0100 Subject: [PATCH] forgot to add Level.java --- src/se/umu/cs/dit06ajnajs/map/Level.java | 372 +++++++++++++++++++++++++++++++ 1 file changed, 372 insertions(+) create mode 100644 src/se/umu/cs/dit06ajnajs/map/Level.java diff --git a/src/se/umu/cs/dit06ajnajs/map/Level.java b/src/se/umu/cs/dit06ajnajs/map/Level.java new file mode 100644 index 0000000..4e131ce --- /dev/null +++ b/src/se/umu/cs/dit06ajnajs/map/Level.java @@ -0,0 +1,372 @@ +package se.umu.cs.dit06ajnajs.map; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Point; +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +import se.umu.cs.dit06ajnajs.AntiTD; +import se.umu.cs.dit06ajnajs.NoActiveStartSquareException; +import se.umu.cs.dit06ajnajs.agent.Direction; +import java.util.Collection; +import se.umu.cs.dit06ajnajs.agent.Tower; + +/** + * Describe class Level here. + * + * @author Anton Johansson, dit06ajn@cs.umu.se + * Andreas Jacobsson, dit06ajs@cs.umu.se + * @version 1.0 + */ +public class Level { + private static Logger logger = Logger.getLogger("AntiTD"); + + private String name; + private int goalSum; + private int currentSum; + private int squareSize; + private int width; + private int height; + private int numCol; + private int numRow; + private MapSquare[][] squareMatrix; + private GoalSquare[] goalSquares; + private StartSquare[] startSquares; + private Collection towers; + + public Level(String name, MapSquare[][] squareMatrix) { + this(name, squareMatrix, null); + } + + + public Level(String name, MapSquare[][] squareMatrix, Collection towers) { + this.name = name; + this.squareSize = AntiTD.SQUARE_SIZE; + this.squareMatrix = squareMatrix; + this.goalSum = 5; //TODO this should be set from XML-document + this.currentSum = 0; + try { + this.numCol = squareMatrix.length; + this.numRow = squareMatrix[0].length; + } catch(NullPointerException e) { + //TODO Felmeddelande + e.printStackTrace(); + return; + } + + // Add towers to TowerSquares + if (towers != null) { + for (Tower tower : towers) { + addTower(tower); + } + this.towers = towers; + } + + // Set width and height for map + this.width = squareSize * numCol; + this.height = squareSize * numRow; + + this.goalSquares = extractGoalSquares(); + this.startSquares = extractStartSquares(); + // Make all startSquares observe each other + for (StartSquare s1 : startSquares) { + for (StartSquare s2 : startSquares) { + if (s1 != s2) { + logger.info("StartSquare observable: " + s1 + + ", is observed by " + s2); + s2.addObserver(s1); + } + } + } + + initTurnSquares(); + } + + public MapSquare getMapSquareAtPoint(int x, int y) { + return getMapSquareAtPoint(new Point(x, y)); + } + + public MapSquare getMapSquareAtPoint(Point point) { + //TODO testa algoritmen + int x = point.x; + int y = point.y; + + if (x > width || y > height) { + throw new IllegalArgumentException("Position is " + + "outside of map bounds."); + } + + int col = x / AntiTD.SQUARE_SIZE; + int row = y / AntiTD.SQUARE_SIZE; + + return squareMatrix[col][row]; + } + + public void setMapSquareAtPoint(int x, int y, MapSquare mapSquare) { + setMapSquareAtPoint(new Point(x, y), mapSquare); + } + + public void setMapSquareAtPoint(Point point, MapSquare mapSquare) { + int x = point.x; + int y = point.y; + + if (x > width || y > height) { + throw new IllegalArgumentException("Position is " + + "outside of map bounds."); + } + + int col = x / AntiTD.SQUARE_SIZE; + int row = y / AntiTD.SQUARE_SIZE; + + squareMatrix[col][row] = mapSquare; + } + + public Image getMapImage() { + Image backgroundImage = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + Graphics g = backgroundImage.getGraphics(); + + for (MapSquare[] row : squareMatrix) { + for (MapSquare square : row) { + square.paint(g); + } + } + return backgroundImage; + } + + 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; + } + + /** + * Extract all StartSquares from this Level. + * + * @return All StartSquares in this Level. + */ + private StartSquare[] extractStartSquares() { + List squares = new ArrayList(); + for (MapSquare[] row : squareMatrix) { + for (MapSquare square : row) { + if (square instanceof StartSquare) { + squares.add((StartSquare) square); + } + } + } + StartSquare[] startSquares = squares.toArray(new StartSquare[squares.size()]); + //arr = list.toArray(new MyBean[list.size()]); + return startSquares; + } + + private TowerSquare getRandomFreeTowerSquare() { + List squares = extractTowerSquares(); + List freeSquares = new ArrayList(); + + for (TowerSquare square : squares) { + if (square.isAvailable()) { + freeSquares.add(square); + } + } + if (freeSquares.isEmpty()) { + // TODO What should happen if there are no free towersquares? + return null; + } + int index = (int) (freeSquares.size()*Math.random()); + return freeSquares.get(index); + } + + public List extractTowerSquares() { + // TODO What should happen if there are no towersquares? + List squares = new ArrayList(); + for (MapSquare[] row : squareMatrix) { + for (MapSquare square : row) { + if (square instanceof TowerSquare) { + squares.add((TowerSquare) square); + } + } + } + return squares; + } + + /** + * Extract all MapSquare of type TurnSquare from this Level. + * + * @return All TurnSquares in this Level. + */ + public List extractTurnSquares() { + // TODO What should happen if there are no TurnSquares? + List squares = new ArrayList(); + for (MapSquare[] row : squareMatrix) { + for (MapSquare square : row) { + if (square instanceof TurnSquare) { + squares.add((TurnSquare) square); + } + } + } + return squares; + } + + // TODO: test implementation. + /** + * @param square + * @param range Should an int describing how many MapSquares away from + * specified MapSquares to return. + * @return + */ + public List getNeighbours(MapSquare square, int range) { + List neighbours = new ArrayList(); + int col = square.getX() / AntiTD.SQUARE_SIZE; + int row = square.getY() / AntiTD.SQUARE_SIZE; + + int scanWidth = range * 2 + 1; + + int colLeft = col - range; + int rowTop = row - range; + + logger.info("Range: " + range + ", colLeft: " + colLeft + + ", rowTop: " + rowTop + ", scanWidth: " + scanWidth); + + // TODO: square is added as neighbours to itself. + + for (int tmpRow = rowTop; + tmpRow < scanWidth + rowTop; + tmpRow++) { + + for (int tmpCol = colLeft; + tmpCol < scanWidth + colLeft; + tmpCol++) { + + if (tmpCol >= 0 && tmpCol < numCol + && tmpRow >= 0 && tmpRow < numRow + && (tmpRow != row || tmpCol != col)) { + logger.info("Adding neigbour for: " + square + "\n" + + " At col: " + tmpCol + ", row: " + tmpRow); + neighbours.add(squareMatrix[tmpCol][tmpRow]); + } + } + } + logger.info(neighbours.size() + " neighbours found."); + return neighbours; + } + + private void initTurnSquares() { + // TODO: Write tests + for (TurnSquare turnSquare : extractTurnSquares()) { + int col = turnSquare.getX() / AntiTD.SQUARE_SIZE; + int row = turnSquare.getY() / AntiTD.SQUARE_SIZE; + if (row - 1 >= 0 + && squareMatrix[col][row - 1] instanceof PathSquare) { + turnSquare.addDirection(Direction.UP); + } + if (col + 1 < numCol + && squareMatrix[col + 1][row] instanceof PathSquare) { + turnSquare.addDirection(Direction.RIGHT); + } + if (row + 1 < numRow + && squareMatrix[col][row + 1] instanceof PathSquare) { + turnSquare.addDirection(Direction.DOWN); + } + if (col - 1 >= 0 + && squareMatrix[col - 1][row] instanceof PathSquare) { + turnSquare.addDirection(Direction.LEFT); + } + } + } + + public GoalSquare[] getGoalSquares() { + return this.goalSquares; + } + + public StartSquare[] getStartSquares() { + return this.startSquares; + } + + /** + * Gets the name of this Level. + * + * @return The name of this Level. + */ + public String getName() { + return this.name; + } + + /** + * Returns an Array representation of this map. + * + * @return An Array representation of this map. + */ + public MapSquare[][] toArray() { + return squareMatrix; + } + + public StartSquare getActiveStartSquare() { + for (StartSquare square : startSquares) { + if (square.isActive()) { + return square; + } + } + throw new NoActiveStartSquareException(); + } + + public void addTower(Tower t) { + TowerSquare square = getRandomFreeTowerSquare(); + if (square != null) { + System.out.println("Adding tower!"); + System.out.println("Free TowerSquare has point @ (" + square.getX() + ", " + square.getY() + ")"); + System.out.println("Free TowerSquare has centerpoint @ (" + square.getCenterX() + ", " + square.getCenterY() + ")"); + // If there is a free square, place tower in the center of square + Point tPos = new Point(square.getCenterX() - (t.getWidth()/2) + , square.getCenterY() - (t.getHeight()/2)); + t.setPostition(tPos); + square.setTower(t); + + System.out.println("Tower placed @ (" + tPos.x + ", " + tPos.y + ")"); + + //Register as observer for the neighbours in range + int shootRange = t.getRange(); + int squareRange = + (int) Math.ceil((shootRange - AntiTD.SQUARE_SIZE*0.5) + / (AntiTD.SQUARE_SIZE)); + + List neighbours + = getNeighbours(square, squareRange); + logger.info("There are >" + neighbours.size() + + "< neighbours in range >" + t.getRange() + "< pixels"); + + for (MapSquare neighbour: neighbours) { + if (neighbour instanceof PathSquare) { + logger.info("Adding tower-observer: " + t + + " to: " + neighbour); + neighbour.addObserver(t); + } + } + } else { + throw new IllegalArgumentException("No available towersquares"); + } + } + + public Collection getTowers() { + return this.towers; + } + + public int getCurrentSum() { + return this.currentSum; + } +} -- 2.11.4.GIT