1 | "use strict";
|
2 |
|
3 | var Camera = require("./camera");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function Scene(canvas, initFunc, simulationFunc, drawFunc) {
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 | this.canvas = canvas;
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | this.initFunc = initFunc;
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | this.simulationFunc = simulationFunc;
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | this.drawFunc = drawFunc;
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | this.context = canvas.getContext("2d");
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | this.lastTimestamp = -1;
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | this.running = false;
|
58 | |
59 |
|
60 |
|
61 |
|
62 | this.timers = {};
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 | this.camera = new Camera(0, 0, canvas.width, canvas.height);
|
70 | |
71 |
|
72 |
|
73 |
|
74 | this.showFrameRate = false;
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | Scene.prototype.start = function() {
|
80 | this.lastTimestamp = -1;
|
81 | this.running = true;
|
82 | this.initFunc.call(this);
|
83 | var scene = this;
|
84 | window.requestAnimationFrame(function(t) { mainLoop(scene, t); });
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 | Scene.prototype.stop = function() {
|
90 | this.running = false;
|
91 | };
|
92 |
|
93 |
|
94 |
|
95 | Scene.prototype.reset = function() {
|
96 | this.initFunc.call(this);
|
97 | };
|
98 |
|
99 | function mainLoop(scene, timestamp) {
|
100 | if (!scene.running) {
|
101 | return;
|
102 | }
|
103 | if (scene.lastTimestamp === -1) {
|
104 | scene.lastTimestamp = timestamp;
|
105 | }
|
106 | var elapsedMillis = timestamp - scene.lastTimestamp;
|
107 | scene.lastTimestamp = timestamp;
|
108 |
|
109 | incrementTimers(scene.timers, elapsedMillis);
|
110 | if (!scene.running) {
|
111 | return;
|
112 | }
|
113 | scene.simulationFunc.call(scene, elapsedMillis);
|
114 | scene.camera.move(elapsedMillis);
|
115 |
|
116 | scene.context.save();
|
117 | scene.camera.draw(scene.context);
|
118 | scene.drawFunc.call(scene, scene.context);
|
119 |
|
120 | if (scene.showFrameRate) {
|
121 | drawFrameRate(scene, elapsedMillis);
|
122 | }
|
123 |
|
124 | scene.context.restore();
|
125 |
|
126 | if (scene.running) {
|
127 | window.requestAnimationFrame(function(t) { mainLoop(scene, t); });
|
128 | }
|
129 | }
|
130 |
|
131 | function incrementTimers(timers, elapsedMillis) {
|
132 | for (var i in timers) {
|
133 | if (timers.hasOwnProperty(i)) {
|
134 | timers[i].tick(elapsedMillis);
|
135 | }
|
136 | }
|
137 | }
|
138 |
|
139 | function drawFrameRate(scene, elapsedMillis) {
|
140 | var fps = (1000 / elapsedMillis) |0;
|
141 |
|
142 | scene.context.font = "24px mono";
|
143 | if (fps < 30) {
|
144 | scene.context.fillStyle = "#ff0000";
|
145 | } else if (fps < 50) {
|
146 | scene.context.fillStyle = "#ffff00";
|
147 | } else {
|
148 | scene.context.fillStyle = "#00ff00";
|
149 | }
|
150 | var msg = fps + " FPS";
|
151 | var w = scene.context.measureText(msg).width;
|
152 | scene.camera.drawAbsolute(scene.context, function() {
|
153 | scene.context.fillText(msg, scene.canvas.width - w - 50, 50);
|
154 | });
|
155 | }
|
156 |
|
157 | module.exports = Scene;
|