/
GWar.java
591 lines (579 loc) · 16.8 KB
/
GWar.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//Name: Daniel Johnson License: GNU GPL 2 Date: Who Knows??
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javazoom.jl.player.*;
import java.io.InputStream;
import java.io.FileInputStream;
public class GWar extends JPanel{
public static boolean DEBUG=false;
private static final int RADIUS = 25;
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
public static final int NUMLEVELS=4;
public double xScale=1024.0/WIDTH;
public double yScale=768/HEIGHT;
public static final Color[] PCOLOR = {new Color(200,0,200),Color.GREEN,Color.BLUE.brighter(),Color.RED,Color.WHITE,Color.YELLOW,Color.GREEN.darker().darker(),Color.GRAY,new Color(130,37,14)};
private int TOTALPLAYERS=2;
public int TOTALLIVES=5;
private static final Color BACKGROUND = java.awt.Color.BLACK;
private BufferedImage myImage;
private Graphics myBuffer;
private Timer t;
public Hero[] p = new Hero[TOTALPLAYERS];
public int[] lives = new int[TOTALPLAYERS];
private int numalive = TOTALPLAYERS;
//public Bumper[] level4={new SpeedBumper(100,150,100,10,3,0,true),new SpeedBumper(400,150,100,10,-3,0,true),new Bumper(250,200,100,5,true), new SpeedBumper(50,300,75,5,0,-15), new SpeedBumper(475,300,75,5,0,-15),new Bumper(150,250,50,5,true),new Bumper(400,250,50,5,true),new Bumper(150,350,100,10,true),new Bumper(350,350,100,10,true)};
public Bumper[] bump;
private Bumper[][] levels=new Bumper[NUMLEVELS][];
public int[] typeAI = new int[]{1, 1, 0};
public KListener kl = new KListener();
public int running = 2; //0-Stopped 1-Running 2-Menu 3-Paused
private boolean fullscreen = false;
private MainMenu mm;
private JFrame window;
private InputStream music;
private boolean ismusic=true;
public boolean sparkles=false;
Player player;
PlayerThread pt;
public GWar(){
ismusic=true;
mm = new MainMenu(this, xScale,yScale);
setupFrame();
initMusic();
TOTALPLAYERS = 2;
p = new Hero[TOTALPLAYERS];
lives = new int[TOTALPLAYERS];
t = new Timer(50,
new ActionListener(){
public void actionPerformed(ActionEvent e){
update();
}
});
setFocusable(true);
addKeyListener(kl);
//newGame();
for(int k=0;k<NUMLEVELS;k++)
levels[k]=readLevel("data/level"+(k+1)+".gwar");
bump=levels[0];
playMusic(music);
t.start();
mm.show();
}
public void initMusic(){
try {
music = new FileInputStream("data/Chrono_Symphonic_23_The_Last_Stand.mp3");
}
catch(Exception e){
System.out.println("Music failed to load:"+e.getMessage());
}
}
public void playMusic(InputStream in){
try{
player = new Player(in);
pt = new PlayerThread();
pt.start();
}
catch(Exception e){
System.out.println("Music failed to play");
}
}
public void toggleMusic(){
ismusic=!ismusic;
if(!ismusic)
stopMusic();
}
public void loadLevel(int index){
bump=levels[index];
for(int x=0;x<p.length;x++)
{
p[x].reset();
}
}
private Bumper[] readLevel(String file){
System.out.println(file);
try{
java.io.BufferedReader f = new java.io.BufferedReader(new java.io.FileReader(file));
int size = Integer.parseInt(f.readLine());
Bumper[] tempbump = new Bumper[size];
for(int k = 0; k<size;k++){
java.util.StringTokenizer st = new java.util.StringTokenizer(f.readLine());
int blocktype=Integer.parseInt(st.nextToken());
if(blocktype==0)
tempbump[k]= new Bumper(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),st.nextToken().equalsIgnoreCase("true"));
else if(blocktype==1)
tempbump[k]= new SpeedBumper(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()),st.nextToken().equalsIgnoreCase("true"));
}
/*size = Integer.parseInt(f.readLine());
e = new LinkedList<Enemy>();
for(int k = 0; k < size; k++){
java.util.StringTokenizer st = new java.util.StringTokenizer(f.readLine());
Double tempx = Double.parseDouble(st.nextToken());
Double tempy = Double.parseDouble(st.nextToken());
boolean type = st.nextToken().equalsIgnoreCase("true");
java.awt.Color tempcolor;
if(type)
tempcolor=java.awt.Color.red;
else
tempcolor=java.awt.Color.blue;
e.add(new Enemy(tempx,tempy,10,this,tempcolor,1,type));
}*/
return tempbump;
}
catch(java.io.FileNotFoundException ex){
System.out.println("Having been erased,");
System.out.println("The document you're seeking");
System.out.println("Must now be retyped.");
System.out.println();
System.out.println("Level file not found");
System.exit(1);
}
catch(java.io.IOException ex){
System.out.println("Problem reading file");
}
return null;
}
public void stopMusic(){
if(player!=null)
player.close();
}
public void newGame(){
p = new Hero[TOTALPLAYERS];
lives = new int[TOTALPLAYERS];
numalive = TOTALPLAYERS;
for(int k = 0; k< lives.length;k++)
lives[k]=TOTALLIVES+1;
running = 1;
for(int k = 0; k< p.length;k++)
respawn(k);
for(int k = 0; k< lives.length;k++)
lives[k]=TOTALLIVES;
display();
t.start();
}
public void newGame(int lives,int humanPlayers, int numAI, int numNick){
TOTALPLAYERS=humanPlayers+numAI+numNick;
TOTALLIVES=lives;
typeAI = new int[TOTALPLAYERS];
int k;
for (k =humanPlayers;k<humanPlayers+numAI;k++)
typeAI[k]=1;
for (;k<humanPlayers+numAI+numNick;k++){
typeAI[k]=2;
}
newGame();
}
public void newBackgroundGame(int n, int nick){
newGame(5,0,n,nick);
running = 2;
}
public void respawn(int index){
if (index==0){
p[0] = new Hero (400,200,10,this,PCOLOR[index],1);
}
else if (index==1){
p[1] = new Hero (200,200,10,this,PCOLOR[index],1);
}
else if (index==2)
p[2] = new Hero (300,150,10,this,PCOLOR[index],1);
else
p[index] = new Hero (400,100,10,this,PCOLOR[index],1);
if(running!=2)
lives[index]--;
if (lives[index] == 0){
p[index]=null;
numalive--;
}
if (numalive<=1)
for(int k = 0; k<p.length;k++)
if (p[k]!=null){
gameOver(k);
break;
}
}
public void update(){
if(ismusic&&pt!=null)
if (!pt.isAlive()){
player.close();
initMusic();
playMusic(music);
}
if(!hasFocus())
requestFocus();
if(running==1||running==2){
Bumper on;
SpeedBumper sOn;
for (int k = 0; k< p.length; k++){
if (p[k]!=null){
if(p[k].onBumper()){
on = p[k].onWhatBumper();
if(on instanceof SpeedBumper)
{
sOn = (SpeedBumper)on;
p[k].dx+=sOn.getXVelocity();
p[k].dy+=sOn.getYVelocity();
p[k].jumps=0;
}
}
p[k].update();
}
}
for (int k = 0; k< p.length; k++){
if (p[k]!=null)
p[k].gravity();
}
for (int k = 0; k< p.length; k++)
for (int n = k+1; n<p.length;n++)
if((k!=n)&&(p[k]!=null)&&(p[n]!=null))
p[k].checkPlayer(p[n]);
for (int k = 0; k< p.length; k++){
if (p[k]!=null)
if(!p[k].onScreen(WIDTH,HEIGHT))
respawn(k);
}
kl.update();
for (int k = 0; k<TOTALPLAYERS; k++)
if(p[k]!=null){
if(typeAI[k]==1)
p[k].ai();
if(typeAI[k]==2)
p[k].nickAI();
}
}
if(running==1)
display();
else if (running == 2){
display();
mm.drawB(myBuffer);
repaint();
}
}
public void display(){
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0,0,(int)(WIDTH*xScale),(int)(HEIGHT*yScale));
for (int k = 0; k< p.length; k++){
if(p[k]!=null)
p[k].draw(myBuffer);
}
for (int k=0;k<bump.length;k++)
{
bump[k].draw(myBuffer,xScale,yScale);
if(DEBUG){
myBuffer.setColor(Color.green);
myBuffer.drawString(k+"",(int)(bump[k].getX()*xScale),(int)(bump[k].getY()*yScale)-2);
}
}
if(running==1){
drawScore(0,WIDTH-30,30);
drawScore(1,30,30);
if (TOTALPLAYERS>=3){
drawScore(2,30, HEIGHT-30);
if (TOTALPLAYERS>=4){
drawScore(3,WIDTH-30,HEIGHT-30);
if(TOTALPLAYERS>=5){
drawScore(4,WIDTH/2-30,30);
if(TOTALPLAYERS>=6){
drawScore(5,WIDTH/2-30,HEIGHT-30);
}
}
}
}
}
repaint();
}
private void drawScore(int player,int xpos, int ypos){
if(lives[player]<=0)
return;
myBuffer.setFont(new Font("Serif",Font.PLAIN,(int)(20*yScale)));
myBuffer.setColor(PCOLOR[player]);
myBuffer.fillOval((int)(xpos*xScale-(30*xScale*((double)(lives[player])/TOTALLIVES))/2),(int)(ypos*yScale-(30*yScale*((double)(lives[player])/TOTALLIVES))/2),(int)(30*xScale*((double)(lives[player])/TOTALLIVES)),(int)(30*yScale*((double)(lives[player])/TOTALLIVES)));
myBuffer.setColor(new Color(255-PCOLOR[player].getRed(),255-PCOLOR[player].getGreen(),255-PCOLOR[player].getBlue()));
myBuffer.drawString(lives[player]+"",(int)((xpos-5)*xScale),(int)((ypos+10)*yScale));
}
public void gameOver(int player){
t.stop();
running = 0;
display();
myBuffer.setColor(PCOLOR[player]);
myBuffer.drawString("Game Over, Player " + (player+1) + " wins!!",(int)(((WIDTH/2.0)-125)*xScale),(int)((HEIGHT/2)*yScale));
myBuffer.drawString("Press Esc for menu, Space to restart.",(int)(((WIDTH/2.0)-150)*xScale),(int)(((HEIGHT/2)+30)*yScale));
repaint();
}
public void pause(){
t.stop();
running = 3;
display();
myBuffer.setColor(Color.white);
myBuffer.drawString("Game Paused",(int)(((WIDTH/2.0)-75)*xScale),(int)((HEIGHT/2)*yScale));
myBuffer.drawString("Press Esc for menu, Any other key to resume.",(int)(((WIDTH/2.0)-225)*xScale),(int)(((HEIGHT/2)+30)*yScale));
repaint();
}
public void unpause(){
running=1;
t.start();
}
private class KListener extends KeyAdapter{
private boolean p1left=false;
private boolean p1right=false;
private boolean p2left = false;
private boolean p2right = false;
private boolean p3left = false;
private boolean p3right = false;
private static final int P1JUMP=KeyEvent.VK_UP;
private static final int P1LEFT=KeyEvent.VK_LEFT;
private static final int P1RIGHT=KeyEvent.VK_RIGHT;
private static final int P1DOWN=KeyEvent.VK_DOWN;
private static final int P1ATTACK=KeyEvent.VK_SHIFT;
private static final int P1ATTACKALT=KeyEvent.VK_CONTROL;
private static final int P2JUMP=KeyEvent.VK_W;
private static final int P2LEFT=KeyEvent.VK_A;
private static final int P2RIGHT=KeyEvent.VK_D;
private static final int P2DOWN=KeyEvent.VK_S;
private static final int P2ATTACK=KeyEvent.VK_Q;
private static final int P3JUMP=KeyEvent.VK_NUMPAD5;
private static final int P3LEFT=KeyEvent.VK_NUMPAD1;
private static final int P3RIGHT=KeyEvent.VK_NUMPAD3;
private static final int P3DOWN=KeyEvent.VK_NUMPAD2;
private static final int P3ATTACK=KeyEvent.VK_NUMPAD0;
private static final int SPARKLE=KeyEvent.VK_0;
private boolean escpressed=true;
public void keyPressed(KeyEvent e){
if(running==3){
if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
if(!escpressed){
t.start();
mm.show();
return;
}
}
else
unpause();
}
if(running==1){
if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
escpressed=true;
pause();
}
else if(e.getKeyCode()==SPARKLE)
sparkles= !sparkles;
else if(p[0]!=null)
if(e.getKeyCode()==P1JUMP){
//p1up=true;
p[0].jump();
}
else if(e.getKeyCode()==P1DOWN){
p[0].fastFall();
}
else if(e.getKeyCode()==P1RIGHT){
if (!p1right){
p1right=true;
p[0].moveRight();
}
}
else if(e.getKeyCode()==P1LEFT){
if (!p1left){
p1left=true;
p[0].moveLeft();
}
}
else if(e.getKeyCode()==P1ATTACK||e.getKeyCode()==P1ATTACKALT){
if(p1left)
p[0].attackLeft();
else if (p1right)
p[0].attackRight();
}
if(p[1]!=null)
if(e.getKeyCode()==P2LEFT){
if(!p2left){
p2left=true;
p[1].moveLeft();
}
}
else if(e.getKeyCode()==P2RIGHT){
if(!p2right){
p2right=true;
p[1].moveRight();
}
}
else if(e.getKeyCode()==P2DOWN){
p[1].fastFall();
}
else if (e.getKeyCode()==P2JUMP)
p[1].jump();
else if(e.getKeyCode()==P2ATTACK){
if(p2left)
p[1].attackLeft();
else if (p2right)
p[1].attackRight();
}
if(TOTALPLAYERS>=3)
if(p[2]!=null)
if(e.getKeyCode()==P3LEFT){
if(!p3left){
p3left=true;
p[2].moveLeft();
}
}
else if(e.getKeyCode()==P3RIGHT){
if(!p3right){
p3right=true;
p[2].moveRight();
}
}
else if(e.getKeyCode()==P3DOWN){
p[2].fastFall();
}
else if (e.getKeyCode()==P3JUMP)
p[2].jump();
else if(e.getKeyCode()==P3ATTACK){
if(p3left)
p[2].attackLeft();
else if (p3right)
p[2].attackRight();
}
}
else if (running ==0){
if(e.getKeyCode()==KeyEvent.VK_SPACE)
newGame();
else if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
t.start();
mm.show();
}
}
else if (running ==2){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
mm.enter();
else if(e.getKeyCode()==KeyEvent.VK_UP)
mm.moveUp();
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
mm.moveDown();
else if(e.getKeyCode()==SPARKLE)
sparkles= !sparkles;
}
}
public void keyReleased(KeyEvent e){
if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
escpressed=false;
}
else if(p[0]!=null)
if(e.getKeyCode()==P1RIGHT){
p1right=false;
p[0].unMoveRight();
}
else if(e.getKeyCode()==P1LEFT){
p1left=false;
p[0].unMoveLeft();
}
if(p[1]!=null)
if(e.getKeyCode()==P2LEFT){
p2left=false;
p[1].unMoveLeft();
}
else if(e.getKeyCode()==P2RIGHT){
p2right=false;
p[1].unMoveRight();
}
if(TOTALPLAYERS>=3)
if(p[2]!=null)
if(e.getKeyCode()==P3LEFT){
p3left=false;
p[2].unMoveLeft();
}
else if(e.getKeyCode()==P3RIGHT){
p3right=false;
p[2].unMoveRight();
}
}
public void update()
{
}
}
public class PlayerThread extends Thread
{
public void run()
{
try
{
player.play();
}
catch(Exception e)
{
System.out.println("PlayerThread error: "+e.getMessage());
}
}
}
public void paintComponent(Graphics g){
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
public static void main(String[] args){
for (int k = 0;k<args.length;k++){
if (args[k].equals("-d")||args[k].equals("--debug"))
DEBUG=true;
}
GWar game = new GWar();
}
public void setupFrame(){
window = new JFrame ("GWar");
window.setResizable(false);
if(!fullscreen){
setWindowed();
}
else{
setFullscreen();
}
//System.out.println(frame.getWidth());
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(this);
//frame.pack();
window.setVisible(true);
Toolkit tk = Toolkit.getDefaultToolkit();
xScale=(double)(tk.getScreenSize().width)/WIDTH;
yScale=(double)(tk.getScreenSize().height)/HEIGHT;
if(!fullscreen){
xScale=1;
yScale=1;
}
mm.updateScreenInfo(xScale,yScale);
myImage = new BufferedImage((int)(WIDTH*xScale), (int)(HEIGHT*yScale), BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, (int)(WIDTH*xScale), (int)(HEIGHT*yScale));
}
private void setFullscreen(){
window.setUndecorated(true);
Toolkit tk = Toolkit.getDefaultToolkit();
window.setSize(tk.getScreenSize());
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if(gd.isFullScreenSupported()) {
gd.setFullScreenWindow(window);
System.out.println("Fulscreen Supported");
}
else
System.out.println("Fullscreen not supported");
fullscreen=true;
}
private void setWindowed(){
window.setUndecorated(false);
window.setVisible(true);
Insets i = window.getInsets();
//System.out.println(i.top+","+i.left+","+i.bottom+","+i.right);
//System.out.println("Width:"+WIDTH);
window.setVisible(false);
window.setSize(WIDTH+i.left+i.right,HEIGHT+i.top+i.bottom);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if(gd.isFullScreenSupported()) {
gd.setFullScreenWindow(null); //unfullscreen
}
fullscreen=false;
}
public void toggleFullscreen(){
window.setVisible(false);
window=null;
fullscreen = !fullscreen;
setupFrame();
window.requestFocus();
}
}