StartSquare selection, distanceCalc changed in TowerSquare, new level with two StartS...
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Tower.java
blobb567c573fc4166536c41e96efd53a287c802bf18
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.Color;
4 import java.awt.Image;
5 import java.awt.Point;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Observable;
9 import java.util.Observer;
10 import java.util.logging.Logger;
12 import se.umu.cs.dit06ajnajs.Paintable;
14 public abstract class Tower implements Paintable, Agent, Observer {
16 private static Logger logger = Logger.getLogger("AntiTD");
18 private int x;
19 private int y;
20 private int centerX;
21 private int centerY;
23 private int width;
24 private int height;
26 private int damage;
27 private int range;
28 private Image image;
29 private boolean alive;
30 private boolean fiering;
31 private List<Unit> fireingList;
34 public Tower(int width, int height, int damage, int range) {
35 this.width = width;
36 this.height = height;
39 this.damage = damage;
40 this.range = range;
41 this.alive = true;
42 this.fiering = false;
44 this.fireingList = new ArrayList<Unit>();
45 // TODO this.image = img;
48 public void setImage(Image img) {
49 // TODO Implement method
52 public void setPostition(Point p) {
53 this.x = p.x;
54 this.y = p.y;
55 this.centerX = this.x + (this.width/2);
56 this.centerY = this.y + (this.height/2);
59 public int getRange() {
60 return this.range;
63 public Point getCenterPoint() {
64 return new Point(this.centerX, this.centerY);
67 public void act() {
68 if (!fireingList.isEmpty()) {
69 Unit unit = fireingList.get(0);
70 if(unit.isAlive()) {
71 Point unitPoint = unit.getCenterPoint();
73 int unitXPos = unitPoint.x;
74 int unitYPos = unitPoint.y;
75 int distans = (int) Math.hypot((this.centerX - unitXPos), this.centerY - unitYPos);
76 System.out.println("Tower range: " + this.range);
77 System.out.println("Distance to unit: " + distans);
78 if (distans<this.range) {
79 logger.info("UNIT IN RANGE, FIIIIIREEEEEEE!!!");
80 this.fiering = true;
81 unit.damage(damage);
82 } else {
83 logger.info("Unit out of reach, removing from fireingList");
84 fireingList.remove(unit);
86 } else {
87 fireingList.remove(unit);
92 public void update(Observable caller, Object arg) {
93 // TODO Auto-generated method stub
94 // Should always be units
95 if (arg instanceof Unit) {
96 Unit unit = (Unit) arg;
97 if (!fireingList.contains(unit)) {
98 fireingList.add(unit);
99 logger.info("Unit >" + unit.getClass().getSimpleName() +
100 "< added to tower...");
105 public boolean isAlive() {
106 return this.alive;
109 public void setAlive(boolean state) {
110 this.alive = state;
113 public int getHeight() {
114 return height;
117 public void setHeight(int height) {
118 this.height = height;
121 public int getWidth() {
122 return width;
125 public void setWidth(int width) {
126 this.width = width;
129 public int getX() {
130 return this.x;
133 public int getY() {
134 return this.y;
137 public int getCenterX() {
138 return centerX;
141 public int getCenterY() {
142 return centerY;
145 public boolean isFiering() {
146 return this.fiering;
149 public void setFiering(boolean fiering) {
150 this.fiering = fiering;
153 public List<Unit> getFireingList() {
154 return this.fireingList;