sound for units onClick
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Tower.java
blobd486c2b97907f4b3c0440f93ad83f9d1ad0c69df
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.Image;
4 import java.awt.Point;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Observable;
8 import java.util.Observer;
9 import java.util.logging.Logger;
11 import se.umu.cs.dit06ajnajs.Paintable;
12 import java.awt.Color;
13 import java.awt.Graphics;
15 public abstract class Tower implements Paintable, Agent, Observer, Cloneable {
17 private static Logger logger = Logger.getLogger("AntiTD");
19 private int x;
20 private int y;
21 private int centerX;
22 private int centerY;
24 private int width;
25 private int height;
27 private int damage;
28 private int range;
29 private Image image;
30 private boolean alive;
31 private boolean fiering;
32 private List<Unit> fireingList;
35 public Tower(int width, int height, int damage, int range, Image image) {
36 this.width = width;
37 this.height = height;
38 this.damage = damage;
39 this.range = range;
40 this.image = image;
42 this.alive = true;
43 this.fiering = false;
46 /**
47 * Used to initialize fields that should not be individually set for every
48 * Tower, since clone() is shallow.
50 public void init() {
51 this.fireingList = new ArrayList<Unit>();
54 public void setImage(Image img) {
55 // TODO Implement method
58 public void setPostition(Point p) {
59 this.x = p.x;
60 this.y = p.y;
61 this.centerX = this.x + (this.width/2);
62 this.centerY = this.y + (this.height/2);
65 public int getRange() {
66 return this.range;
69 public Point getCenterPoint() {
70 return new Point(this.centerX, this.centerY);
73 public void act() {
74 if (!fireingList.isEmpty()) {
75 Unit unit = fireingList.get(0);
76 if(unit.isAlive()) {
77 Point unitPoint = unit.getCenterPoint();
79 int unitXPos = unitPoint.x;
80 int unitYPos = unitPoint.y;
81 int distans = (int) Math.hypot((this.centerX - unitXPos), this.centerY - unitYPos);
82 System.out.println("Tower range: " + this.range);
83 System.out.println("Distance to unit: " + distans);
84 if (distans<this.range) {
85 logger.info("UNIT IN RANGE, FIIIIIREEEEEEE!!!");
86 this.fiering = true;
87 unit.damage(damage);
88 } else {
89 logger.info("Unit out of reach, removing from fireingList");
90 fireingList.remove(unit);
92 } else {
93 fireingList.remove(unit);
98 public void update(Observable caller, Object arg) {
99 // TODO Auto-generated method stub
100 // Should always be units
101 if (arg instanceof Unit) {
102 Unit unit = (Unit) arg;
103 if (!fireingList.contains(unit)) {
104 fireingList.add(unit);
105 logger.info("Unit >" + unit.getClass().getSimpleName() +
106 "< added to tower...");
111 public boolean isAlive() {
112 return this.alive;
115 public void setAlive(boolean state) {
116 this.alive = state;
119 public int getHeight() {
120 return height;
123 public void setHeight(int height) {
124 this.height = height;
127 public int getWidth() {
128 return width;
131 public void setWidth(int width) {
132 this.width = width;
135 public int getX() {
136 return this.x;
139 public int getY() {
140 return this.y;
143 public int getCenterX() {
144 return centerX;
147 public int getCenterY() {
148 return centerY;
151 public boolean isFiering() {
152 return this.fiering;
155 public void setFiering(boolean fiering) {
156 this.fiering = fiering;
159 public List<Unit> getFireingList() {
160 return this.fireingList;
163 public void paint(Graphics g) {
164 // TODO cast to graphics2D?
165 // g.setColor(Color.RED);
166 // g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
167 g.drawImage(image, x, y, null);
169 // TODO Tillfällig implementering
170 if (this.isFiering()) {
171 this.setFiering(false);
172 g.setColor(Color.WHITE);
173 List<Unit> fireingList = this.getFireingList();
174 if (!fireingList.isEmpty()) {
175 Unit unit = this.getFireingList().get(0);
176 g.drawLine(this.getCenterX(), this.getCenterY(),
177 unit.getCenterPoint().x, unit.getCenterPoint().y);
183 * Empty method to handle mouseClicks, override in subclass to implement
184 * behaviour.
187 public void click() {
188 // Overried in subclass
192 * Attemts to clone this Tower.
194 * @return A new instance of the same type as the instantiated Tower.
196 @Override
197 public Object clone() {
198 try {
199 return super.clone();
200 } catch (CloneNotSupportedException e) {
201 e.printStackTrace();
202 throw new Error("Object " + this.getClass().getName()
203 + " is not Cloneable");