1 | "use strict";
|
2 |
|
3 | var Scene = require("./scene");
|
4 | var Mouse = require("./mouse");
|
5 | var Accelerometer = require("./accelerometer");
|
6 | var Keyboard = require("./keyboard");
|
7 | var keyMap = require("./key_map");
|
8 | var ImageLoader = require("./image_loader");
|
9 | var SoundLoader = require("./sound_loader");
|
10 | var FontLoader = require("./font_loader");
|
11 | var AnimationLoader = require("./animation_loader");
|
12 | var SceneManager = require("./scene_manager");
|
13 | var platform = require("./platform");
|
14 |
|
15 | function loadAssets(assetLoader, assets) {
|
16 | for (var key in assets) {
|
17 | if (assets.hasOwnProperty(key)) {
|
18 | assetLoader.load(key, assets[key]);
|
19 | }
|
20 | }
|
21 | }
|
22 |
|
23 | function makeLoadingScene(game, canvas, nextScene) {
|
24 | return new Scene(canvas, function() {
|
25 | }, function() {
|
26 | if (game.isLoaded()) {
|
27 | game.scenes.switchTo(nextScene);
|
28 | }
|
29 | }, function(context) {
|
30 | context.fillStyle = "#000000";
|
31 | context.fillRect(0, 0, canvas.width, canvas.height);
|
32 |
|
33 | var quarterWidth = (canvas.width / 4) |0;
|
34 | var halfWidth = (canvas.width / 2) |0;
|
35 | var halfHeight = (canvas.height / 2) |0;
|
36 |
|
37 | context.fillStyle = "#ffffff";
|
38 | context.fillRect(quarterWidth, halfHeight - 15, halfWidth, 30);
|
39 |
|
40 | context.fillStyle = "#000000";
|
41 | context.fillRect(quarterWidth + 3, halfHeight - 12, halfWidth - 6, 24);
|
42 |
|
43 | context.fillStyle = "#ffffff";
|
44 | var barWidth = (halfWidth - 6) * game.percentLoaded();
|
45 | context.fillRect(quarterWidth + 3, halfHeight - 12, barWidth, 24);
|
46 | });
|
47 | }
|
48 |
|
49 | function setCanvasSizeScaled(canvas) {
|
50 | var ww = window.innerWidth;
|
51 | var wh = window.innerHeight;
|
52 | var cw = canvas.width;
|
53 | var ch = canvas.height;
|
54 |
|
55 | if (ww >= cw && wh >= ch) {
|
56 | return;
|
57 | } else if (ww < cw && wh >= ch) {
|
58 | wh = ((ww / cw) * ch) | 0;
|
59 | canvas.style.width = ww + "px";
|
60 | canvas.style.height = wh + "px";
|
61 | } else if (ww >= cw && wh < ch) {
|
62 | ww = ((wh / ch) * cw) | 0;
|
63 | canvas.style.width = ww + "px";
|
64 | canvas.style.height = wh + "px";
|
65 | } else if (ww < cw && wh < ch) {
|
66 | if ((ww / cw) * ch > wh) {
|
67 | ww = ((wh / ch) * cw) | 0;
|
68 | } else {
|
69 | wh = ((ww / cw) * ch) | 0;
|
70 | }
|
71 | canvas.style.width = ww + "px";
|
72 | canvas.style.height = wh + "px";
|
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 | function Game(canvas, manifest) {
|
110 | window.addEventListener("resize", function() { setCanvasSizeScaled(canvas); });
|
111 | setCanvasSizeScaled(canvas);
|
112 |
|
113 | var game = this;
|
114 | var wasMuted = false;
|
115 | window.addEventListener("visibilitychange", function() {
|
116 | var scene = game.scenes.currentScene;
|
117 | if (typeof scene.visibilitychange === "function") {
|
118 | scene.visibilitychange(document.visibilityState);
|
119 | return;
|
120 | }
|
121 | if (document.visibilityState === "hidden") {
|
122 | scene.stop();
|
123 | wasMuted = game.sounds.muted;
|
124 | game.sounds.mute();
|
125 | } else {
|
126 | scene.start();
|
127 | if (!wasMuted) {
|
128 | game.sounds.unmute();
|
129 | }
|
130 | }
|
131 | });
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 | this.mouse = new Mouse(canvas);
|
138 | |
139 |
|
140 |
|
141 |
|
142 | this.keyboard = new Keyboard(keyMap.US);
|
143 | |
144 |
|
145 |
|
146 |
|
147 | this.accelerometer = new Accelerometer();
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 | this.images = new ImageLoader();
|
154 | loadAssets(this.images, manifest.images);
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 | this.sounds = new SoundLoader();
|
161 | loadAssets(this.sounds, manifest.sounds);
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 | this.fonts = new FontLoader();
|
168 | this.fonts.load(manifest.fonts);
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 | this.animations = new AnimationLoader(this.images, manifest.animations);
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 | this.scenes = new SceneManager();
|
181 | this.scenes.add("loading", makeLoadingScene(this, canvas, "title"));
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | Game.prototype.isLoaded = function() {
|
188 | return this.images.allLoaded() &&
|
189 | this.sounds.allLoaded() &&
|
190 | this.fonts.allLoaded() &&
|
191 | this.animations.allLoaded();
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | Game.prototype.percentLoaded = function() {
|
198 | var totalAssets =
|
199 | this.images.totalImages +
|
200 | this.sounds.totalSounds +
|
201 | this.fonts.totalFonts;
|
202 | var loadedAssets =
|
203 | this.images.loadedImages +
|
204 | this.sounds.loadedSounds +
|
205 | this.fonts.loadedFonts;
|
206 | return loadedAssets / totalAssets;
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | Game.prototype.isChromeApp = function() {
|
213 | return platform.isChromeApp();
|
214 | };
|
215 |
|
216 | module.exports = Game;
|