-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
388 lines (302 loc) · 8.77 KB
/
bundle.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
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
var GameView = __webpack_require__(1);
document.addEventListener("DOMContentLoaded", function() {
let canvas = document.getElementById("game-canvas");
let ctx = canvas.getContext("2d");
let view = new GameView(ctx);
view.start();
});
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
var Game = __webpack_require__(2);
function GameView(ctx) {
this.game = new Game();
this.canvas = ctx;
}
GameView.prototype.start = function() {
const view = this;
this.bindKeyHandlers();
window.setInterval(function() {
view.game.step();
view.game.draw(view.canvas);
}, 20);
};
GameView.prototype.bindKeyHandlers = function () {
let game = this.game;
key('w', function() {
game.ship.power([0,-1]);
});
key('a', function() {game.ship.power([-1,0])});
key('s', function() {game.ship.power([0,1])});
key('d', function() {game.ship.power([1,0])});
key('space', function() {game.ship.fireBullet()});
};
module.exports = GameView;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
var Util = __webpack_require__(3);
var Asteroid = __webpack_require__(4);
var Bullet = __webpack_require__(6);
var Ship = __webpack_require__(7);
function Game() {
this.DIM_X = 1000;
this.DIM_Y = 800;
const NUM_ASTEROIDS = 8;
this.asteroids = [];
this.bullets = [];
while (this.asteroids.length < NUM_ASTEROIDS) {
this.addAsteroids();
}
let options = {
pos: [this.DIM_X/2, this.DIM_Y/2],
game: this
};
this.ship = new Ship(options);
}
Game.prototype.allObjects = function() {
let allOfThem = this.asteroids.concat(this.ship).concat(this.bullets);
return allOfThem;
};
Game.prototype.addAsteroids = function() {
let options = {
pos: this.randomPosition(),
game: this
};
this.asteroids.push(new Asteroid(options));
};
Game.prototype.randomPosition = function() {
let x = Math.floor(Math.random() * this.DIM_X);
let y = Math.floor(Math.random() * this.DIM_Y);
return [x, y];
};
Game.prototype.draw = function(ctx) {
ctx.clearRect(0,0,this.DIM_X,this.DIM_Y);
this.allObjects().forEach(function(object) {
object.draw(ctx);
});
};
Game.prototype.moveObjects = function() {
this.allObjects().forEach(function(object) {
object.move();
});
};
Game.prototype.wrap = function(pos) {
pos[0] = pos[0] % this.DIM_X;
pos[1] = pos[1] % this.DIM_Y;
if (pos[0] < 0) {
pos[0] = this.DIM_X + pos[0];
}
if (pos[1] < 0) {
pos[1] = this.DIM_Y + pos[1];
}
return pos;
};
Game.prototype.checkCollisions = function() {
let objects = this.allObjects();
objects.forEach(function(object, index) {
objects.forEach(function(object2, index2) {
if(index !== index2 && object.isCollidedWith(object2)){
object.collideWith(object2);
}
});
});
};
Game.prototype.step = function() {
this.moveObjects();
this.checkCollisions();
};
Game.prototype.remove = function(object){
object.pos = [undefined, undefined];
};
Game.prototype.isOutOfBounds = function(pos) {
if (pos[0] < 0 || pos[0] > this.DIM_X) {
return true;
}
if (pos[1] < 0 || pos[1] > this.DIM_Y) {
return true;
}
return false;
};
module.exports = Game;
/***/ },
/* 3 */
/***/ function(module, exports) {
const Util = {
inherits (childClass, parentClass) {
function Surrogate(){ }
Surrogate.prototype = parentClass.prototype;
childClass.prototype = new Surrogate();
childClass.prototype.constructor = childClass;
},
randomVec(length) {
let a = (Math.random()*(length * 2)) - length;
let b = Math.sqrt(Math.pow(length, 2) - Math.pow(a, 2));
let dir = Math.floor(Math.random() * 2);
if (dir === 0) {
b = b * -1;
}
return [a, b];
},
dist(pos1, pos2){
let a = pos1[0] - pos2[0];
let b = pos1[1] - pos2[1];
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
};
module.exports = Util;
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
var MovingObject = __webpack_require__(5);
var Util = __webpack_require__(3);
var Ship = __webpack_require__(7);
const Bullet = __webpack_require__(6);
function Asteroid(options) {
options.color = "#808080";
options.radius = 25;
options.vel = Util.randomVec(5);
MovingObject.call(this, options);
}
Util.inherits(Asteroid, MovingObject);
Asteroid.prototype.collideWith = function (otherObject){
if (otherObject instanceof Ship) {
otherObject.relocate();
} else if (otherObject instanceof Bullet) {
this.game.remove(otherObject);
this.game.remove(this);
}
};
module.exports = Asteroid;
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
var Util = __webpack_require__(3);
function MovingObject(options) {
this.pos = options.pos;
this.vel = options.vel;
this.radius = options.radius;
this.color = options.color;
this.game = options.game;
this.isWrappable = true;
}
MovingObject.prototype.draw = function(ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(
this.pos[0],
this.pos[1],
this.radius,
0,
2*Math.PI
);
ctx.fill();
};
MovingObject.prototype.move = function() {
this.pos[0] += this.vel[0];
this.pos[1] += this.vel[1];
if (this.isWrappable) {
this.pos = this.game.wrap(this.pos);
} else if (this.game.isOutOfBounds(this.pos)) {
this.game.remove(this);
}
};
MovingObject.prototype.isCollidedWith = function(otherObject) {
let radiusSum = this.radius + otherObject.radius;
let distance = Util.dist(this.pos, otherObject.pos);
if (radiusSum > distance) {
return true;
}
return false;
};
MovingObject.prototype.collideWith = function(otherObject) {};
module.exports = MovingObject;
/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {
const MovingObject = __webpack_require__(5);
const Ship = __webpack_require__(7);
const Asteroid = __webpack_require__(4);
const Util = __webpack_require__(3);
function Bullet(options) {
options.color = "#FFFF00";
options.radius = 5;
MovingObject.call(this, options);
this.isWrappable = false;
}
Util.inherits(Bullet, MovingObject);
// Bullet.prototype.collideWith = function(otherObject){
// if (otherObject instanceof Asteroid) {
// this.game.remove(otherObject);
// this.game.remove(this);
// }
// };
module.exports = Bullet;
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
var MovingObject = __webpack_require__(5);
var Util = __webpack_require__(3);
var Bullet = __webpack_require__(6);
function Ship(options) {
options.color = "#FF0000";
options.radius = 15;
options.vel = [0, 0];
MovingObject.call(this, options);
}
Util.inherits(Ship, MovingObject);
Ship.prototype.relocate = function() {
this.pos = [this.game.DIM_X / 2, this.game.DIM_Y / 2];
this.vel = [0, 0];
};
Ship.prototype.power = function(impulse) {
this.vel[0] += impulse[0];
this.vel[1] += impulse[1];
};
Ship.prototype.fireBullet = function() {
let bulletVel = [this.vel[0] * 2, this.vel[1] * 2];
if (this.vel[0] === 0 && this.vel[1] === 0) {
bulletVel = [0, -2];
}
let bulletPos = [this.pos[0], this.pos[1]];
let options = {game: this.game, pos: bulletPos, vel: bulletVel };
let bullet = new Bullet(options);
this.game.bullets.push(bullet);
};
module.exports = Ship;
/***/ }
/******/ ]);