-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacteriveDriver.java
More file actions
456 lines (451 loc) · 13.7 KB
/
BacteriveDriver.java
File metadata and controls
456 lines (451 loc) · 13.7 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
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
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
import java.util.ArrayList;
public class BacteriveDriver extends JFrame
{
private BacterivePanel gamePanel=new BacterivePanel();
private boolean running=true,paused=false;
private int ups,fps;
private BacteriveDriver()
{
super("Bacterive");
pack();
setSize(800,800);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(gamePanel);
setVisible(true);
gamePanel.requestFocus();
runGameLoop();
}
private void runGameLoop()
{
Thread game=
new Thread()
{
@Override
public void run()
{
gameLoop();
}
};
game.run();
}
private void gameLoop()
{
final int TARGET_UPS=30;
final long TARGET_UPDATE_TIME=1000000000/TARGET_UPS;
final int TARGET_FPS=60;
final long TARGET_FRAME_TIME=1000000000/TARGET_FPS;
long prevFrameTime=System.nanoTime();
long accumulator=0;
//for finding ups
int updates=0;
long runningUpdateTime=0;
long prevUpdateTime=prevFrameTime;
//for finding fps
int frames=0;
long runningFrameTime=0;
while(running)
{
long now=System.nanoTime();
accumulator+=now-prevFrameTime;
runningFrameTime+=now-prevFrameTime;
frames++;
while(accumulator>TARGET_UPDATE_TIME)
{
gamePanel.update();
accumulator-=TARGET_UPDATE_TIME;
runningUpdateTime+=System.nanoTime()-prevUpdateTime;
updates++;
prevUpdateTime=System.nanoTime();
}
double interpolation=(double)(System.nanoTime()-prevFrameTime)/TARGET_UPDATE_TIME;
//System.out.println(interpolation);
if(paused)
interpolation=0;
if(runningUpdateTime>=1000000000)
{
ups=updates;
updates=0;
runningUpdateTime=0;
}
if(runningFrameTime>=1000000000)
{
fps=frames;
frames=0;
runningFrameTime=0;
}
gamePanel.render(interpolation);
prevFrameTime=now;
while(now-prevFrameTime<TARGET_FRAME_TIME && now-prevFrameTime<TARGET_UPDATE_TIME)
{
Thread.yield();
now=System.nanoTime();
}
}
}
public static void main(String[]arg)
{
new BacteriveDriver();
}
private class BacterivePanel extends JPanel
{
private ArrayList<Bacterium> bacteria=new ArrayList<Bacterium>(0);
private int fieldX=-750,fieldY=-1000,fieldWidth=1500,fieldHeight=2000;
private int MAX_BACTERIA=50;
private final String MOVE_UP="move up"
,MOVE_DOWN="move down"
,MOVE_LEFT="move left"
,MOVE_RIGHT="move right"
,STOP_VERTICAL="stop vertical"
,STOP_HORIZONTAL="stop horizontal"
,EXIT="exit"
,PAUSE="pause";
private BufferedImage tile;
private BacterivePanel()
{
try{tile=ImageIO.read(new File("Sprites/BackgroundTile.png"));}
catch(IOException e){e.printStackTrace();}
initBacteria();
setBackground(Color.WHITE);
setKeyBindings();
setFocusable(true);
}
private void initBacteria()
{
bacteria.add(new Player());
for(int i=0;i<100;i++)
{
Enemy temp=new Enemy((int)(Math.random()*(fieldWidth+1))+fieldX,(int)(Math.random()*(fieldHeight+1))+fieldY,bacteria.get(0).size);
while(isCollision(temp,bacteria.get(0)))
{
temp.x+=100;
temp.y+=100;
}
bacteria.add(temp);
}
}
private void gimp(String key,String name)
{
getInputMap().put(KeyStroke.getKeyStroke(key),name);
}
private void gamp(String name,AbstractAction action)
{
getActionMap().put(name,action);
}
private void setKeyBindings()
{
gimp("UP",MOVE_UP);
gimp("W",MOVE_UP);
gimp("DOWN",MOVE_DOWN);
gimp("S",MOVE_DOWN);
gimp("LEFT",MOVE_LEFT);
gimp("A",MOVE_LEFT);
gimp("RIGHT",MOVE_RIGHT);
gimp("D",MOVE_RIGHT);
gimp("ESCAPE",EXIT);
gimp("P",PAUSE);
gimp("released UP",STOP_VERTICAL);
gimp("released W",STOP_VERTICAL);
gimp("released DOWN",STOP_VERTICAL);
gimp("released S",STOP_VERTICAL);
gimp("released LEFT",STOP_HORIZONTAL);
gimp("released A",STOP_HORIZONTAL);
gimp("released RIGHT",STOP_HORIZONTAL);
gimp("released D",STOP_HORIZONTAL);
gamp(MOVE_UP,new MoveAction(0));
gamp(MOVE_RIGHT,new MoveAction(1));
gamp(MOVE_DOWN,new MoveAction(2));
gamp(MOVE_LEFT,new MoveAction(3));
gamp(EXIT,new MenuAction(0));
gamp(PAUSE,new MenuAction(1));
gamp(STOP_VERTICAL,new MoveAction(4));
gamp(STOP_HORIZONTAL,new MoveAction(5));
}
private void detectCollisions()
{
for(int i=0;i<bacteria.size();i++)
{
Bacterium b1=bacteria.get(i);
for(int j=i+1;j<bacteria.size();j++)
{
Bacterium b2=bacteria.get(j);
if(isCollision(b1,b2))
if(b1.size>b2.size)
{
b1.size+=b2.size/10;
//b1.maxSpeed++;
b1.x-=b2.size/20;
b1.y-=b2.size/20;
bacteria.remove(b2);
}
else if(b1 instanceof Player && b1.size!=b2.size)
{
running=false;
}
else if(b1.size<b2.size)
{
b2.size+=b1.size/10;
//b2.maxSpeed++;
b2.x-=b1.size/20;
b2.y-=b1.size/20;
bacteria.remove(b1);
}
}
}
}
private boolean isCollision(Bacterium b1,Bacterium b2)
{
return Math.sqrt(Math.pow(b2.getCenterX()-b1.getCenterX(),2)+Math.pow(b2.getCenterY()-b1.getCenterY(),2))<b1.size/2+b2.size/2;
}
private void spawnEnemies()
{
MAX_BACTERIA=fieldHeight;
if(bacteria.size()<MAX_BACTERIA)
{
Enemy temp=new Enemy((int)(Math.random()*(fieldWidth+1))+fieldX,(int)(Math.random()*(fieldHeight+1))+fieldY,bacteria.get(0).size);
while(isCollision(temp,bacteria.get(0)))
{
temp.x+=100;
temp.y+=100;
}
bacteria.add(temp);
}
}
private void update()
{
for(Bacterium bacterium:bacteria)
{
int targetSize=bacterium.size*5;
if(targetSize>fieldWidth)
{
fieldWidth=targetSize;
fieldHeight=targetSize;
}
}
if(bacteria.get(0).size>500)
for(Bacterium bacterium:bacteria)
bacterium.size/=2;
spawnEnemies();
for(Bacterium bacterium:bacteria)
bacterium.update(fieldX,fieldY,fieldWidth,fieldHeight);
detectCollisions();
}
private void render(double interpolation)
{
for(Bacterium bacterium:bacteria)
bacterium.interpolate(interpolation);
repaint();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Bacterium p=bacteria.get(0);
int tileWidth=tile.getWidth();
int tileHeight=tile.getHeight();
for(int x=-tileWidth;x<getWidth()+tileWidth;x+=tileWidth)
for(int y=-tileHeight;y<getHeight()+tileHeight;y+=tileWidth)
g.drawImage(tile,x-p.getCenterX()%tileWidth,y-p.getCenterY()%tileHeight,tileWidth,tileHeight,null);
int translateX=getWidth()/2-p.getCenterX();
int translateY=getHeight()/2-p.getCenterY();
g.translate(translateX,translateY);
for(Bacterium bacterium:bacteria)
bacterium.draw(g);
g.setColor(Color.BLACK);
g.drawRect(fieldX,fieldY,fieldWidth,fieldHeight);
g.translate(-translateX,-translateY);
g.setFont(new Font(Font.DIALOG_INPUT,Font.BOLD,20));
g.drawString("UPS: "+ups,getWidth()-100,getHeight()-50);
g.drawString("FPS: "+fps,getWidth()-100,getHeight()-25);
g.setFont(new Font(Font.DIALOG_INPUT,Font.BOLD,30));
g.drawString("Size: "+p.size,20,getHeight()-25);
if(!running)
{
g.setColor(Color.RED);
g.setFont(new Font(Font.DIALOG_INPUT,Font.BOLD,75));
g.drawString("YOU LOSE",getWidth()/2-175,100);
}
else if(paused)
{
g.setColor(Color.RED);
g.setFont(new Font(Font.DIALOG_INPUT,Font.BOLD,75));
g.drawString("PAUSED",getWidth()/2-135,100);
}
}
private class MoveAction extends AbstractAction
{
private int direction;
private MoveAction(int dir)
{
direction=dir;
}
@Override
public void actionPerformed(ActionEvent e)
{
Player p=(Player)bacteria.get(0);
if(direction<4)
{
p.move(direction);
p.update(fieldX,fieldY,fieldWidth,fieldHeight);
}
else
p.stop(direction);
}
}
}
private class MenuAction extends AbstractAction
{
int code;
private MenuAction(int key)
{
code=key;
}
@Override
public void actionPerformed(ActionEvent e)
{
if(code==0)
System.exit(1);
else if(code==1)
paused=!paused;
}
}
private class Bacterium
{
protected final int minSpeed;
protected int x,y,xSpeed,ySpeed,size,maxSpeed;
private Color color;
private Bacterium(int xPos,int yPos,int diameter,int minS)
{
x=xPos;
y=yPos;
size=diameter;
maxSpeed=(int)(1.0/size*300);
int rand=(int)(Math.random()*5);
if(rand==0)
color=Color.BLUE;
else if(rand==1)
color=Color.RED;
else if(rand==2)
color=Color.BLACK;
else if(rand==3)
color=Color.GREEN;
else
color=Color.ORANGE;
minSpeed=minS;
}
private void interpolate(double interpolation)
{
x+=xSpeed*interpolation;
y+=ySpeed*interpolation;
}
protected void update(int fX,int fY,int fW,int fH)
{
if(this instanceof Player)
maxSpeed=(int)(1.0/size*500);
else
maxSpeed=(int)(1.0/size*300);
if(maxSpeed<minSpeed)
maxSpeed=minSpeed;
if(x+xSpeed<=fX)
{
x=fX;
xSpeed=0;
}
else if(x+xSpeed+size>=fX+fW)
{
x=fX+fW-size;
xSpeed=0;
}
if(y+ySpeed<=fY)
{
y=fY;
ySpeed=0;
}
else if(y+ySpeed+size>=fY+fH)
{
y=fY+fH-size;
ySpeed=0;
}
}
protected void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x,y,size,size);
}
private int getCenterX()
{
return x+size/2;
}
private int getCenterY()
{
return y+size/2;
}
}
private class Player extends Bacterium
{
private Player()
{
super(0,0,50,5);
}
private void move(int direction)
{
if(direction==0)
ySpeed=-maxSpeed;
else if(direction==1)
xSpeed=maxSpeed;
else if(direction==2)
ySpeed=maxSpeed;
else if(direction==3)
xSpeed=-maxSpeed;
}
private void stop(int direction)
{
if(direction==4)
ySpeed=0;
else if(direction==5)
xSpeed=0;
}
}
private class Enemy extends Bacterium
{
private Enemy(int xPos,int yPos,int pSize)
{
super(xPos,yPos,(int)(Math.random()*pSize/2)+10,2);
}
protected void update(int fX,int fY,int fW,int fH)
{
int xS=xSpeed;
int yS=ySpeed;
super.update(fX,fY,fW,fH);
if(xS!=xSpeed || yS!=ySpeed || (xSpeed==0 && ySpeed==0))
{
int xDir=(int)(Math.random()*3);
int yDir=(int)(Math.random()*3);
if(xDir==0)
xSpeed=0;
else if(xDir==1)
xSpeed=-maxSpeed;
else
xSpeed=maxSpeed;
if(yDir==0)
ySpeed=0;
else if(yDir==1)
ySpeed=-maxSpeed;
else
ySpeed=maxSpeed;
}
}
}
}