-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.js
45 lines (42 loc) · 1.2 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
var canvas = null;
var context = null;
var backgroundColor = "#eee";
var world = new World();
function World()
{
this.canvas = null;
this.context = null;
this.backgroundColor = "#ccc";
this.camera = new Camera(0, 0);
}
World.prototype.start = function(canvasId)
{
this.canvas = canvas = document.getElementById(canvasId);
if (this.canvas == null)
throw "Canvas doesn't exist!";
if(!this.canvas.getContext)
throw "Cannot retrieve canvas context!";
//Need the lambda or else this.render() will have the Window instance as "this" inside the function scope
setInterval(() => this.render(), 16);
this.context = context = this.canvas.getContext('2d');
Mouse.start(this.canvas);
Keyboard.start();
};
World.prototype.clearCanvas = function(color)
{
if(color == null)
{
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
return;
}
this.context.fillStyle = color.toString();
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
};
World.prototype.render = function()
{
Entity.updateAll();
this.clearCanvas(this.backgroundColor);
Entity.renderAll();
Mouse.update();
Keyboard.update();
};