This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
213 lines (189 loc) · 5.61 KB
/
Cell.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
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
207
208
209
210
211
212
213
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
public class Cell {
private Polygon hexagon = new Polygon();
private int x;
private int y;
public Cell north;
public Cell south;
public Cell northEast;
public Cell southEast;
public Cell northWest;
public Cell southWest;
public static enum Type {EARTH, WATER, SWAMP};
private Type type;
private Unit unit;
private static final Color COLOR_BORDER = Color.LIGHT_GRAY;
private final Color COLOR_FILL;
private final Color COLOR_FILL_HOVER;
private static final Color COLOR_FILL_SELECTED = new Color(250, 255 , 200);
private final Color COLOR_VALID_MOVE;
private Color currentFill; //current color fill
private Color currentBorder = COLOR_BORDER;
private static final double ANGLE = (2*Math.PI)/6;
public static final int DIST_TO_CORNER = 18; // DETERMINES SIZE OF EVERYTHING
public static final int DIST_TO_EDGE = (int)(DIST_TO_CORNER * Math.cos(ANGLE/2));
private boolean firstPaint = true;
/**
* Hexagon shaped cell on the board
* @param xPos The x position
* @param yPos The y position
* @param c The color of the cell
*/
public Cell(int xPos, int yPos, Type cellType) {
x = xPos;
y = yPos;
type = cellType;
switch (type) {
case EARTH:currentFill = new Color(139, 69, 19, 175); break;
case WATER: currentFill = new Color(0, 0, 255, 175); break;
case SWAMP: currentFill = new Color(0, 255, 0, 175); break;
default: throw new IllegalArgumentException("Unknown cell type!");
}
COLOR_FILL = currentFill;
COLOR_FILL_HOVER = BasicWars.bleach(COLOR_FILL, 0.25f);
COLOR_VALID_MOVE = BasicWars.bleach(COLOR_FILL, 0.50f);
for (int i = 0; i < 6; i++) {
int x2 = (int)(x + DIST_TO_CORNER * Math.cos(i * ANGLE));
int y2 = (int)(y + DIST_TO_CORNER * Math.sin(i * ANGLE));
hexagon.addPoint(x2, y2);
}
}
public void paintCell(Graphics g) {
g.setColor(currentFill);
g.fillPolygon(hexagon);
//g.drawOval(x, y, 1, 1); //center point
g.setColor(currentBorder);
g.drawPolygon(hexagon);
if (unit != null) { // Ideal image is 33x28 so resize doesn't look terrible
Image image = unit.getImage();
Graphics2D g2 = (Graphics2D)g;
int newW = (int)(DIST_TO_CORNER * 2);
int newH = (int)(DIST_TO_EDGE * 2);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, x-DIST_TO_CORNER, y-DIST_TO_EDGE, newW, newH, null);
//image = image.getScaledInstance(2*DIST_TO_CORNER, 2*DIST_TO_EDGE, Image.SCALE_SMOOTH);
//g.drawImage(image,x-DIST_TO_CORNER+2,y-DIST_TO_EDGE+1,null);
}
}
/**
* Place a unit to this cell. Only one unit per cell.
* @param u Unit that resides in this cell
*/
public void setUnit(Unit u) { unit = u; }
public Unit getUnit() { return unit; }
public int getX() { return x; }
public int getY() { return y; }
public Type getType() { return type; }
public boolean contains(Point p) { return hexagon.contains(p); }
public static Type parseType(char ch) {
Cell.Type type;
switch (ch) {
case 'E': type = Cell.Type.EARTH; break;
case 'W': type = Cell.Type.WATER; break;
case 'S': type = Cell.Type.SWAMP; break;
default: throw new IllegalArgumentException("Unknown cell type, '" + ch + "'");
}
return type;
}
/**
* Changes color of cell and shows unit as selected in controlPanel
* @param o BasicWars object
* @return this Cell
*/
public Cell setSelected(BasicWars o) {
if (o != null) {
currentFill = COLOR_FILL_SELECTED;
o.showSelected(unit);
} else {
currentFill = COLOR_FILL;
}
return this;
}
public void setValidMove(boolean isValid) {
currentFill = (isValid) ? COLOR_VALID_MOVE : COLOR_FILL;
}
public void setValidAttack(boolean isValid) {
currentFill = (isValid) ? COLOR_VALID_MOVE : COLOR_FILL;
}
public void mouseEntered() {
currentFill = COLOR_FILL_HOVER;
}
public void mouseExited() {
currentFill = COLOR_FILL;
}
public Cell[] getAdjacentCells() {
Cell[] adj = new Cell[6];
adj[0] = north;
adj[1] = northEast;
adj[2] = southEast;
adj[3] = south;
adj[4] = southWest;
adj[5] = northWest;
return adj;
}
public Cell generateSouth(Type type) {
Cell c = new Cell(x, y + 2*DIST_TO_EDGE, type);
south = c;
c.north = this;
if (southWest != null) {
southWest.southEast = south;
south.northWest = southWest;
}
if (southEast != null) {
southEast.southWest = south;
south.northEast = southEast;
}
return c;
}
public Cell generateSouthEast(Type type) {
Cell c = new Cell(x + (int)(1.5*DIST_TO_CORNER), y + DIST_TO_EDGE, type);
southEast = c;
c.northWest = this;
if (south != null) {
south.northEast = southEast;
southEast.southWest = south;
}
if (northEast != null) {
northEast.south = southEast;
southEast.north = northEast;
}
return c;
}
public Cell generateNorthEast(Type type) {
Cell c = new Cell(x + (int)(1.5*DIST_TO_CORNER), y - DIST_TO_EDGE, type);
northEast = c;
c.southWest = this;
if (north != null) {
north.southEast = c;
c.northWest = north;
Cell relNorth = north.northEast;
if (relNorth != null) {
relNorth.south = c;
c.north = relNorth;
Cell relNorthEast = relNorth.southEast;
if (relNorthEast != null) {
relNorthEast.southWest = c;
northEast.northEast = relNorthEast;
}
}
}
if (southEast != null) {
southEast.north = northEast;
northEast.south = southEast;
}
return c;
}
public boolean firstPaint() {
if (firstPaint) {
firstPaint = false;
return true;
}
return false;
}
}