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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.NoninvertibleTransformException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
// TODO: remove unused import
import com.google.gson.Gson;
public abstract class MapScene extends Scene implements ActionListener {
protected Map map;
protected int panX = 0;
protected int panY = 0;
protected boolean panning = false;
protected double zoom = 1;
protected int mouseX;
protected int mouseY;
protected boolean zooming = false;
protected int maxSize;
protected AffineTransform tx = new AffineTransform();
public MapScene() {
super();
//this.map = new Map(new Dimension(10, 10));
}
@Override
protected void render() {
if (this.canvasSize.width < this.canvasSize.height) {
this.maxSize = WIDTH;
} else {
this.maxSize = HEIGHT;
}
// TODO: negate if condition and return
if (this.map == null) {
return;
}
Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics();
//clear
g.setColor(Palette.BLACK);
g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height);
//zoom and pan
if (this.zooming) {
Point2D p1 = new Point2D.Double(this.mouseX, this.mouseY);
Point2D p2 = null;
try {
p2 = tx.inverseTransform(p1, null);
} catch (NoninvertibleTransformException ex) {}
this.tx.setToIdentity();
this.tx.translate(p1.getX(), p1.getY());
this.tx.scale(this.zoom, this.zoom);
this.tx.translate(-p2.getX(), -p2.getY());
this.zooming = false;
}
if (this.panning) {
this.tx.translate(this.panX/this.zoom, this.panY/this.zoom);
this.panning = false;
this.panX = 0;
this.panY = 0;
}
g.transform(this.tx);
//draw tiles
int tileSize = this.maxSize / 10;
for (Tile tile : this.map.getGrid()) {
switch (tile.getType()) {
case CLEAR:
continue;
case GRASS:
g.setColor(Palette.GREEN);
break;
case WATER:
g.setColor(Palette.BLUE);
break;
case MOUNTAIN:
g.setColor(Palette.BROWN);
break;
}
g.fillRect(
tileSize * tile.getX(), tileSize * tile.getY(),
tileSize, tileSize);
if (tile.isSelected()) {
g.setColor(Palette.ORANGE_T);
//g.fillRect(tileSize * tile.getX() + tileSize/4, tileSize * tile.getY() + tileSize/4,
// tileSize/2, tileSize/2);
g.fillRect(
tileSize * tile.getX(), tileSize * tile.getY(),
tileSize, tileSize);
}
//TODO: set as class variables the storkes
if (tile.cursorOnIt()) {
BasicStroke oldStroke = (BasicStroke) g.getStroke();
g.setStroke(new BasicStroke(
10.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL
));
g.setColor(Palette.ORANGE);
g.drawRect(
tileSize * tile.getX(), tileSize * tile.getY(),
tileSize, tileSize);
g.setStroke(oldStroke);
}
g.setPaint(Palette.BLACK);
g.drawRect(
tileSize * tile.getX(), tileSize * tile.getY(),
tileSize, tileSize);
}
g.setColor(Palette.ORANGE);
int maxBound = this.map.getSize()*tileSize;
g.drawRect(0, 0, maxBound, maxBound);
//draw actors
g.setFont(g.getFont().deriveFont(g.getFont().getSize()*2.0F));
for (Actor actor: this.map.getActors()) {
if (actor.isEnemy()) {
g.setColor(Palette.RED);
} else {
g.setColor(Palette.BLUE);
}
g.fillRect(tileSize * actor.getX() + tileSize/5, tileSize * actor.getY() + tileSize/5,
3*tileSize/5, 3*tileSize/5);
g.setColor(Palette.BLACK);
g.drawString(actor.getName(), tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20);
g.setColor(Palette.RED);
g.fillRect(tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20+tileSize/30,
(tileSize*4/5), tileSize/10);
g.setColor(Palette.DARKGREEN);
g.fillRect(tileSize*actor.getX() + tileSize/5, tileSize*actor.getY()-tileSize/20+tileSize/30,
(tileSize*4/5)*actor.getHP()/10, tileSize/10);
}
// Restore absolute coordinates
AffineTransform invertTx = null;
try {
invertTx = this.tx.createInverse();
} catch (NoninvertibleTransformException ex) {}
g.transform(invertTx);
this.absoluteRender(g);
g.dispose();
this.buffer.show();
}
protected abstract void absoluteRender(Graphics2D g);
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
this.panY += 10;
break;
case KeyEvent.VK_DOWN:
this.panY -= 10;
break;
case KeyEvent.VK_LEFT:
this.panX += 10;
break;
case KeyEvent.VK_RIGHT:
this.panX -= 10;
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
this.mouseX = e.getX();
this.mouseY = e.getY();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
this.panX += x - this.mouseX;
this.panY += y - this.mouseY;
this.mouseX = x;
this.mouseY = y;
this.panning = true;
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
this.mouseX = e.getX();
this.mouseY = e.getY();
if (this.zoom > 0.3 || e.getWheelRotation() < 0) {
this.zoom -= ((double) e.getWheelRotation())/5;
}
this.zooming = true;
}
@Override
public void actionPerformed(ActionEvent e) {}
}
|