-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUBBLETROUBLE.js
183 lines (115 loc) · 3.48 KB
/
BUBBLETROUBLE.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
// =========
// ASTEROIDS
// =========
/*
A sort-of-playable version of the classic arcade game.
HOMEWORK INSTRUCTIONS:
You have some "TODO"s to fill in again, particularly in:
spatialManager.js
But also, to a lesser extent, in:
Rock.js
Wire.js
player.js
...Basically, you need to implement the core of the spatialManager,
and modify the Rock/Wire/player to register (and unregister)
with it correctly, so that they can participate in collisions.
*/
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
var g_canvas = document.getElementById("myCanvas");
var g_ctx = g_canvas.getContext("2d");
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// ====================
// CREATE INITIAL playerS
// ====================
function createInitialplayers() {
entityManager.generateplayer({
cx : 200,
cy : 200
});
}
// =============
// GATHER INPUTS
// =============
function gatherInputs() {
// Nothing to do here!
// The event handlers do everything we need for now.
}
// =================
// UPDATE SIMULATION
// =================
// We take a very layered approach here...
//
// The primary `update` routine handles generic stuff such as
// pausing, single-step, and time-handling.
//
// It then delegates the game-specific logic to `updateSimulation`
// GAME-SPECIFIC UPDATE LOGIC
function updateSimulation(du) {
processDiagnostics();
entityManager.update(du);
// Prevent perpetual firing!
eatKey(player.prototype.KEY_FIRE);
}
// GAME-SPECIFIC DIAGNOSTICS
var g_allowMixedActions = true;
var g_useGravity = false;
var g_useAveVel = true;
var g_renderSpatialDebug = false;
var KEY_MIXED = keyCode('M');;
var KEY_GRAVITY = keyCode('G');
var KEY_AVE_VEL = keyCode('V');
var KEY_SPATIAL = keyCode('X');
var KEY_HALT = keyCode('H');
var KEY_RESET = keyCode('R');
function processDiagnostics() {
if (eatKey(KEY_MIXED))
g_allowMixedActions = !g_allowMixedActions;
if (eatKey(KEY_GRAVITY)) g_useGravity = !g_useGravity;
if (eatKey(KEY_AVE_VEL)) g_useAveVel = !g_useAveVel;
if (eatKey(KEY_SPATIAL)) g_renderSpatialDebug = !g_renderSpatialDebug;
if (eatKey(KEY_HALT)) entityManager.haltplayers();
if (eatKey(KEY_RESET)) entityManager.resetplayers();
}
// =================
// RENDER SIMULATION
// =================
// We take a very layered approach here...
//
// The primary `render` routine handles generic stuff such as
// the diagnostic toggles (including screen-clearing).
//
// It then delegates the game-specific logic to `gameRender`
// GAME-SPECIFIC RENDERING
function renderSimulation(ctx) {
entityManager.render(ctx);
if (g_renderSpatialDebug) spatialManager.render(ctx);
}
// =============
// PRELOAD STUFF
// =============
var g_images = {};
function requestPreloads() {
var requiredImages = {
player : "1.png",
rock : "https://notendur.hi.is/~pk/308G/images/rock.png",
bg : "bg.png",
wire : "img/wire.png"
};
imagesPreload(requiredImages, g_images, preloadDone);
}
var g_sprites = {};
function preloadDone() {
g_sprites.player = new Sprite(g_images.player);
g_sprites.rock = new Sprite(g_images.rock);
g_sprites.bg = new Sprite(g_images.bg);
g_sprites.Wire = new Sprite(g_images.wire);
entityManager.init();
createInitialplayers();
main.init();
}
// Kick it off
requestPreloads();