-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
59 lines (49 loc) · 1.32 KB
/
Game.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
import java.util.*;
public class Game extends Simulation implements CollisionTriggerListener
{
static final int WIDTH = 1500;
static final int HEIGHT = 750;
public static void main(String[] args) throws InterruptedException
{
Game app = new Game();
}
public Game() throws InterruptedException
{
super(WIDTH, HEIGHT);
Random random = new Random();
for(int x = 100; x < WIDTH - 100; x += 1)
{
Node node = new Node(x + randomInt(random, -10, 10), 100 + randomInt(random, -90, 0));
nodes.add(node);
}
for(int x = 0; x < WIDTH; x += 25)
{
for(int y = 200; y < HEIGHT - 100; y += 25)
{
Collider col = new CircleCollider(x + (y % 2 == 0 ? 0 : 12) + randomInt(random, -5, 5), y, randomInt(random, 3, 14));
colliders.add(col);
}
}
Collider bottom = new BoxCollider(WIDTH / 2, HEIGHT - 5, WIDTH, 10, 0);
bottom.addTriggerListener(this);
colliders.add(bottom);
// main simulation loop
while(true)
{
simulate();
repaint();
Thread.sleep(5);
}
}
public void onCollisionTriggered(Collider col, Node node)
{
double vel = node.oldY - node.y;
node.y = 5;
node.oldY = node.y + vel;
}
// returns a random integer between the min and the max based on a seed (exclusive)
public static int randomInt(Random random, int min, int max)
{
return random.nextInt(max - min) + min;
}
}