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 |
|
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 | function Game(canvas, manifest) {
|
109 | window.addEventListener("resize", function() { setCanvasSizeScaled(canvas); });
|
110 | setCanvasSizeScaled(canvas);
|
111 |
|
112 | |
113 |
|
114 |
|
115 |
|
116 | this.mouse = new Mouse(canvas);
|
117 | |
118 |
|
119 |
|
120 |
|
121 | this.keyboard = new Keyboard(keyMap.US);
|
122 | |
123 |
|
124 |
|
125 |
|
126 | this.accelerometer = new Accelerometer();
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 | this.images = new ImageLoader();
|
133 | loadAssets(this.images, manifest.images);
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 | this.sounds = new SoundLoader();
|
140 | loadAssets(this.sounds, manifest.sounds);
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 | this.fonts = new FontLoader();
|
147 | this.fonts.load(manifest.fonts);
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 | this.animations = new AnimationLoader(this.images, manifest.animations);
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 | this.scenes = new SceneManager();
|
160 | this.scenes.add("loading", makeLoadingScene(this, canvas, "title"));
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | Game.prototype.isLoaded = function() {
|
167 | return this.images.allLoaded() &&
|
168 | this.sounds.allLoaded() &&
|
169 | this.fonts.allLoaded() &&
|
170 | this.animations.allLoaded();
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | Game.prototype.percentLoaded = function() {
|
177 | var totalAssets =
|
178 | this.images.totalImages +
|
179 | this.sounds.totalSounds +
|
180 | this.fonts.totalFonts;
|
181 | var loadedAssets =
|
182 | this.images.loadedImages +
|
183 | this.sounds.loadedSounds +
|
184 | this.fonts.loadedFonts;
|
185 | return loadedAssets / totalAssets;
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | Game.prototype.isChromeApp = function() {
|
192 | return window.chrome && window.chrome.app && window.chrome.app.runtime;
|
193 | };
|
194 |
|
195 | module.exports = Game;
|