-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoD_Prototype.java
More file actions
396 lines (348 loc) · 15.2 KB
/
Copy pathDoD_Prototype.java
File metadata and controls
396 lines (348 loc) · 15.2 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
public class DoD_Prototype extends JPanel implements KeyListener {
private Timer introTimer;
private Timer strobeTimer;
private Timer finalBossTimer;
private Timer endScreenTimer;
private ArrayList<StoryFrame> introFrames = new ArrayList<>();
private boolean inIntro = true;
private int currentFrame = 0;
private int lettersShown = 0;
private long frameCompleteTime = 0;
private final int lingerMillis = 7000;
private boolean inMenu = false;
private int menuOrientation = 0;
private Color menuColor = Color.RED;
private boolean gameStarted = false;
private int strobeIndex = 0;
private final int arenaHeight = 500;
private final int playerBoxWidth = 50;
private final int playerBoxHeight = 50;
private int orientation = 0;
private Color playerColor = Color.RED;
private int level = 0;
private ArrayList<EnemyOrb> enemyOrbs = new ArrayList<>();
private int lastOrientation = 0;
private BufferedImage finalBossImage;
private boolean inFinalLevel = false;
private ArrayList<EnemyOrb> finalOrbs = new ArrayList<>();
private EndScreen endScreen = null;
public DoD_Prototype() {
setFocusable(true);
addKeyListener(this);
try {
introFrames.add(new StoryFrame(ImageIO.read(new File("scene00.png")),
"Once upon a time, there lived a Kingdom inside of a computer...",
"One day the computer was infected by an unknown virus."));
introFrames.add(new StoryFrame(ImageIO.read(new File("scene01.png")),
"The ruler of the kingdom held its only defense against the plague",
"but in his old age could not use it to defend the kingdom.",
"The king asks YOU! Could you bear the sacrifice of your own arm?"));
introFrames.add(new StoryFrame(ImageIO.read(new File("scene02.png")),
"This is the story of Random Access Memory"));
finalBossImage = ImageIO.read(new File("finalBoss.png"));
} catch (IOException e) {
e.printStackTrace();
}
introTimer = new Timer(50, e -> {
if (inIntro && currentFrame < introFrames.size()) {
StoryFrame frame = introFrames.get(currentFrame);
int totalLetters = Arrays.stream(frame.lines).mapToInt(String::length).sum();
if (lettersShown < totalLetters) lettersShown++;
else {
if (frameCompleteTime == 0) frameCompleteTime = System.currentTimeMillis();
if (System.currentTimeMillis() - frameCompleteTime >= lingerMillis) {
lettersShown = 0;
currentFrame++;
frameCompleteTime = 0;
if (currentFrame >= introFrames.size()) {
inIntro = false;
inMenu = true;
}
}
}
repaint();
}
});
introTimer.start();
strobeTimer = new Timer(16, e -> {
if (gameStarted) strobeIndex++;
repaint();
});
strobeTimer.start();
finalBossTimer = new Timer(900, e -> {
if (!gameStarted || !inFinalLevel || finalOrbs.isEmpty()) return;
for (EnemyOrb orb : finalOrbs) {
orb.orientation = (int) (Math.random() * 4);
int c = (int) (Math.random() * 3);
orb.triangleColor = switch (c) {
case 0 -> Color.RED;
case 1 -> Color.GREEN;
default -> Color.BLUE;
};
}
repaint();
});
finalBossTimer.start();
}
private void spawnLevel(int lvl) {
enemyOrbs.clear();
int numOrbs;
switch (lvl) {
case 0 -> numOrbs = 1;
case 1 -> numOrbs = 3;
case 2 -> numOrbs = 4;
default -> numOrbs = 3;
}
int arenaW = getWidth() - 200;
int arenaX = (getWidth() - arenaW) / 2;
int centerY = (getHeight() - arenaHeight) / 2 + arenaHeight / 2;
int spacing = arenaW / (numOrbs + 1);
for (int i = 0; i < numOrbs; i++) {
int x = arenaX + spacing * (i + 1) - 25;
int y = centerY - 25;
Color color = switch (lvl % 3) {
case 0 -> Color.RED;
case 1 -> Color.GREEN;
default -> Color.BLUE;
};
int orbOrientation = lastOrientation;
lastOrientation = (lastOrientation + 1) % 4;
enemyOrbs.add(new EnemyOrb(x, y, 50, color, orbOrientation));
}
}
private void spawnFinalLevel() {
finalOrbs.clear();
if (finalBossImage == null) return;
int orbSize = 50;
int[][] eyeCoords = {
{217, 165}, {213, 271}, {340, 206}, {473, 103},
{567, 231}, {645, 122}, {722, 298}
};
for (int[] coord : eyeCoords) {
int orbX = coord[0] - orbSize / 2;
int orbY = coord[1] - orbSize / 2;
int orientation = (int) (Math.random() * 4);
Color color = switch ((int) (Math.random() * 3)) {
case 0 -> Color.RED;
case 1 -> Color.GREEN;
default -> Color.BLUE;
};
finalOrbs.add(new EnemyOrb(orbX, orbY, orbSize, color, orientation));
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
if (endScreen != null) {
stopAllTimers();
endScreen.setSize(getWidth(), getHeight());
endScreen.paintComponent(g2d);
return;
}
if (inIntro && currentFrame < introFrames.size()) {
StoryFrame frame = introFrames.get(currentFrame);
int imgW = frame.image.getWidth();
int imgH = frame.image.getHeight();
double scale = Math.min((double) (getWidth() / 2) / imgW, (double) (getHeight() / 2) / imgH);
int drawW = (int) (imgW * scale);
int drawH = (int) (imgH * scale);
int imgX = (getWidth() - drawW) / 2;
int imgY = 50;
g2d.drawImage(frame.image, imgX, imgY, drawW, drawH, null);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Times New Roman", Font.BOLD, 24));
int lineHeight = 30;
int y = getHeight() - frame.lines.length * lineHeight - 50;
int lettersRemaining = lettersShown;
for (String line : frame.lines) {
int len = Math.min(line.length(), lettersRemaining);
String substring = line.substring(0, len);
int textWidth = g2d.getFontMetrics().stringWidth(substring);
int x = (getWidth() - textWidth) / 2;
g2d.drawString(substring, x, y);
y += lineHeight;
lettersRemaining -= len;
if (lettersRemaining <= 0) break;
}
return;
}
if (inMenu) {
int triSize = Math.min(getWidth(), getHeight()) / 3;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int[] xPoints = {0, -triSize, -triSize};
int[] yPoints = {0, -triSize, triSize};
AffineTransform old = g2d.getTransform();
g2d.translate(centerX, centerY);
g2d.rotate(Math.toRadians(menuOrientation * 90));
g2d.setColor(menuColor);
g2d.fillPolygon(xPoints, yPoints, 3);
g2d.setTransform(old);
return;
}
if (gameStarted) {
int arenaW = getWidth() - 200;
int arenaH = arenaHeight;
int arenaX = (getWidth() - arenaW) / 2;
int arenaY = (getHeight() - arenaH) / 2;
float hue = (strobeIndex % 360) / 360f;
g2d.setColor(Color.getHSBColor(hue, 1f, 1f));
g2d.setStroke(new BasicStroke(5));
g2d.drawRect(arenaX, arenaY, arenaW, arenaH);
int playerX = arenaX + arenaW / 2 - playerBoxWidth / 2;
int playerY = arenaY + arenaH + 10;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(1));
g2d.drawRect(playerX, playerY, playerBoxWidth, playerBoxHeight);
int triSize = playerBoxWidth / 2 - 5;
AffineTransform old = g2d.getTransform();
g2d.translate(playerX + playerBoxWidth / 2, playerY + playerBoxHeight / 2);
g2d.rotate(Math.toRadians(orientation * 90));
g2d.setColor(playerColor);
g2d.fillPolygon(new int[]{0, -triSize, -triSize}, new int[]{0, -triSize, triSize}, 3);
g2d.setTransform(old);
if (inFinalLevel && finalBossImage != null) {
int imgW = finalBossImage.getWidth();
int imgH = finalBossImage.getHeight();
double scale = Math.min((double) getWidth() / 2 / imgW, (double) getHeight() / 2 / imgH);
int drawW = (int) (imgW * scale);
int drawH = (int) (imgH * scale);
int imgX = (getWidth() - drawW) / 2;
int imgY = (getHeight() - drawH) / 2;
g2d.drawImage(finalBossImage, imgX, imgY, drawW, drawH, null);
int[][] eyeCoords = {{734, 127}, {424, 293}, {408, 152}, {600, 60}, {217, 240}, {599, 278}};
int count = Math.min(finalOrbs.size(), eyeCoords.length);
for (int i = 0; i < count; i++) {
EnemyOrb orb = finalOrbs.get(i);
int screenX = imgX + (int) (eyeCoords[i][0] * ((double) drawW / imgW)) - orb.size / 2;
int screenY = imgY + (int) (eyeCoords[i][1] * ((double) drawH / imgH)) - orb.size / 2;
orb.draw(g2d, true, screenX, screenY, orb.size);
}
} else {
for (int i = 0; i < enemyOrbs.size(); i++) {
EnemyOrb orb = enemyOrbs.get(i);
boolean isActive = (i == 0);
orb.draw(g2d, isActive);
}
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (endScreen != null) return;
if (inIntro && e.getKeyCode() == KeyEvent.VK_SPACE) {
inIntro = false;
inMenu = true;
repaint();
return;
}
if (gameStarted) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT -> orientation = (orientation + 1) % 4;
case KeyEvent.VK_LEFT -> orientation = (orientation + 3) % 4;
case KeyEvent.VK_DOWN -> {
ArrayList<EnemyOrb> activeOrbs = inFinalLevel ? finalOrbs : enemyOrbs;
EnemyOrb matchedOrb = null;
for (EnemyOrb orb : activeOrbs) {
if (orb.matchesPlayer(orientation, playerColor)) {
matchedOrb = orb;
break;
}
}
if (matchedOrb != null) {
final EnemyOrb orbToRemove = matchedOrb;
orbToRemove.triangleColor = Color.WHITE;
repaint();
Timer flashTimer = new Timer(150, ev -> {
activeOrbs.remove(orbToRemove);
repaint();
if (activeOrbs.isEmpty()) {
if (!inFinalLevel) {
level++;
if (playerColor.equals(Color.RED)) playerColor = Color.GREEN;
else if (playerColor.equals(Color.GREEN)) playerColor = Color.BLUE;
else if (playerColor.equals(Color.BLUE)) {
inFinalLevel = true;
spawnFinalLevel();
}
spawnLevel(level);
} else {
endScreen = new EndScreen(false); // Victory here
stopAllTimers();
endScreenTimer = new Timer(50, evt2 -> {
repaint();
});
endScreenTimer.start();
}
}
});
flashTimer.setRepeats(false);
flashTimer.start();
}
}
case KeyEvent.VK_UP -> {
if (inFinalLevel) {
if (playerColor.equals(Color.RED)) playerColor = Color.GREEN;
else if (playerColor.equals(Color.GREEN)) playerColor = Color.BLUE;
else if (playerColor.equals(Color.BLUE)) playerColor = Color.RED;
}
}
}
repaint();
}
if (inMenu) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT -> menuOrientation = (menuOrientation + 1) % 4;
case KeyEvent.VK_LEFT -> menuOrientation = (menuOrientation + 3) % 4;
case KeyEvent.VK_UP -> {
if (menuColor.equals(Color.RED)) menuColor = Color.GREEN;
else if (menuColor.equals(Color.GREEN)) menuColor = Color.BLUE;
else menuColor = Color.RED;
}
case KeyEvent.VK_DOWN -> {
inMenu = false;
gameStarted = true;
spawnLevel(level);
repaint();
}
}
}
}
@Override public void keyReleased(KeyEvent e) {}
@Override public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("DoD Prototype");
DoD_Prototype panel = new DoD_Prototype();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1226, 733);
frame.setResizable(false);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
panel.requestFocusInWindow();
}
static class StoryFrame {
BufferedImage image;
String[] lines;
StoryFrame(BufferedImage image, String... lines) { this.image = image; this.lines = lines; }
}
private void stopAllTimers(){
if (introTimer != null) introTimer.stop();
if (strobeTimer != null) strobeTimer.stop();
if (finalBossTimer != null) finalBossTimer.stop();
if (endScreenTimer != null) endScreenTimer.stop();
}
}