/
Battle.java
454 lines (387 loc) · 15.3 KB
/
Battle.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Battle extends JPanel {
private boolean disabled = false;
private JPanel battlePanel = new JPanel();
JPanel opponentPanel = new JPanel();
JPanel bottomPanel = new JPanel();
JPanel PokemonPanel1 = new JPanel();
JPanel InfoPanel1 = new JPanel();
JPanel PokemonPanel2 = new JPanel();
JPanel InfoPanel2 = new JPanel();
JPanel attacksPanel = new JPanel();
JLabel battleStatus = new JLabel();
JPanel statusPanel = new JPanel();
JPanel leftPanel = new JPanel(new CardLayout());
CardLayout cl = (CardLayout)(leftPanel.getLayout());
private JButton choosePokemon;
private JButton viewBag;
private JButton fightButton;
JPanel rightPanel = new JPanel(new CardLayout());
CardLayout cl2 = (CardLayout)(rightPanel.getLayout());
private static final long serialVersionUID = 1L;
// data for updating UI
JLabel imageLabel1;
ImageIcon myPokemonImage;
JLabel imageLabel2;
ImageIcon opponentImage;
JLabel health1;
double percentage;
String healthpoints1;
JLabel health2;
double percentage2;
String healthpoints2;
JButton attack1;
JButton attack2;
JButton attack3;
JButton attack4;
JButton useSteroidsButton;
JButton useMorphineButton;
JButton useEpinephrineButton;
JButton backToSelectionButton3;
JPanel bagPanel = new JPanel();
JPanel selectionPanel = new JPanel();
JPanel changePokemonPanel = new JPanel();
JPanel epinephrineUsePanel = new JPanel();
ClientUser user;
PokemonFrame pk;
Battle(ClientUser user, PokemonFrame pk) {
setLayout(new BorderLayout());
setSize(800, 720);
this.user = user;
this.pk = pk;
battlePanel.setBackground(Color.RED);
opponentPanel.setBackground(Color.BLUE);
bottomPanel.setBackground(Color.GRAY);
battlePanel.setPreferredSize(new Dimension(490, 500));
InfoPanel1.setPreferredSize(new Dimension(490, 300));
InfoPanel2.setPreferredSize(new Dimension(490, 300));
PokemonPanel1.setPreferredSize(new Dimension(490, 200));
PokemonPanel2.setPreferredSize(new Dimension(800, 300));
opponentPanel.setPreferredSize(new Dimension(300, 500));
bottomPanel.setPreferredSize(new Dimension(800, 200));
leftPanel.setPreferredSize(new Dimension(390, 200));
rightPanel.setPreferredSize(new Dimension(390, 200));
bagPanel.setPreferredSize(new Dimension(390, 200));
changePokemonPanel.setPreferredSize(new Dimension(390, 200));
epinephrineUsePanel.setPreferredSize(new Dimension(390, 200));
add(battlePanel, BorderLayout.WEST);
add(opponentPanel, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);
battlePanel.add(InfoPanel1, BorderLayout.NORTH);
battlePanel.add(PokemonPanel1, BorderLayout.SOUTH);
opponentPanel.add(PokemonPanel2, BorderLayout.NORTH);
opponentPanel.add(InfoPanel2, BorderLayout.SOUTH);
bottomPanel.add(leftPanel, BorderLayout.EAST);
bottomPanel.add(rightPanel, BorderLayout.WEST);
//display user image
imageLabel1 = new JLabel();
myPokemonImage = new ImageIcon();
myPokemonImage = user.getCurrentPokemon().getPokemonImage();
imageLabel1.setIcon(myPokemonImage);
//display opponent image
imageLabel2 = new JLabel();
opponentImage = new ImageIcon("images/" + user.opponentPokemon + ".png");
//opponentImage = new ImageIcon//opponent.getCurrentPokemon().getPokemonImage();
imageLabel2.setIcon(opponentImage);
//display user health
health1 = new JLabel();
percentage = (user.getCurrentPokemon().getHealthPoints()/user.getCurrentPokemon().getTotalHealthPoints());
healthpoints1 = Double.toString(percentage*100.0);
health1.setText(user.getCurrentPokemon().getName() + " "+ healthpoints1 + "%");
health1.setFont(new Font("Serif", Font.BOLD, 25));
//display opponent health
health2 = new JLabel();
double totalHealthOfOpponentsPokemon = Pokemon.getPokemonObjectFromName(user.opponentPokemon).getTotalHealthPoints();
percentage2 = (user.opponentHealth/totalHealthOfOpponentsPokemon);
healthpoints2 = Double.toString(percentage2*100.0);
health2.setText(user.opponentPokemon + " " + healthpoints2 + "%");
health2.setFont(new Font("Serif", Font.BOLD,25));
battleStatus.setText("Waiting on your opponents move...");
//Attacks Panel just displaying for now.
// get pokemon's attacks
attacksPanel.setLayout(new GridLayout(2, 2));
attack1 = new JButton(user.getCurrentPokemon().allAttacks[0]);
attack2 = new JButton(user.getCurrentPokemon().allAttacks[1]);
attack3 = new JButton(user.getCurrentPokemon().allAttacks[2]);
attack4 = new JButton(user.getCurrentPokemon().allAttacks[3]);
attacksPanel.add(attack1);
attacksPanel.add(attack2);
attacksPanel.add(attack3);
attacksPanel.add(attack4);
// all the same actionlistener and write message?
AttackListener al = new AttackListener(user);
attack1.addActionListener(al);
attack2.addActionListener(al);
attack3.addActionListener(al);
attack4.addActionListener(al);
bagPanel.setLayout(new BoxLayout(bagPanel, BoxLayout.Y_AXIS));
changePokemonPanel.setLayout(new BoxLayout(changePokemonPanel, BoxLayout.Y_AXIS));
epinephrineUsePanel.setLayout(new BoxLayout(epinephrineUsePanel, BoxLayout.Y_AXIS));
useSteroidsButton = new JButton("Steroids x" + user.getItems().get("steroids"));
useMorphineButton = new JButton("Morphine x" + user.getItems().get("morphine"));
useEpinephrineButton = new JButton("Epinephrine x" + user.getItems().get("epinephrine"));
JButton backToSelectionButton = new JButton("<- Back");
JButton backToSelectionButton2 = new JButton("<- Back");
backToSelectionButton3 = new JButton("<- Back");
bagPanel.add(useSteroidsButton);
bagPanel.add(useMorphineButton);
bagPanel.add(useEpinephrineButton);
bagPanel.add(backToSelectionButton);
// populate epinephrine use panel
for(int i = 0; i < user.getPokemons().size(); ++i){
ImageIcon imageForButton = new ImageIcon("images/" + user.getPokemons().get(i).getName() + ".png");
Image image = imageForButton.getImage();
Image newimg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
ImageIcon actualImageForButton = new ImageIcon(newimg);
JButton tempButton = new JButton(user.getPokemons().get(i).getName(), actualImageForButton);
// add action listener here
tempButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
user.getItems().put("epinephrine", user.getItemQuantity("epinephrine") - 1);
System.out.println("Used 1 epinephrine. " + user.getItemQuantity("epinephrine") + " left.");
String pokemonToRevive = tempButton.getText();
user.sendMessageToServer(new Item("epinephrine", pokemonToRevive));
cl2.show(rightPanel, "Selection");
cl.show(leftPanel, "Status");
}
});
epinephrineUsePanel.add(tempButton);
}
epinephrineUsePanel.add(backToSelectionButton2);
updateChoosePokemonPanel(false);
this.choosePokemon = new JButton("Choose Pokemon");
selectionPanel.add(choosePokemon);
this.viewBag = new JButton("View Bag");
selectionPanel.add(viewBag);
this.fightButton = new JButton("FIGHT");
selectionPanel.add(fightButton);
// ACTION LISTENERS
fightButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl.show(leftPanel, "Attacks");
}
});
choosePokemon.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
updateChoosePokemonPanel(false);
cl2.show(rightPanel, "ChangePokemon");
}
});
viewBag.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl2.show(rightPanel, "Bag");
}
});
backToSelectionButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl2.show(rightPanel, "Selection");
}
});
backToSelectionButton2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl2.show(rightPanel, "Selection");
}
});
backToSelectionButton3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
cl2.show(rightPanel, "Selection");
}
});
useMorphineButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(user.getItems().get("morphine") > 0){
// SEND MESSAGE TO SERVER
user.sendMessageToServer(new Item("morphine"));
// user.getCurrentPokemon().setHealthPoints(user.getCurrentPokemon().getHealthPoints() + 200);
// setStatus("You used morphine. Health increased by 200 points.");
//// BattleData bdToSend = new BattleData(user.getOpponentID(), "morphine", user.getCurrentPokemon().getName(), user.getCurrentPokemon().getHealthPoints());
//// user.sendMessageToServer(bdToSend);
// System.out.println("Used 1 morphine. " + user.getItemQuantity("morphine") + " left.");
// updateBattleUI();
cl2.show(rightPanel, "Selection");
cl.show(leftPanel, "Status");
}
else{
JOptionPane.showMessageDialog(pk,
"You don't have any morphine to use!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
useSteroidsButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(user.getItems().get("steroids") > 0){
// SEND MESSAGE TO SERVER
System.out.println("CALLED!");
user.sendMessageToServer(new Item("steroids"));
user.getItems().put("steroids", user.getItemQuantity("steroids") - 1);
// System.out.println("Used 1 steroid. " + user.getItemQuantity("steroids") + " left.");
cl2.show(rightPanel, "Selection");
cl.show(leftPanel, "Status");
}
else{
JOptionPane.showMessageDialog(pk,
"You don't have any steroids to use!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
useEpinephrineButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(user.getItems().get("epinephrine") > 0){
// SEND MESSAGE TO SERVER
cl2.show(rightPanel, "Epinephrine");
cl.show(leftPanel, "Status");
}
else{
JOptionPane.showMessageDialog(pk,
"You don't have any epinephrine to use!",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
InfoPanel1.add(health1, BorderLayout.EAST);
InfoPanel2.add(health2);
PokemonPanel1.add(imageLabel1);
PokemonPanel2.add(imageLabel2);
statusPanel.add(battleStatus);
// flow of leftPanel
leftPanel.add(statusPanel, "Status");
leftPanel.add(attacksPanel, "Attacks");
rightPanel.add(bagPanel, "Bag");
rightPanel.add(selectionPanel, "Selection");
rightPanel.add(changePokemonPanel, "ChangePokemon");
rightPanel.add(epinephrineUsePanel, "Epinephrine");
cl.show(leftPanel, "Status");
cl2.show(rightPanel, "Selection");
this.toggle();
System.out.println("my health from bottom constr:" + user.getCurrentPokemon().getHealthPoints());
}
public void toggle() {
if(disabled) {
this.fightButton.setEnabled(true);
this.viewBag.setEnabled(true);
this.choosePokemon.setEnabled(true);
} else {
this.fightButton.setEnabled(false);
this.viewBag.setEnabled(false);
this.choosePokemon.setEnabled(false);
}
this.disabled = !this.disabled;
this.repaint();
}
class AttackListener implements ActionListener{
ClientUser user;
public AttackListener(ClientUser user) {
this.user = user;
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
this.user.sendMessageToServer(new Attack(button.getText()));
cl.show(leftPanel, "Status");
cl2.show(rightPanel, "Selection");
// opponent needs to receive user's attack
// user needs to wait for opponent to send an attack.
// user needs to receive attack
}
}
public void updateBattleUI(){
myPokemonImage = user.getCurrentPokemon().getPokemonImage();
imageLabel1.setIcon(myPokemonImage);
opponentImage = new ImageIcon("images/" + user.opponentPokemon + ".png");
imageLabel2.setIcon(opponentImage);
double mHealth = user.getCurrentPokemon().getHealthPoints();
double mTotalHealth = user.getCurrentPokemon().getTotalHealthPoints();
percentage = mHealth/mTotalHealth;
DecimalFormat df2 = new DecimalFormat("#.00");
System.out.println("My health percentage: " + percentage);
healthpoints1 = df2.format(percentage*100.0);
health1.setText(user.getCurrentPokemon().getName() + " "+ healthpoints1 + "%");
double totalHealthOfOpponentsPokemon = Pokemon.getPokemonObjectFromName(user.opponentPokemon).getTotalHealthPoints();
percentage2 = (user.opponentHealth/totalHealthOfOpponentsPokemon);
percentage2 = Double.valueOf(df2.format(percentage2));
healthpoints2 = df2.format(percentage2*100.0);
health2.setText(user.opponentPokemon + " " + healthpoints2 + "%");
attack1.setText(user.getCurrentPokemon().allAttacks[0]);
attack2.setText(user.getCurrentPokemon().allAttacks[1]);
attack3.setText(user.getCurrentPokemon().allAttacks[2]);
attack4.setText(user.getCurrentPokemon().allAttacks[3]);
updateBagView();
this.repaint();
}
public void updateBagView(){
useSteroidsButton.setText("Steroids x" + user.getItemQuantity("steroids"));
useMorphineButton.setText("Morphine x" + user.getItemQuantity("morphine"));
useEpinephrineButton.setText("Epinephrine x" + user.getItemQuantity("epinephrine"));
}
public void updateChoosePokemonPanel(boolean backDisabled){
changePokemonPanel.removeAll();
changePokemonPanel.repaint();
changePokemonPanel.revalidate();
// populate choose pokemon panel
for(int i = 0; i < user.getPokemons().size(); ++i){
if(user.getPokemons().get(i).isDead() || user.getPokemons().get(i).getName().equals(user.getCurrentPokemon().getName())){
continue;
}
ImageIcon imageForButton = new ImageIcon("images/" + user.getPokemons().get(i).getName() + ".png");
Image image = imageForButton.getImage();
Image newimg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
ImageIcon actualImageForButton = new ImageIcon(newimg);
JButton tempButton = new JButton(user.getPokemons().get(i).getName(), actualImageForButton);
// add action listener here
tempButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String pokemonToSwitchTo = tempButton.getText();
user.sendMessageToServer(new Switch(pokemonToSwitchTo));
cl2.show(rightPanel, "Selection");
cl.show(leftPanel, "Status");
backToSelectionButton3.setEnabled(true);
}
});
changePokemonPanel.add(tempButton);
}
if(backDisabled) {
backToSelectionButton3.setEnabled(false);
}
changePokemonPanel.add(backToSelectionButton3);
changePokemonPanel.repaint();
changePokemonPanel.revalidate();
}
public void setStatus(String text){
battleStatus.setText(text);
}
public void forceSwitchPokemon() {
updateChoosePokemonPanel(true);
cl2.show(rightPanel, "ChangePokemon");
}
}