-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.java
348 lines (308 loc) · 12.3 KB
/
Scene.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
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
/* Tejas B and Nischay S
* Scene.java file for RingFighter
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.sound.sampled.*;
import java.io.File;
public class Scene extends JPanel {
private Image background;
private Image character1;
private Image character2;
private Player player1;
private Player player2;
private Random random;
private JButton attackButton1;
private JButton defendButton1;
private JButton attackButton2;
private JButton defendButton2;
private boolean isPlayer1Turn;
private String player1Action;
private String player2Action;
private int character1X;
private int character2X;
private boolean highlightCharacter1;
private boolean highlightCharacter2;
private Color highlightColor1;
private Color highlightColor2;
public Scene(Player player1, Player player2, boolean isPlayer1First) {
System.out.println("Initializing Scene...");
this.player1 = player1;
this.player2 = player2;
this.random = new Random();
this.isPlayer1Turn = isPlayer1First;
this.player1Action = null;
this.player2Action = null;
this.character1X = 50;
this.character2X = 600; // Adjust for initial load
this.highlightCharacter1 = false;
this.highlightCharacter2 = false;
setLayout(null);
loadImages();
createButtons();
updateButtonVisibility();
playBackgroundMusic("images/audio/introMUSIC.wav");
}
private void loadImages() {
System.out.println("Loading images...");
this.background = Toolkit.getDefaultToolkit().getImage("images/1023.jpg");
this.character1 = Toolkit.getDefaultToolkit().getImage("images/character1.png");
this.character2 = Toolkit.getDefaultToolkit().getImage("images/character2.png");
}
private void createButtons() {
System.out.println("Creating buttons...");
attackButton1 = new JButton("Attack");
defendButton1 = new JButton("Defend");
attackButton1.setBounds(10, 500, 100, 30);
defendButton1.setBounds(120, 500, 100, 30);
add(attackButton1);
add(defendButton1);
attackButton2 = new JButton("Attack");
defendButton2 = new JButton("Defend");
attackButton2.setBounds(670, 500, 100, 30);
defendButton2.setBounds(780, 500, 100, 30);
add(attackButton2);
add(defendButton2);
setButtonActions();
}
private void setButtonActions() {
System.out.println("Setting button actions...");
attackButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Player 1 Attack button clicked");
player1Action = "attack";
handlePlayerAction();
}
});
defendButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Player 1 Defend button clicked");
player1Action = "defend";
handlePlayerAction();
}
});
attackButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Player 2 Attack button clicked");
player2Action = "attack";
handlePlayerAction();
}
});
defendButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Player 2 Defend button clicked");
player2Action = "defend";
handlePlayerAction();
}
});
}
private void handlePlayerAction() {
if (isPlayer1Turn && player1Action != null) {
System.out.println("Player 1 action: " + player1Action);
isPlayer1Turn = false;
updateButtonVisibility();
} else if (!isPlayer1Turn && player2Action != null) {
System.out.println("Player 2 action: " + player2Action);
resolveActions();
player1Action = null;
player2Action = null;
isPlayer1Turn = true;
updateButtonVisibility();
}
}
private void resolveActions() {
System.out.println("Resolving actions...");
if (player1Action != null) {
executeAction(player1, player1Action, player2);
}
if (player2.getHealth() > 0 && player2Action != null) { // Only execute Player 2's action if they're not dead
executeAction(player2, player2Action, player1);
}
repaint();
}
private void executeAction(Player player, String action, Player opponent) {
if (action == null) return;
if (action.equals("attack")) {
performAttack(player, opponent);
} else if (action.equals("defend")) {
performDefense(player);
}
if (player1.getHealth() <= 0 || player2.getHealth() <= 0) {
displayGameOver();
}
}
private void performAttack(Player attacker, Player defender) {
System.out.println(attacker.getName() + " is attacking " + defender.getName());
int damage = random.nextInt(20) + 1;
defender.takeDamage(damage);
animateAttack(attacker, defender);
JOptionPane.showMessageDialog(this, attacker.getName() + " dealt " + damage + " damage to " + defender.getName());
}
private void performDefense(Player defender) {
System.out.println(defender.getName() + " is defending");
int mitigation = random.nextInt(10) + 1;
int newHealth = Math.min(defender.getHealth() + mitigation, 100); // Cap health at 100
defender.setHealth(newHealth);
JOptionPane.showMessageDialog(this, defender.getName() + " defended and healed " + mitigation + " health");
animateDefense(defender);
}
private void animateAttack(Player attacker, Player defender) {
System.out.println("Animating attack...");
Timer timer = new Timer(50, null);
final int[] step = {0};
final int totalSteps = 10;
timer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int offset;
if (step[0] < totalSteps / 2) {
offset = 5;
} else {
offset = -5;
}
if (attacker == player1) {
character1X += offset;
} else {
character2X -= offset;
}
if (step[0] == totalSteps) {
((Timer) e.getSource()).stop();
highlightImage(defender, Color.RED);
}
step[0]++;
repaint();
}
});
timer.start();
}
private void animateDefense(Player defender) {
System.out.println("Animating defense...");
highlightImage(defender, Color.BLUE);
}
private void highlightImage(Player defender, Color color) {
System.out.println("Highlighting image...");
if (defender == player1) {
highlightCharacter1 = true;
highlightColor1 = color;
} else {
highlightCharacter2 = true;
highlightColor2 = color;
}
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (defender == player1) {
highlightCharacter1 = false;
} else {
highlightCharacter2 = false;
}
repaint();
((Timer) e.getSource()).stop();
}
});
timer.setRepeats(false);
timer.start();
repaint();
}
private void updateButtonVisibility() {
System.out.println("Updating button visibility...");
attackButton1.setVisible(isPlayer1Turn);
defendButton1.setVisible(isPlayer1Turn);
attackButton2.setVisible(!isPlayer1Turn);
defendButton2.setVisible(!isPlayer1Turn);
}
private void displayGameOver() {
System.out.println("Displaying game over...");
String winner;
if (player1.getHealth() <= 0) {
winner = player2.getName();
} else {
winner = player1.getName();
}
JOptionPane.showMessageDialog(null, "Game Over! " + winner + " wins!");
JFrame gameOverFrame = new JFrame();
gameOverFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gameOverFrame.setSize(300, 300);
gameOverFrame.add(new JLabel("Game Over!"));
gameOverFrame.setVisible(true);
Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gameOverFrame.dispose();
showEndOptions();
}
});
timer.setRepeats(false);
timer.start();
}
private void showEndOptions() {
System.out.println("Showing end options...");
int option = JOptionPane.showOptionDialog(this, "What would you like to do?", "Game Over", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new Object[]{"Play Again", "Exit"}, "Play Again");
if (option == JOptionPane.YES_OPTION) {
GameUI gameUI = new GameUI((JFrame) SwingUtilities.getWindowAncestor(this));
gameUI.initialize();
} else {
System.exit(0);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
int char1Width = character1.getWidth(this);
int char1Height = character1.getHeight(this);
int char2Width = character2.getWidth(this);
int char2Height = character2.getHeight(this);
if (highlightCharacter1) {
g.setColor(highlightColor1);
g.fillRect(character1X, getHeight() - char1Height - 50, char1Width, char1Height);
}
if (highlightCharacter2) {
g.setColor(highlightColor2);
g.fillRect(character2X, getHeight() - char2Height - 50, char2Width, char2Height);
}
g.drawImage(character1, character1X, getHeight() - char1Height - 50, this);
g.drawImage(character2, character2X, getHeight() - char2Height - 50, this); // Adjust position
int healthBarWidth = 200;
int healthBarHeight = 10;
int health1 = (int) ((double) player1.getHealth() / 100 * healthBarWidth);
int health2 = (int) ((double) player2.getHealth() / 100 * healthBarWidth);
g.setColor(Color.RED);
g.fillRect(10, 10, health1, healthBarHeight);
g.setColor(Color.BLUE);
g.fillRect(getWidth() - healthBarWidth - 10, 10, health2, healthBarHeight);
g.setColor(Color.WHITE);
g.drawString(player1.getName() + "'s Health: " + player1.getHealth() + " / 100", 10, 30);
g.drawString(player2.getName() + "'s Health: " + player2.getHealth() + " / 100", getWidth() - healthBarWidth - 10, 30);
}
private void playBackgroundMusic(String filePath) {
System.out.println("Playing background music...");
try {
File musicFile = new File(filePath);
if (musicFile.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();
System.out.println("Playing background music: " + filePath);
} else {
System.out.println("File not found: " + filePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static JPanel drawGamePanel(Player player1, Player player2, boolean isPlayer1First) {
System.out.println("Drawing game panel...");
return new Scene(player1, player2, isPlayer1First);
}
}