-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
130 lines (115 loc) · 3.46 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
var Game = function Game() {
var spriteCanvas, spriteContext;
var currentScene;
var SCREENRATIO = .85;
var _state = -1;
var MENU = -1, ARCADE = 0, ENDLESS = 1, CHALLENGE = 2;//states
this.init = function() {
//this.bgCanvas = document.getElementById('background');
this.spriteCanvas = document.getElementById('sprite');
width = this.spriteCanvas.width;
height = this.spriteCanvas.height;
window.addEventListener("keydown", function(evt) { currentScene.keydown(evt); }, false);
this.spriteCanvas.addEventListener("mousedown", function(evt) {
evt.preventDefault();
currentScene.mousedown(evt);
}, false);
this.spriteCanvas.addEventListener("mouseup", function(evt) {
evt.preventDefault();
currentScene.mouseup(evt);
}, false);
this.spriteCanvas.addEventListener("moustout", function(evt) {
evt.preventDefault();
currentScene.mouseout(evt);
}, false);
this.spriteCanvas.addEventListener("mousemove", function(evt) {
evt.preventDefault();
currentScene.mousemove(evt);
}, false);
this.spriteCanvas.addEventListener("touchstart", function(evt) {
evt.preventDefault();
currentScene.touchstart(evt);
}, false);
this.spriteCanvas.addEventListener("touchend", function(evt) {
evt.preventDefault();
currentScene.touchend(evt);
}, false);
this.spriteCanvas.addEventListener("touchcancel", function(evt) {
evt.preventDefault();
currentScene.touchcancel(evt);
}, false);
this.spriteCanvas.addEventListener("touchmove", function(evt) {
evt.preventDefault();
currentScene.touchmove(evt);
}, false);
this.spriteCanvas.addEventListener("contextmenu", function(evt) { evt.preventDefault(); }, false);
if (this.spriteCanvas.getContext) {
//this.bgContext = this.bgCanvas.getContext('2d');
this.spriteContext = this.spriteCanvas.getContext('2d');
_height = window.innerHeight;
_width = window.innerWidth;
//_width = _height*10/16;
this.spriteCanvas.width = _width;
this.spriteCanvas.height = _height;
//this.spriteContext.drawImage(resourceRepository.background, 0, _height*SCREENRATIO, _width, _height*(1-SCREENRATIO));
_state = MENU;
var m = new Menu();
m.init(_width, _height);
m.subscribe(changeScene);
currentScene = m;
return true;
}
return false;
};
changeScene = function(s) {
if (_state == MENU) {
if (s == ARCADE) {
var a = new Arcade();
a.init(_width, _height);
a.subscribe(changeScene);
var oldScene = currentScene;
currentScene = a;
delete oldScene;
_state = ARCADE;
}
else if (s == ENDLESS) {
var e = new Endless();
e.init(_width, _height);
e.subscribe(changeScene);
var oldScene = currentScene;
currentScene = e;
delete oldScene;
_state = ENDLESS;
}
else if (s == CHALLENGE) {
var c = new Challenge();
c.init(_width, _height);
c.subscribe(changeScene);
var oldScene = currentScene;
currentScene = c;
delete oldScene;
_state = CHALLENGE;
}
}
else {
var m = new Menu();
m.init(_width, _height);
m.subscribe(changeScene);
var oldScene = currentScene;
currentScene = m;
delete oldScene;
_state = MENU;
}
};
this.draw = function() {
var ctx = this.spriteContext;
currentScene.draw(ctx);
};
this.update = function() {
currentScene.update();
};
// Start the animation loop
this.start = function() {
animate();
};
}