-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGame.js
executable file
·243 lines (192 loc) · 5.24 KB
/
Game.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
(function(window, document) {
var tick, tickID,
prev = (+ new Date),
current = (+ new Date),
curTime = Date.now(),
FPS = 60,
skipTicks = 1000 / FPS,
nextGameTick = (new Date).getTime(),
frame = 0,
stagePos;
function addEvent(ctx, obj, type, callback) {
if (arguments.length === 3) {
callback = type;
type = obj;
obj = document;
}
//save anonymous function to be able to remove
var afn = function (e) {
var e = e || window.event;
callback.call(ctx, e)
};
if (obj.attachEvent) { //IE
obj.attachEvent('on' + type, afn);
} else { //Everyone else
obj.addEventListener(type, afn, false);
}
}
function Point(x, y) {
this.x = x;
this.y = y;
}
function Rect(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
function clone(obj){
var temp = {}; // changed
for(var key in obj)
temp[key] = obj[key];
return temp;
}
//input events
function onPos(e) {
e = clone(e);
var type;
if(e.type == "mousedown" || e.type == "touchstart")
type = "Down";
else if(e.type == "mouseup" || e.type == "touchend")
type = "Up";
else
type = "Click";
e.point = new Point(
e.clientX - stagePos.x + document.body.scrollLeft + document.documentElement.scrollLeft,
e.clientY - stagePos.y + document.body.scrollTop + document.documentElement.scrollTop
);
Game._input(type, e);
}
function onKey(e) {
e = clone(e);
Game._input(e.type, e);
}
var Game = {
_cb: {
tick: [],
render: [],
input: []
},
extend: function(obj) {
//shallow copy
for(var key in obj) {
if(!obj.hasOwnProperty(key)) continue;
this[key] = obj[key];
}
},
start: function(canvas) {
this._canvas = canvas;
this._ctx = canvas.getContext("2d");
this.width = canvas.width;
this.height = canvas.height;
var onFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
null;
if (onFrame) {
tick = function () {
Game.step();
tickID = onFrame(tick);
}
tick();
} else {
tick = setInterval(Game.step, 1000 / FPS);
}
addEvent(this, canvas, "touchstart", onPos);
addEvent(this, canvas, "mousedown", onPos);
addEvent(this, canvas, "touchend", onPos);
addEvent(this, canvas, "mouseup", onPos);
addEvent(this, canvas, "keydown", onKey);
addEvent(this, canvas, "keyup", onKey);
addEvent(this, canvas, "click", onPos);
addEvent(this, canvas, "tap", onPos);
addEvent(this, window, "resize", function() {
stagePos = Game.DOM.inner(canvas);
});
stagePos = Game.DOM.inner(canvas);
},
step: function() {
var loops = 0;
var dt = Date.now() - curTime;
curTime = Date.now();
if(curTime - nextGameTick > FPS * skipTicks) {
nextGameTick = curTime - skipTicks;
}
while(curTime > nextGameTick) {
Game._tick(dt, frame++);
nextGameTick += skipTicks;
loops++;
}
if(loops) {
Game._render(this._ctx);
}
},
stop: function() {
},
_tick: function(dt, frame) {
var cbs = this._cb.tick,
i = 0, l = cbs.length;
for(; i < l; ++i) {
cbs[i](dt, frame);
}
},
tick: function(cb) {
this._cb.tick.push(cb);
},
_render: function(ctx) {
var cbs = this._cb.render,
i = 0, l = cbs.length;
for(; i < l; ++i) {
cbs[i](ctx);
}
},
render: function(cb) {
this._cb.render.push(cb);
},
_input: function(type, e) {
var cbs = this._cb.input,
i = 0, l = cbs.length;
for(; i < l; ++i) {
cbs[i](type, e);
}
},
input: function(cb) {
this._cb.input.push(cb);
}
};
/**
* DOM features
*/
Game.extend({ DOM: {
inner: function (obj) {
var rect = obj.getBoundingClientRect(),
x = rect.left + (window.pageXOffset ? window.pageXOffset : document.body.scrollTop),
y = rect.top + (window.pageYOffset ? window.pageYOffset : document.body.scrollLeft),
//border left
borderX = parseInt(this.getStyle(obj, 'border-left-width') || 0, 10) ||
parseInt(this.getStyle(obj, 'borderLeftWidth') || 0, 10) || 0,
borderY = parseInt(this.getStyle(obj, 'border-top-width') || 0, 10) ||
parseInt(this.getStyle(obj, 'borderTopWidth') || 0, 10) || 0;
x += borderX;
y += borderY;
return { x: x, y: y };
},
getStyle: function (obj, prop) {
var result;
if (obj.currentStyle)
result = obj.currentStyle[this.camelize(prop)];
else if (window.getComputedStyle)
result = document.defaultView.getComputedStyle(obj, null).getPropertyValue(this.csselize(prop));
return result;
},
camelize: function (str) {
return str.replace(/-+(.)?/g, function (match, chr){ return chr ? chr.toUpperCase() : '' });
},
csselize: function (str) {
return str.replace(/[A-Z]/g, function (chr){ return chr ? '-' + chr.toLowerCase() : '' });
}
}});
window.Game = Game;
}(window, window.document));