UNPKG

5.67 kBJavaScriptView Raw
1"use strict";
2
3var Scene = require("./scene");
4var Mouse = require("./mouse");
5var Accelerometer = require("./accelerometer");
6var Keyboard = require("./keyboard");
7var keyMap = require("./key_map");
8var ImageLoader = require("./image_loader");
9var SoundLoader = require("./sound_loader");
10var FontLoader = require("./font_loader");
11var AnimationLoader = require("./animation_loader");
12var SceneManager = require("./scene_manager");
13var platform = require("./platform");
14
15function loadAssets(assetLoader, assets) {
16 for (var key in assets) {
17 if (assets.hasOwnProperty(key)) {
18 assetLoader.load(key, assets[key]);
19 }
20 }
21}
22
23function 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
49function 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 * Represents a whole game. This class contains all the inputs, outputs, and data for the game.
78 * @constructor
79 * @alias Splat.Game
80 * @param {external:canvas} canvas The canvas on which to render the game.
81 * @param {object} manifest A key-value set of attributes that describe all the external resources for the game. This references all the images, sounds, fonts, and animations.
82 * @example
83 var canvas = document.getElementById("canvas");
84 var manifest = {
85 "images": {
86 "bg": "images/bg.png"
87 },
88 "sounds": {
89 "point": "sounds/point.wav"
90 },
91 "fonts": [
92 "pixelade": {
93 "embedded-opentype": "pixelade/pixelade-webfont.eot",
94 "woff": "pixelade/pixelade-webfont.woff",
95 "truetype": "pixelade/pixelade-webfont.ttf",
96 "svg": "pixelade/pixelade-webfont.svg#pixeladeregular"
97 }
98 ],
99 "animations": {
100 "player-slide-left": {
101 "strip": "images/player-slide-anim.png",
102 "frames": 8,
103 "msPerFrame": 100
104 }
105 }
106 };
107 var game = new Splat.Game(canvas, manifest);
108 */
109function 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 * The mouse input for the game.
135 * @member {Mouse}
136 */
137 this.mouse = new Mouse(canvas);
138 /**
139 * The keyboard input for the game.
140 * @member {Keyboard}
141 */
142 this.keyboard = new Keyboard(keyMap.US);
143 /**
144 * The accelerometer input for the game.
145 * @member {Accelerometer}
146 */
147 this.accelerometer = new Accelerometer();
148
149 /**
150 * The image assets for the game.
151 * @member {ImageLoader}
152 */
153 this.images = new ImageLoader();
154 loadAssets(this.images, manifest.images);
155
156 /**
157 * The sound assets for the game.
158 * @member {SoundLoader}
159 */
160 this.sounds = new SoundLoader();
161 loadAssets(this.sounds, manifest.sounds);
162
163 /**
164 * The font assets for the game.
165 * @member {FontLoader}
166 */
167 this.fonts = new FontLoader();
168 this.fonts.load(manifest.fonts);
169
170 /**
171 * The animation assets for the game.
172 * @member {AnimationLoader}
173 */
174 this.animations = new AnimationLoader(this.images, manifest.animations);
175
176 /**
177 * The scenes for the game.
178 * @member {SceneManager}
179 */
180 this.scenes = new SceneManager();
181 this.scenes.add("loading", makeLoadingScene(this, canvas, "title"));
182}
183/**
184 * Test if all the game's assets are loaded.
185 * @returns {boolean}
186 */
187Game.prototype.isLoaded = function() {
188 return this.images.allLoaded() &&
189 this.sounds.allLoaded() &&
190 this.fonts.allLoaded() &&
191 this.animations.allLoaded();
192};
193/**
194 * Determine the percent of the game's assets that are loaded. This is useful for drawing a loading bar.
195 * @returns {number} A number between 0 and 1
196 */
197Game.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 * Test if the game is running within a Chrome App.
210 * @returns {boolean}
211 */
212Game.prototype.isChromeApp = function() {
213 return platform.isChromeApp();
214};
215
216module.exports = Game;