UNPKG

5.2 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");
13
14function loadAssets(assetLoader, assets) {
15 for (var key in assets) {
16 if (assets.hasOwnProperty(key)) {
17 assetLoader.load(key, assets[key]);
18 }
19 }
20}
21
22function 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
48function 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 * Represents a whole game. This class contains all the inputs, outputs, and data for the game.
77 * @constructor
78 * @alias Splat.Game
79 * @param {external:canvas} canvas The canvas on which to render the game.
80 * @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.
81 * @example
82 var canvas = document.getElementById("canvas");
83 var manifest = {
84 "images": {
85 "bg": "images/bg.png"
86 },
87 "sounds": {
88 "point": "sounds/point.wav"
89 },
90 "fonts": [
91 "pixelade": {
92 "embedded-opentype": "pixelade/pixelade-webfont.eot",
93 "woff": "pixelade/pixelade-webfont.woff",
94 "truetype": "pixelade/pixelade-webfont.ttf",
95 "svg": "pixelade/pixelade-webfont.svg#pixeladeregular"
96 }
97 ],
98 "animations": {
99 "player-slide-left": {
100 "strip": "images/player-slide-anim.png",
101 "frames": 8,
102 "msPerFrame": 100
103 }
104 }
105 };
106 var game = new Splat.Game(canvas, manifest);
107 */
108function Game(canvas, manifest) {
109 window.addEventListener("resize", function() { setCanvasSizeScaled(canvas); });
110 setCanvasSizeScaled(canvas);
111
112 /**
113 * The mouse input for the game.
114 * @member {Mouse}
115 */
116 this.mouse = new Mouse(canvas);
117 /**
118 * The keyboard input for the game.
119 * @member {Keyboard}
120 */
121 this.keyboard = new Keyboard(keyMap.US);
122 /**
123 * The accelerometer input for the game.
124 * @member {Accelerometer}
125 */
126 this.accelerometer = new Accelerometer();
127
128 /**
129 * The image assets for the game.
130 * @member {ImageLoader}
131 */
132 this.images = new ImageLoader();
133 loadAssets(this.images, manifest.images);
134
135 /**
136 * The sound assets for the game.
137 * @member {SoundLoader}
138 */
139 this.sounds = new SoundLoader();
140 loadAssets(this.sounds, manifest.sounds);
141
142 /**
143 * The font assets for the game.
144 * @member {FontLoader}
145 */
146 this.fonts = new FontLoader();
147 this.fonts.load(manifest.fonts);
148
149 /**
150 * The animation assets for the game.
151 * @member {AnimationLoader}
152 */
153 this.animations = new AnimationLoader(this.images, manifest.animations);
154
155 /**
156 * The scenes for the game.
157 * @member {SceneManager}
158 */
159 this.scenes = new SceneManager();
160 this.scenes.add("loading", makeLoadingScene(this, canvas, "title"));
161}
162/**
163 * Test if all the game's assets are loaded.
164 * @returns {boolean}
165 */
166Game.prototype.isLoaded = function() {
167 return this.images.allLoaded() &&
168 this.sounds.allLoaded() &&
169 this.fonts.allLoaded() &&
170 this.animations.allLoaded();
171};
172/**
173 * Determine the percent of the game's assets that are loaded. This is useful for drawing a loading bar.
174 * @returns {number} A number between 0 and 1
175 */
176Game.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 * Test if the game is running within a Chrome App.
189 * @returns {boolean}
190 */
191Game.prototype.isChromeApp = function() {
192 return window.chrome && window.chrome.app && window.chrome.app.runtime;
193};
194
195module.exports = Game;