-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·121 lines (100 loc) · 2.64 KB
/
main.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
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function message(msg) {
document.getElementById("message").innerHTML = msg;
}
// keyboard event handling
Game.keysDown = {};
addEventListener("keydown", function(e) { Game.keysDown[e.keyCode] = true; }, false);
addEventListener("keyup", function(e) { delete Game.keysDown[e.keyCode]; }, false);
Game.input = function() {
var thrust = 0.2 * Game.player.m;
// Player holding up
if (38 in Game.keysDown) {
Game.player.impulse(0,-thrust);
}
// Player holding down
if (40 in Game.keysDown) {
Game.player.impulse(0,thrust);
}
// Player holding left
if (37 in Game.keysDown) {
Game.player.impulse(-thrust,0);
}
// Player holding right
if (39 in Game.keysDown) {
Game.player.impulse(thrust,0);
}
// Space bar
if (32 in Game.keysDown) {
Game.player.basicLaser();
Game.player.boom();
Game.player.gun();
}
};
Game.load = function() {
Game.director = new Director(Game.directorStat);
Game.director.init();
Game.display = new Display(Game.width, Game.height);
};
Game.start = function() {
initMic();
Game.running = true;
Game.startTime = new Date();
Game.frameNum = 0;
Game.fpsTime = Game.startTime;
Game.updateLoop = setInterval(Game.update, 5);
Game.renderLoop = (function animloop(){
Game.display.render();
requestAnimFrame(animloop);
}
)();
};
Game.pause = function() {
};
Game.getXVel = function(x,y) {
return 0;
};
Game.getYVel = function(x,y) {
return 0;
};
Game.over = function(failed) {
if(failed) {
//Game over
}
else {
//Level clear, display score
}
};
Game.isAreaClear = function(x,y,r) {
var dx;
var dy;
for (j in Game.actors) {
dx = Game.actors[j].x - x;
dy = Game.actors[j].y - y;
if (norm(x,y) < r + Game.actors[j].r) {
return false;
}
}
return true;
};
Game.update = function() {
var curTime = new Date();
Game.gameTime = curTime - Game.startTime;
Game.frameNum += 1;
if(curTime - Game.fpsTime > 1000){
document.getElementById("fps").innerHTML = ((1000 * (Game.frameNum) / (curTime - Game.fpsTime) + 0.5) | 0);
Game.fpsTime = curTime;
Game.frameNum = 0;
}
Game.director.run()
Game.input();
};
$( document ).ready(function() {
Game.load();
console.log(Game);
});