-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTower.java
executable file
·139 lines (126 loc) · 3.13 KB
/
Tower.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
/**
* Tower Class. Specifies the things a tower can do.
*
* @author haolidu
* @version 1.00
*/
public abstract class Tower {
private int attackPower;
private int attackRadius;
private int x, y;
protected int simulAttack;
protected int attacked;
protected int cost;
protected BufferedImage icon;
protected Circle circle;
/**
* Tower constructor.
*
* @param x
* int position in x-axis
* @param y
* int position in y-axis
*/
public Tower(int x, int y) {
this.x = x;
this.y = y;
attacked = 0;
}
/**
* Gets the attack power
*
* @return int attack power
*/
public int getAttackPower() {
return attackPower;
}
/**
* Returns the string representation of the tower.
*
* @return String.
*/
public abstract String toString();
/**
* Gets the attack radius of the tower.
*
* @return attackRadius.
*/
public int getAttackRadius() {
return attackRadius;
}
/**
* Reset the number of attacked by the tower.
*
* @return none
*/
protected void resetAttacked() {
attacked = 0;
}
/**
* Sets the attack power of the tower.
*
* @param attackPower
* @return none
*/
protected void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
/**
* Sets the attack radius of the tower
*
* @param attackRadius
* @return none
*/
protected void setAttackRadius(int attackRadius) {
this.attackRadius = attackRadius;
}
/**
* Checks if the tower is intersecting with the given enemy
*
* @param enemy
* @return true if this tower is intersecting the enemy.
*/
public boolean isIntersecting(Enemy enemy) {
ArrayList<Line2D.Double> lines = enemy.getAllLines();
for (Line2D line : lines) {
if (circle.intersects(line) && attacked < simulAttack) {
attacked++;
return true;
}
}
return false;
}
/**
* Draws the tower and its attack radius
*
* @param g
* Graphics object.
*/
public void draw(Graphics g) {
if (icon != null) {
// Paints the circle
Graphics2D g2D = (Graphics2D) g;
Color color = new Color(119, 136, 153, 50);
g2D.setColor(color);
g2D.fill(circle);
// Paints rim
color = new Color(49, 79, 79, 60);
g2D.setColor(color);
g2D.setStroke(new BasicStroke(5));
Circle c = new Circle(circle.x, circle.y, circle.width + 1);
g2D.draw(c);
// Paints the icon.
g2D.drawImage(icon, x - icon.getWidth()
/ 2, y - icon.getHeight() / 2, null);
} else {
System.exit(0);
}
}
}