-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameengine.js
More file actions
135 lines (116 loc) · 3.63 KB
/
gameengine.js
File metadata and controls
135 lines (116 loc) · 3.63 KB
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
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
};
})();
function GameEngine() {
this.entities = [];
this.ctx = null;
this.click = null;
this.mouse = null;
this.wheel = null;
this.surfaceWidth = null;
this.surfaceHeight = null;
}
GameEngine.prototype.init = function (ctx) {
this.ctx = ctx;
this.surfaceWidth = this.ctx.canvas.width;
this.surfaceHeight = this.ctx.canvas.height;
this.timer = new Timer();
this.startInput();
console.log('game initialized');
}
GameEngine.prototype.start = function () {
console.log("starting game");
var that = this;
(function gameLoop() {
that.loop();
requestAnimFrame(gameLoop, that.ctx.canvas);
})();
}
GameEngine.prototype.startInput = function () {
console.log('Starting input');
var that = this;
this.ctx.canvas.addEventListener("keydown", function (e) {
if (String.fromCharCode(e.which) === ' ') that.space = true;
// console.log(e);
e.preventDefault();
}, false);
console.log('Input started');
}
GameEngine.prototype.addEntity = function (entity) {
console.log('added entity');
this.entities.push(entity);
}
GameEngine.prototype.draw = function () {
this.ctx.clearRect(0, 0, this.surfaceWidth, this.surfaceHeight);
this.ctx.save();
for (var i = 0; i < this.entities.length; i++) {
this.entities[i].draw(this.ctx);
}
this.ctx.restore();
}
GameEngine.prototype.update = function () {
var entitiesCount = this.entities.length;
for (var i = 0; i < entitiesCount; i++) {
var entity = this.entities[i];
entity.update();
}
}
GameEngine.prototype.loop = function () {
this.clockTick = this.timer.tick();
this.update();
this.draw();
this.space = null;
}
function Timer() {
this.gameTime = 0;
this.maxStep = 0.05;
this.wallLastTimestamp = 0;
}
Timer.prototype.tick = function () {
var wallCurrent = Date.now();
var wallDelta = (wallCurrent - this.wallLastTimestamp) / 1000;
this.wallLastTimestamp = wallCurrent;
var gameDelta = Math.min(wallDelta, this.maxStep);
this.gameTime += gameDelta;
return gameDelta;
}
function Entity(game, x, y) {
this.game = game;
this.x = x;
this.y = y;
this.removeFromWorld = false;
}
Entity.prototype.update = function () {
}
Entity.prototype.draw = function (ctx) {
if (this.game.showOutlines && this.radius) {
this.game.ctx.beginPath();
this.game.ctx.strokeStyle = "green";
this.game.ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
this.game.ctx.stroke();
this.game.ctx.closePath();
}
}
Entity.prototype.rotateAndCache = function (image, angle) {
var offscreenCanvas = document.createElement('canvas');
var size = Math.max(image.width, image.height);
offscreenCanvas.width = size;
offscreenCanvas.height = size;
var offscreenCtx = offscreenCanvas.getContext('2d');
offscreenCtx.save();
offscreenCtx.translate(size / 2, size / 2);
offscreenCtx.rotate(angle);
offscreenCtx.translate(0, 0);
offscreenCtx.drawImage(image, -(image.width / 2), -(image.height / 2));
offscreenCtx.restore();
//offscreenCtx.strokeStyle = "red";
//offscreenCtx.strokeRect(0,0,size,size);
return offscreenCanvas;
}