-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainGame.js
More file actions
83 lines (73 loc) · 2.14 KB
/
MainGame.js
File metadata and controls
83 lines (73 loc) · 2.14 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
//wrapper for requestAnimFrame that works on multiple Browsers
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
/*---- MainGame Class ----*/
function MainGame() {
//related to levels and powerup
//canvas used everywhere
this.canvas = document.getElementById("c0");
this.ctx = this.canvas.getContext("2d");
this.canvasWidth = this.ctx.canvas.width;
this.canvasHeight = this.ctx.canvas.height;
this.mobile = false;
this.mobileCheck();
}
MainGame.prototype.mobileCheck = function() {
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent.toLocaleLowerCase()) ){
this.mobile = true;
}
}
//change the game state
MainGame.prototype.setState = function (state) {
gameState = state;
}
//the game loop that runs 60/s
MainGame.prototype.run = function () {
(function loop(animStart) {
gameState.update(animStart);
gameState.draw();
requestAnimFrame(loop);
})();
}
/*---- End of MainGame ----*/
/*---- Global vars ----*/
var theGame = new MainGame();
//states
gameState = new ExampleState(theGame);
//all Events
var keysDown = {};
addEventListener("keydown", function(e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function(e) {
delete keysDown[e.keyCode];
}, false);
window.addEventListener("pointerdown", function(e) {
// media.zik.play();
e.x-=theGame.canvas.offsetLeft;
e.y-=theGame.canvas.offsetTop;
gameState.pDown(e);
}, false);
window.addEventListener("pointerup", function(e) {
var rect = theGame.canvas.getBoundingClientRect();
e.x-=rect.left;
e.y-=rect.top;
gameState.pUp(e);
}, false);
window.addEventListener("pointermove", function(e) {
var rect = theGame.canvas.getBoundingClientRect();
e.x-=rect.left;
e.y-=rect.top;
gameState.pMove(e);
}, false);
/*---- Make it run ----*/
theGame.run();
/*---- ----*/