From 28026a43ee78a2e0c6a02585acde634c193d31b8 Mon Sep 17 00:00:00 2001 From: Andreas Jakobsson Date: Mon, 15 Dec 2008 17:23:24 +0100 Subject: [PATCH] addTower-method --- src/se/umu/cs/dit06ajnajs/ATDController.java | 8 +++- src/se/umu/cs/dit06ajnajs/ATDModel.java | 11 ++++++ src/se/umu/cs/dit06ajnajs/Map.java | 50 ++++++++++++++++++++----- src/se/umu/cs/dit06ajnajs/Player.java | 10 ++++- src/se/umu/cs/dit06ajnajs/TowerSquare.java | 7 ++++ src/se/umu/cs/dit06ajnajs/agent/BasicTower.java | 12 ++++-- src/se/umu/cs/dit06ajnajs/agent/Tower.java | 8 +++- 7 files changed, 89 insertions(+), 17 deletions(-) diff --git a/src/se/umu/cs/dit06ajnajs/ATDController.java b/src/se/umu/cs/dit06ajnajs/ATDController.java index 906fbab..777b563 100644 --- a/src/se/umu/cs/dit06ajnajs/ATDController.java +++ b/src/se/umu/cs/dit06ajnajs/ATDController.java @@ -4,6 +4,7 @@ import java.awt.Graphics; import java.util.List; 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; @@ -24,7 +25,11 @@ public class ATDController { model.addAgent(new FootmanUnit(100,200, 20, 20, 1, Direction.UP)); model.addAgent(new FootmanUnit(140,200, 20, 20, 2, Direction.UP)); - model.addAgent(new FootmanUnit(-20,100, 20, 20, 1, Direction.RIGHT)); + model.addAgent(new FootmanUnit(-20,50, 20, 20, 1, Direction.RIGHT)); + model.addAgent(new FootmanUnit(-20,100, 20, 20, 4, Direction.RIGHT)); + model.addAgent(new FootmanUnit(-20,150, 20, 20, 2, Direction.RIGHT)); + + model.addTower(new BasicTower(10, 1)); initGame(); running = true; @@ -37,6 +42,7 @@ public class ATDController { } + private class AnimationThread implements Runnable { diff --git a/src/se/umu/cs/dit06ajnajs/ATDModel.java b/src/se/umu/cs/dit06ajnajs/ATDModel.java index fd3fa28..968cf19 100644 --- a/src/se/umu/cs/dit06ajnajs/ATDModel.java +++ b/src/se/umu/cs/dit06ajnajs/ATDModel.java @@ -6,6 +6,7 @@ import java.awt.Image; import java.util.ArrayList; import java.util.List; import se.umu.cs.dit06ajnajs.agent.Agent; +import se.umu.cs.dit06ajnajs.agent.Tower; public class ATDModel { @@ -17,6 +18,16 @@ public class ATDModel { this.agents = new ArrayList(); this.map = MapBuilder.createMap(); } + + public void addTower(Tower t) { + TowerSquare square = map.getRandomFreeTowerSquare(); + if (square != null) { + square.setTower(t); + agents.add(t); + } else { + System.out.println("No available towersquares"); + } + } public void addAgent(Agent agent) { // TODO Auto-generated method stub diff --git a/src/se/umu/cs/dit06ajnajs/Map.java b/src/se/umu/cs/dit06ajnajs/Map.java index 6ef49be..ad432aa 100644 --- a/src/se/umu/cs/dit06ajnajs/Map.java +++ b/src/se/umu/cs/dit06ajnajs/Map.java @@ -5,6 +5,8 @@ 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; public class Map { @@ -27,14 +29,14 @@ public class Map { e.printStackTrace(); return; } - + // Set width and height for map this.width = squareSize * numCol; this.height = squareSize * numRow; } // TODO Gör metod för att få ut bakgrundsbild. I denna klass eller någon annanstns - + public MapSquare getMapSquareAtPos(Point point) { //TODO testa algoritmen int x = point.x; @@ -55,16 +57,46 @@ public class Map { 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); - } + for (MapSquare square : row) { + square.paint(g); + } } return backgroundImage; } - public Dimension getDimension() { - return new Dimension(width, height); - } + public Dimension getDimension() { + return new Dimension(width, height); + } + + public TowerSquare getRandomFreeTowerSquare() { + List squares = getTowerSquares(); + 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 getTowerSquares() { + // 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; + } } diff --git a/src/se/umu/cs/dit06ajnajs/Player.java b/src/se/umu/cs/dit06ajnajs/Player.java index 18d16b0..e2880dc 100644 --- a/src/se/umu/cs/dit06ajnajs/Player.java +++ b/src/se/umu/cs/dit06ajnajs/Player.java @@ -1,6 +1,12 @@ package se.umu.cs.dit06ajnajs; -public class Player { - public Player() { +public class Player { + + private String name; + private int score; + + public Player(String name) { + this.name = name; + score = 0; } } diff --git a/src/se/umu/cs/dit06ajnajs/TowerSquare.java b/src/se/umu/cs/dit06ajnajs/TowerSquare.java index 876bd33..72eaa13 100644 --- a/src/se/umu/cs/dit06ajnajs/TowerSquare.java +++ b/src/se/umu/cs/dit06ajnajs/TowerSquare.java @@ -6,16 +6,23 @@ import se.umu.cs.dit06ajnajs.agent.Tower; public class TowerSquare extends MapSquare { private Tower tower; + private boolean available; public TowerSquare(int x, int y, Image img) { super(x, y, img); + this.available = true; } public void setTower(Tower tower) { this.tower = tower; + available = false; } public Tower getTower() { return this.tower; } + + public boolean isAvailable() { + return this.available; + } } \ No newline at end of file diff --git a/src/se/umu/cs/dit06ajnajs/agent/BasicTower.java b/src/se/umu/cs/dit06ajnajs/agent/BasicTower.java index 3e36ebd..89a08c7 100644 --- a/src/se/umu/cs/dit06ajnajs/agent/BasicTower.java +++ b/src/se/umu/cs/dit06ajnajs/agent/BasicTower.java @@ -6,9 +6,13 @@ import se.umu.cs.dit06ajnajs.Paintable; public class BasicTower extends Tower implements Paintable { - public BasicTower(int damage, int range, Image img) { - super.damage = damage; - super.range = range; - super.image = img; + public BasicTower(int damage, int range) { + super(damage, range); + // TODO super.image = img; + } + + public void update() { + // TODO Auto-generated method stub + } } diff --git a/src/se/umu/cs/dit06ajnajs/agent/Tower.java b/src/se/umu/cs/dit06ajnajs/agent/Tower.java index 68ecda4..ea48a4d 100644 --- a/src/se/umu/cs/dit06ajnajs/agent/Tower.java +++ b/src/se/umu/cs/dit06ajnajs/agent/Tower.java @@ -5,12 +5,18 @@ import java.awt.Image; import se.umu.cs.dit06ajnajs.Paintable; -public abstract class Tower implements Paintable { +public abstract class Tower implements Paintable, Agent { protected int damage; protected int range; protected Image image; + public Tower(int damage, int range) { + this.damage = damage; + this.range = range; + // TODO this.image = img; + } + public void paint(Graphics g) { // TODO Auto-generated method stub // TODO Implement method -- 2.11.4.GIT