forked from KilledByAPixel/Drive13K
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.js
230 lines (200 loc) · 6.91 KB
/
game.js
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
'use strict';
showMap = 0;
speakEnable = 1;
debugInfo = 0;
//debugTile = vec3(6,0)
//debugGenerativeCanvas = 1
soundVolume = .3;
const engineName = 'js13kRace';
const testDrive = 0;
const enableTexture = 1;
const enableLighting = 1;
const pixelate = 0;
const canvasFixedSize = 0;
const frameRate = 60;
const timeDelta = 1/60;
// track settings
const trackWidth = 1000; // how wide is track
const trackEnd = 1e4; // how many sections until end of the track
const trackSegmentLength = 100; // length of each segment
const drawDistance = 1e3; // how many track segments to draw in front
const sceneryDrawDistance = 500; // how far to draw scenery
const cameraPlayerOffset = vec3(0,700,1050);
const checkpointTrackSegments = 3e3;
const checkpointDistance = checkpointTrackSegments*trackSegmentLength; // how far between checkpoints
const checkpointMaxDifficulty = 9; // how many checkpoints before max difficulty
const startCheckpointTime = 30;
let quickStart = 0;
let attractMode = 1;
let mainCanvasSize = pixelate ? vec3(640, 420) : vec3(1280, 720);
let mainCanvas, mainContext;
let time, frame, frameTimeLastMS, averageFPS, frameTimeBufferMS;
let attractVehicleSpawnTimer;
let paused;
let checkpointTimeLeft, startCountdown, startCountdownTimer, gameOverTimer, nextCheckpointDistance;
///////////////////////////////
// game variables
let cameraPos, cameraRot, cameraOffset, cameraTrackInfo;
let worldHeading, mouseControl;
let track, vehicles, playerVehicle;
///////////////////////////////
function gameInit()
{
if (quickStart)
attractMode = 0;
debug && debugInit();
glInit();
const styleBody = 'background-color:#111;margin:0';
const styleCanvas = 'position:absolute;' + // position
'top:50%;left:50%;transform:translate(-50%,-50%);' + // center
(pixelate?' image-rendering: pixelated':'');
document.body.style = styleBody;
document.body.appendChild(mainCanvas = document.createElement('canvas'));
mainContext = mainCanvas.getContext('2d');
mainContext.imageSmoothingEnabled = !pixelate;
glContext.imageSmoothingEnabled = !pixelate;
glCanvas.style.cssText = mainCanvas.style.cssText = styleCanvas;
drawInit();
inputInit();
initSounds()
initGenerative();
initHUD();
buildTrack();
gameStart();
gameUpdate();
}
function gameStart()
{
attractVehicleSpawnTimer = time = frame = frameTimeLastMS = averageFPS = frameTimeBufferMS =
worldHeading = cameraOffset = checkpointTimeLeft = 0;
startCountdown = quickStart ? 0 : 4;
checkpointTimeLeft = startCheckpointTime;
nextCheckpointDistance = checkpointDistance;
startCountdownTimer = new Timer;
gameOverTimer = new Timer;
cameraPos = vec3();
cameraRot = vec3();
vehicles = [];
playerVehicle = new PlayerVehicle(2e3, hsl(0,.8,.5));
vehicles.push(playerVehicle);
for(let i = 10; i--;)
vehicles.push(new Vehicle(5e3*i+3e3, hsl(rand(),.8,.5)));
}
function gameUpdateInternal()
{
if (attractMode)
{
// update attract mode
if (vehicles.length < 10 && attractVehicleSpawnTimer-- < 0)
{
vehicles.push(new Vehicle(playerVehicle.pos.z-1e3, hsl(rand(),.8,.5)));
attractVehicleSpawnTimer = randInt(100,300);
}
if (mouseWasPressed(0))
{
attractMode = 0;
sound_start.play();
gameStart();
}
}
else
{
if (startCountdown > 0 && !startCountdownTimer.active())
{
--startCountdown;
speak(startCountdown || 'GO!' );
startCountdownTimer.set(1);
}
if (keyWasPressed('Escape'))
{
attractMode = 1;
sound_start.play();
gameStart();
}
if (gameOverTimer > 1 && mouseWasPressed(0) || gameOverTimer > 9)
{
attractMode = 1;
gameStart();
}
if (checkpointTimeLeft > 0 && startCountdown == 0)
{
checkpointTimeLeft -= timeDelta;
if (checkpointTimeLeft <= 0)
{
speak('GAME OVER');
gameOverTimer.set();
checkpointTimeLeft = 0;
}
}
}
if (keyWasPressed('KeyR'))
{
attractMode = 0;
sound_start.play();
gameStart();
}
for(const v of vehicles)
v.update();
}
function gameUpdate(frameTimeMS=0)
{
if (canvasFixedSize)
{
// clear canvas and set fixed size
mainCanvas.width = mainCanvasSize.x;
mainCanvas.height = mainCanvasSize.y;
}
else
{
mainCanvasSize = vec3(innerWidth, innerHeight);
if (pixelate)
{
const s = 2;
mainCanvasSize.x = mainCanvasSize.x / s | 0;
mainCanvasSize.y = mainCanvasSize.y / s | 0;
}
mainCanvas.width = mainCanvasSize.x;
mainCanvas.height = mainCanvasSize.y;
}
// fit to window by adding space on top or bottom if necessary
const aspect = innerWidth / innerHeight;
const fixedAspect = mainCanvas.width / mainCanvas.height;
mainCanvas.style.width = glCanvas.style.width = aspect < fixedAspect ? '100%' : '';
mainCanvas.style.height = glCanvas.style.height = aspect < fixedAspect ? '' : '100%';
// update time keeping
let frameTimeDeltaMS = frameTimeMS - frameTimeLastMS;
frameTimeLastMS = frameTimeMS;
const debugSpeedUp = debug && keyIsDown('Equal'); // +
const debugSpeedDown = debug && keyIsDown('Minus'); // -
if (debug) // +/- to speed/slow time
frameTimeDeltaMS *= debugSpeedUp ? 5 : debugSpeedDown ? .2 : 1;
averageFPS = lerp(.05, averageFPS, 1e3/(frameTimeDeltaMS||1));
frameTimeBufferMS += paused ? 0 : frameTimeDeltaMS;
frameTimeBufferMS = min(frameTimeBufferMS, 50); // clamp in case of slow framerate
// apply time delta smoothing, improves smoothness of framerate in some browsers
let deltaSmooth = 0;
if (frameTimeBufferMS < 0 && frameTimeBufferMS > -9)
{
// force an update each frame if time is close enough (not just a fast refresh rate)
deltaSmooth = frameTimeBufferMS;
frameTimeBufferMS = 0;
}
// update multiple frames if necessary in case of slow framerate
inputUpdate();
for (;frameTimeBufferMS >= 0; frameTimeBufferMS -= 1e3 / frameRate)
{
// increment frame and update time
time = frame++ / frameRate;
gameUpdateInternal();
}
// add the time smoothing back in
frameTimeBufferMS += deltaSmooth;
trackPreRender();
glPreRender();
drawScene();
drawHUD();
drawDebug();
inputUpdatePost();
requestAnimationFrame(gameUpdate);
}
gameInit();