-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.js
68 lines (55 loc) · 1.5 KB
/
World.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
function World(canvas, objects) {
var self = this;
self.context = canvas.getContext('2d');
self.objects = objects;
self.timestamp = null;
self.run = run;
self.tick = tick;
self.resize = resize;
self.plot = plot;
self.tickObjects = tickObjects;
self.resolveCollisions = resolveCollisions;
function run() {
requestAnimationFrame(this.tick.bind(this));
}
function tick(timestamp) {
var dt = timestamp - (this.timestamp || timestamp);
this.timestamp = timestamp;
this.resize();
this.plot();
this.tickObjects(dt);
this.resolveCollisions();
this.run();
}
function resize() {
var canvas = this.context.canvas;
if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight) {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
}
}
function plot() {
var canvas = this.context.canvas;
this.context.clearRect(0, 0, canvas.width, canvas.height);
this.objects.forEach(function (object) {
object.plot(this.context);
}, this);
}
function tickObjects(dt) {
this.objects.forEach(function (object) {
object.tick(dt);
});
}
function resolveCollisions() {
var canvas = this.context.canvas;
var rectangle = {x: 0, y: 0, width: canvas.width, height: canvas.height};
for (var i = 0; i < objects.length; i++) {
var object = objects[i];
object.resolveRectangleCollision(rectangle);
for (var j = i + 1; j < objects.length; j++) {
object.resolveBallCollision(objects[j]);
}
}
}
}
export default World;