-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
206 lines (170 loc) · 5.65 KB
/
Tile.java
File metadata and controls
206 lines (170 loc) · 5.65 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class Tile extends JPanel implements Cloneable, Comparable {
public static int TILEWIDTH = 64, TILEHEIGHT = 64;
public BufferedImage tileImage;
public Tile pathParent;
public float costFromStart;
/**
* Tile Klasse - Basis der Maps
* -> hat verschiedene Attribute (blocked,danger) auf die vom Mover reagiert werden können
* -> besitzt eine ID zur Identifikation (standardmäßig die Tilenummer im TileSet)
* -> besitzt ein Image (Grafik)
*/
protected boolean blocked = false;
protected boolean danger = false;
protected LinkedList neighbours = new LinkedList();
protected boolean showImage = true;
protected int id;
protected float estimatedCostToGoal;
protected Insets borderInsets;
protected boolean fill;
protected Color color;
public Tile(BufferedImage pTileImage) {
super();
setOpaque(true);
tileImage = pTileImage;
borderInsets = new Insets(0, 0, 0, 0);
}
public static void setTILEWIDTH(int TILEWIDTH) {
Tile.TILEWIDTH = TILEWIDTH;
}
public static void setTILEHEIGHT(int TILEHEIGHT) {
Tile.TILEHEIGHT = TILEHEIGHT;
}
/**
* rendert das Tile Bild und den Border
*/
public void renderTile(Graphics2D g2d, int pXPos, int pYPos) {
this.setLocation(pXPos, pYPos);
if (showImage) {
g2d.drawImage(tileImage, this.getX(), this.getY(), TILEWIDTH, TILEHEIGHT, null);
}
paintBorder(g2d);
}
private void paintBorder(Graphics2D g2d) {
g2d.setColor(color);
if (borderInsets.top > 0) {
int stroke = borderInsets.top;
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(getX() + stroke / 2, getY() + stroke / 2, getX() + TILEWIDTH - stroke / 2, getY() + stroke / 2);
}
if (borderInsets.right > 0) {
int stroke = borderInsets.right;
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(getX() + TILEWIDTH - stroke / 2, getY() + stroke / 2, getX() + TILEWIDTH - stroke / 2, getY() + TILEHEIGHT - stroke / 2);
}
if (borderInsets.bottom > 0) {
int stroke = borderInsets.bottom;
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(getX() + stroke / 2, getY() + TILEHEIGHT - stroke / 2, getX() + TILEWIDTH - stroke / 2, getY() + TILEHEIGHT - stroke / 2);
}
if (borderInsets.left > 0) {
int stroke = borderInsets.left;
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(getX() + stroke / 2, getY() + stroke / 2, getX() + stroke / 2, getY() + TILEHEIGHT - stroke / 2);
}
if (fill) {
g2d.fillRect(getX(), getY(), Tile.TILEWIDTH, Tile.TILEHEIGHT);
}
}
public Point getTileLocation() {
return new Point((getX() / TILEWIDTH) - 1, (getY() / TILEHEIGHT) - 1);
}
public void setFill(boolean fill) {
this.fill = fill;
}
public void showImage(boolean b) {
showImage = b;
}
public boolean isDanger() {
return danger;
}
public void setDanger(boolean danger) {
this.danger = danger;
if (danger) {
setColor(Color.red);
}
}
public boolean isBlocked() {
return blocked;
}
public void setBlocked(boolean b) {
blocked = b;
}
public Tile clone() {
try {
return (Tile) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
public Insets getBorderInsets() {
return borderInsets;
}
public void setBorderInsets(Insets insets) {
borderInsets = insets;
}
public LinkedList getNeighbours() {
return neighbours;
}
public void setNeighbours(LinkedList pNeighbours) {
neighbours = pNeighbours;
}
public void setEstimatedCostToGoal(Tile pGoal) {
int dx = pGoal.getX() - this.getX();
int dy = pGoal.getY() - this.getY();
this.estimatedCostToGoal = (float) Math.sqrt((dx * dx) + (dy * dy)); //sqrt leifert einen Double. Mit Typecasting leicht zu wandeln.
}
public float getCost() {
return costFromStart + estimatedCostToGoal;
}
public Tile getPathParent() {
return pathParent;
}
public void setPathParent(Tile pTile) {
pathParent = pTile;
}
public float getCostFromStart() {
return costFromStart;
}
public void setCostFromStart(float pCost) {
costFromStart = pCost;
}
public float getCostForNextStep(Tile pTile) {
float cost = 0;
LinkedList neighbours = pTile.getNeighbours();
int dx = pTile.getX() - this.getX(); //
int dy = pTile.getY() - this.getY();
cost = (float) Math.sqrt((dx * dx) + (dy * dy));
return cost;
}
public BufferedImage getTileImage() {
return tileImage;
}
public void setTileImage(BufferedImage pTileImage) {
tileImage = pTileImage;
}
public int getID() {
return id;
}
public void setID(int pID) {
id = pID;
}
public int compareTo(Object other) {
float thisValue = this.getCost();
float otherValue = ((Tile) other).getCost();
float v = thisValue - otherValue;
return (v > 0) ? 1 : (v < 0) ? -1 : 0;
}
public void setColor(Color color) {
this.color = color;
}
public void drawCenteredCircle(Graphics2D g, int x, int y, int r) {
x = x - (r / 2);
y = y - (r / 2);
g.fillOval(x, y, r, r);
}
}