This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
226 lines (174 loc) · 4.42 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
/*
* GameIndus - A free online platform to imagine, create and publish your game with ease!
*
* GameIndus old 2d engine
* Copyright (c) 2015-2016 Maxime Malgorn (Utarwyn)
* <https://github.com/GameIndus/engine-2d-old>
*
*/
/**
* Game
* @class
*/
var Game = function(){
this.loader = new Loader();
this.ressources = new Ressources();
this.events = new eventListener();
this.spectrometer = new Spectrometer();
this.errorView = new ErrorView(this);
this.collisionsManager = new CollisionsManager();
this.networkManager = new NetworkManager();
this.canvas = new Canvas();
this.currentScene = null;
this.scenes = {};
this.paused = false;
this.initialized = false;
this.network = false;
this.delta = 0;
this.fps = 0;
this.maxFps = -1;
}
Game.prototype = {
load: function(id){
if(this.initialized) return false;
this.ressources.loadRessources();
SoundsManager.loadSounds();
if(id!==undefined)
this.canvas.load(id);
else
this.canvas.load("canvas");
this.canvas.setSize(Config.defaultSize.w, Config.defaultSize.h);
loop();
log("Game engine ready.");
},
init: function(){
if(this.initialized) return false;
this.loader.init();
if(this.network) this.networkManager.load();
if(Config.debugMode){
// Load spectrometer
this.spectrometer.setEnabled(true);
this.spectrometer.init();
log("Game engine initialized.");
}
this.initialized = true;
},
setCanvasSize: function(width, height){
this.canvas.setSize(width, height);
},
setMaxFPS: function(maxFps){
this.maxFps = maxFps;
},
getCanvas: function(){
return this.canvas;
},
getSize: function(){
return this.canvas.getSize();
},
getEventsManager: function(){
return this.events;
},
getContext: function(){
return this.canvas.getContext();
},
getRessources: function(){
return this.ressources;
},
/**
* Allow you to add a scene into the game
* @param {String} name The scene name
* @param {Scene} scene The scene object
*/
addScene: function(name, scene){
this.scenes[name] = scene;
},
/**
* Define the current scene
* @param {String} name The scene name
*/
setCurrentScene: function(name){
if(typeof name === "string"){
if(this.getScene(name) == null && Config.debugMode) log("Scene '" + name + "' is undefined.", "error");
this.currentScene = this.getScene(name);
}
else
this.currentScene = name;
},
/**
* Get a scene with its name
* @param {String} name The scene name
* @return {Scene} The scene returned (or null)
*/
getScene: function(name){
return this.scenes[name];
},
getCurrentScene: function(){
return this.currentScene;
},
getNetworkManager: function(){
return this.networkManager;
},
getServer: function(){
return this.getNetworkManager().getNetwork();
}
};
var lastTime = 0;
var frames = 0;
var averageFps = 0;
function loop(){
if(!Game.paused){
var now = Date.now();
var dt = (now - lastTime) / 1000.0;
// Get FPS
frames++;
if(frames>=45){
Game.fps = Math.round(1/dt);
if(averageFps==0) averageFps = Game.fps
else averageFps = Math.round((averageFps+Game.fps)/2)
frames = 0;
}
// Spectrometer
if(Game.loader.isLoaded)
Game.spectrometer.update();
Game.delta = dt;
Game.canvas.clear();
if(Game.loader.isLoaded){
if(!Game.errorView.enabled){
if(Game.getCurrentScene()!=null){
Game.getNetworkManager().updateNetwork();
if(Game.getCurrentScene().camera!=null){
Game.getCurrentScene().camera.begin();
Game.getCurrentScene().camera.update();
}
Game.getCurrentScene().render(dt);
if(Game.getCurrentScene().camera!=null)
Game.getCurrentScene().camera.end();
}
Game.events.dispatch("gameRendered", {delta: dt, fps: Game.fps, averageFps: averageFps});
}else{
Game.errorView.render();
}
}else{
Game.loader.update();
Game.loader.render();
}
if(Game.loader.isLoaded)
Game.spectrometer.render();
if(Game.debugMode && window.debugRpgCollisions != null){
var lt = window.debugRpgCollisions;
var ctx = Game.getContext();
ctx.fillStyle = "rgba(102,51,153,0.5)";
for(var i = 0; i < lt.x.length; i++){
for(var j = 0; j < lt.y.length; j++){
var couple = [lt.x[i], lt.y[j]];
ctx.fillRect(couple[0] * lt.cell[0], couple[1] * lt.cell[1], lt.cell[0], lt.cell[1]);
}
}
}
lastTime = now;
}
if(Game.maxFps==-1)
requestAnimationFrame(loop);
else
setTimeout(loop, 1000/Game.maxFps);
}