-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscene.js.html
More file actions
229 lines (194 loc) · 7.48 KB
/
scene.js.html
File metadata and controls
229 lines (194 loc) · 7.48 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
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
227
228
229
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: scene.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: scene.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
var Camera = require("./camera");
/**
* A Scene handles the render loop for the game. Inside of initFunc, simulationFunc, and drawFunc `this` refers to the current scene.
* @constructor
* @alias Splat.Scene
* @param {external:canvas} canvas The canvas to render on.
* @param {emptyCallback} initFunc A callback to be called every time the Scene is {@link Splat.Scene#start started}.
* @param {simulationCallback} simulationFunc A callback that updates the state of the game's simulation.
* @param {drawCallback} drawFunc A callback that draws the game.
*/
function Scene(canvas, initFunc, simulationFunc, drawFunc) {
/**
* The canvas to render on.
* @member {external:canvas}
* @private
*/
this.canvas = canvas;
/**
* A callback to be called ever time the Scene is {@link Splat.Scene#start started}.
* @member {emptyCallback}
* @private
*/
this.initFunc = initFunc;
/**
* A callback that updates the state of the game's simulation.
* @member {simulationCallback}
* @private
*/
this.simulationFunc = simulationFunc;
/**
* A callback that draws the game.
* @member {drawCallback}
* @private
*/
this.drawFunc = drawFunc;
/**
* The drawing context for {@link Scene#canvas}
* @member {external:CanvasRenderingContext2D}
* @private
*/
this.context = canvas.getContext("2d");
/**
* The timestamp of the last frame. Used to determine how many milliseconds elapsed between frames.
* @member {number}
* @private
*/
this.lastTimestamp = -1;
/**
* How frequently to run the simulation in Hertz (cycles per second). This should be higher than your expected framerate.
* @member {number}
* @private
*/
this.simulationFrequencyHz = 180;
/**
* An accumulator of the leftover time between frames. This lets us run the simulation at a constant framerate independant of the drawing framerate.
* @member {number}
* @private
*/
this.timeAccumulator = 0;
/**
* Whether or not the Scene is currently running.
* @member {boolean}
* @private
*/
this.running = false;
/**
* A key-value store of named timers. Timers in this object will be automatically {@link Splat.Timer#tick tick()}ed for you when the scene is running..
* @member {object}
*/
this.timers = {};
/**
* The Camera used to offset the Scene's drawing.
* This Camera's {@link Splat.Entity#move move} and {@link Splat.Camera#draw draw} methods are called automatically for you. The default Camera starts at the origin (0,0).
* @member {Splat.Camera}
*/
this.camera = new Camera(0, 0, canvas.width, canvas.height);
/**
* A flag that enables/disables a frame rate counter in the corner of the screen. This is useful during development.
* @member {boolean}
*/
this.showFrameRate = false;
}
/**
* Start running the scene.
*/
Scene.prototype.start = function() {
this.lastTimestamp = -1;
this.running = true;
var scene = this;
window.requestAnimationFrame(function(t) { mainLoop(scene, t); });
};
/**
* Stop running the scene.
*/
Scene.prototype.stop = function() {
this.running = false;
};
/**
* Reset the simulation by re-running the {@link Splat.Scene#initFunc}.
*/
Scene.prototype.reset = function() {
this.initFunc.call(this);
};
var gamepad = require("./gamepad");
function mainLoop(scene, timestamp) {
if (!scene.running) {
return;
}
if (scene.lastTimestamp === -1) {
scene.lastTimestamp = timestamp;
}
var elapsedMillis = timestamp - scene.lastTimestamp;
scene.lastTimestamp = timestamp;
gamepad.update();
scene.timeAccumulator += elapsedMillis;
var simulationMs = Math.floor(1000 / scene.simulationFrequencyHz);
while (scene.timeAccumulator > simulationMs) {
scene.timeAccumulator -= simulationMs;
incrementTimers(scene.timers, simulationMs);
if (!scene.running) {
return;
}
scene.simulationFunc.call(scene, simulationMs);
scene.camera.move(simulationMs);
}
scene.context.save();
scene.camera.draw(scene.context);
scene.drawFunc.call(scene, scene.context);
if (scene.showFrameRate) {
drawFrameRate(scene, elapsedMillis);
}
scene.context.restore();
if (scene.running) {
window.requestAnimationFrame(function(t) { mainLoop(scene, t); });
}
}
function incrementTimers(timers, elapsedMillis) {
for (var i in timers) {
if (timers.hasOwnProperty(i)) {
timers[i].tick(elapsedMillis);
}
}
}
function drawFrameRate(scene, elapsedMillis) {
var fps = (1000 / elapsedMillis) |0;
scene.context.font = "24px mono";
if (fps < 30) {
scene.context.fillStyle = "#ff0000";
} else if (fps < 50) {
scene.context.fillStyle = "#ffff00";
} else {
scene.context.fillStyle = "#00ff00";
}
var msg = fps + " FPS";
var w = scene.context.measureText(msg).width;
scene.camera.drawAbsolute(scene.context, function() {
scene.context.fillText(msg, scene.canvas.width - w - 50, 50);
});
}
module.exports = Scene;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-buffer.html">buffer</a></li><li><a href="module-KeyMap.html">KeyMap</a></li></ul><h3>Externals</h3><ul><li><a href="external-AudioContext.html">AudioContext</a></li><li><a href="external-canvas.html">canvas</a></li><li><a href="external-CanvasRenderingContext2D.html">CanvasRenderingContext2D</a></li><li><a href="external-image.html">image</a></li></ul><h3>Classes</h3><ul><li><a href="Accelerometer.html">Accelerometer</a></li><li><a href="Animation.html">Animation</a></li><li><a href="AnimationLoader.html">AnimationLoader</a></li><li><a href="FontLoader.html">FontLoader</a></li><li><a href="ImageLoader.html">ImageLoader</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="SceneManager.html">SceneManager</a></li><li><a href="SoundLoader.html">SoundLoader</a></li><li><a href="Splat.AnimatedEntity.html">AnimatedEntity</a></li><li><a href="Splat.AStar.html">AStar</a></li><li><a href="Splat.BinaryHeap.html">BinaryHeap</a></li><li><a href="Splat.Camera.html">Camera</a></li><li><a href="Splat.Entity.html">Entity</a></li><li><a href="Splat.EntityBoxCamera.html">EntityBoxCamera</a></li><li><a href="Splat.Game.html">Game</a></li><li><a href="Splat.math.Random.html">Random</a></li><li><a href="Splat.NinePatch.html">NinePatch</a></li><li><a href="Splat.Scene.html">Scene</a></li><li><a href="Splat.Timer.html">Timer</a></li></ul><h3>Namespaces</h3><ul><li><a href="Splat.html">Splat</a></li><li><a href="Splat.ads.html">ads</a></li><li><a href="Splat.leaderboards.html">leaderboards</a></li><li><a href="Splat.math.html">math</a></li><li><a href="Splat.saveData.html">saveData</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Thu Sep 17 2015 23:22:42 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>