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