-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (102 loc) · 2.45 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
/** MAIN OBJECT **/
Main = (function() {
var lsHighScoreKey = "highScore";
var currentColors, lsColorsKey = "colors"
return {
// Color management
colors: function(index) {
if(!currentColors) {
var stored = localStorage.getItem(lsColorsKey)
if(stored) {
currentColors = JSON.parse(stored)
} else {
currentColors = ["#333333", "#ee1010"]
}
}
return currentColors[index];
},
setColors: function(color_1, color_2) {
localStorage.setItem(lsColorsKey, JSON.stringify([color_1, color_2]))
currentColors = [color_1, color_2]
},
resetColors: function() {
localStorage.removeItem(lsColorsKey)
currentColors = false
},
// Game play management
pause: function() {
gz.update = false;
Main.gameMode = MODE_PAUSE;
},
resume: function() {
gz.update = true;
Main.gameMode = MODE_PLAYING;
},
restart: function() {
Player.restart();
World.reset();
Background.reset();
},
// High score management
saveHighScore: function(score) {
if (!GameCenter.authed) {
//GameCenter.authenticate();
}
GameCenter.reportScore(GC_CATEGORY, score);
if(Main.getHighScore() < score) {
localStorage.setItem(lsHighScoreKey, score);
return true
}
},
getHighScore: function() {
var highScore = localStorage.getItem(lsHighScoreKey);
return highScore ? highScore : 0;
},
resetHighScore: function() {
localStorage.setItem(lsHighScoreKey, 0);
},
gameMode: MODE_PLAYING
}
}());
/** MAIN LOOP **/
var oldTime = new Date();
function mainLoop() {
var time = new Date() - oldTime;
draw(ctx);
update(time);
if(showFPS) {
strokeText(ctx, Math.round(1000 / time), {
x: ((X - 1) * tile.width),
y: tile.height / 2
})
oldTime = new Date();
}
window.requestAnimationFrame(mainLoop);
};
ResourcesLoader.onload(function() {
mainLoop(oldTime)
//Menu.show()
})
function update(time) {
Menu.update(time);
if(!gz.update) {
return;
}
Background.update(time);
World.update();
Player.update();
}
function draw(ctx) {
if(!gz.draw) {
return;
}
//ctx.clearRect(0, 0, gz.width, gz.height);
ctx.globalAlpha = 0.9;
ctx.fillStyle = "#ffffff"
ctx.fillRect(0, 0, gz.width, gz.height);
ctx.globalAlpha = 1.0;
Background.draw(ctx);
World.draw(ctx);
Player.draw(ctx);
Menu.draw(ctx);
}