-
Notifications
You must be signed in to change notification settings - Fork 0
/
TankGame.pde
131 lines (107 loc) · 3.04 KB
/
TankGame.pde
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
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import de.lighti.clipper.*;
import processing.sound.*;
final boolean DRAW_BOUNDING_BOXES = false;
final int DEFAULT_WIDTH = 960;
final int DEFAULT_HEIGHT = 540;
final int TICK_RATE = 100;
final int TICK_MS = 1000 / TICK_RATE;
final float GRAVITY = 0.005;
Sound sound;
SoundFile SOUND_GRENADE_SHOOT;
SoundFile SOUND_ROCKET_SHOOT;
SoundFile SOUND_GRENADE_EXPLODE;
SoundFile SOUND_ROCKET_EXPLODE;
long lastTickMillis = 0;
float tickRate = 0;
int lastTickDuration = 0;
InputManager inputs;
LevelManager levels;
EntityManager entities;
ParticleManager particles;
Camera camera;
PFont fontRegular;
PFont fontBold;
void settings() {
size(DEFAULT_WIDTH, DEFAULT_HEIGHT, P2D);
pixelDensity(displayDensity());
PJOGL.setIcon("textures/tank.png");
}
void setup() {
sound = new Sound(this);
sound.volume(0.05);
SOUND_GRENADE_SHOOT = new SoundFile(this, "sound/grenade-shoot.wav");
SOUND_ROCKET_SHOOT = new SoundFile(this, "sound/rocket-shoot.wav");
SOUND_GRENADE_EXPLODE = new SoundFile(this, "sound/grenade-explode.wav");
SOUND_ROCKET_EXPLODE = new SoundFile(this, "sound/rocket-explode.wav");
frameRate(1000);
surface.setTitle("Untitled Tank Game");
cursor(loadImage("textures/xhair.png"), 16, 16);
// surface.setResizable(true);
colorMode(HSB, 360.0, 1.0, 1.0, 1.0);
fontRegular = createFont("fonts/barlow-medium.ttf", 64);
fontBold = createFont("fonts/barlow-extrabold.ttf", 64);
textFont(fontRegular);
inputs = new InputManager();
levels = new LevelManager();
entities = new EntityManager();
particles = new ParticleManager();
camera = new Camera(new PVector(0, 0), DEFAULT_WIDTH * 2);
TimerTask tickTask = new TimerTask() {
void run() {
long millis = System.currentTimeMillis();
tick();
lastTickDuration = (int)(System.currentTimeMillis() - millis);
tickRate = (float)(1000 / (millis - lastTickMillis == 0 ? 1 : millis - lastTickMillis));
lastTickMillis = millis;
}
};
new Timer().scheduleAtFixedRate(tickTask, 0L, (long)TICK_MS);
levels.loadLevel("test");
}
void tick() {
inputs.OnTick();
levels.OnTick();
entities.OnTick();
particles.OnTick();
camera.OnTick();
}
void draw() {
background(#aad9dd);
camera.OnDraw();
String debugText = "fps: " + (int)frameRate + "\n" +
"tps: " + (int)tickRate + " (" + lastTickDuration + "ms)\n" +
"entities: " + entities.getCount() + "\n" +
"particles: " + particles.getCount();
push();
textSize(16);
textLeading(16);
text(debugText, 4, 16);
pop();
}
void keyPressed() {
entities.OnKeyPressed();
inputs.OnKeyPressed();
}
void keyReleased() {
entities.OnKeyReleased();
inputs.OnKeyReleased();
}
void mousePressed() {
entities.OnMousePressed();
}
void mouseMoved() {
entities.OnMouseMoved();
}