UNPKG

5.54 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
75function 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 * Represents a whole game. This class contains all the inputs, outputs, and data for the game.
84 * @constructor
85 * @alias Splat.Game
86 * @param {external:canvas} canvas The canvas on which to render the game.
87 * @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.
88 * @example
89 var canvas = document.getElementById("canvas");
90 var manifest = {
91 "images": {
92 "bg": "images/bg.png"
93 },
94 "sounds": {
95 "point": "sounds/point.wav"
96 },
97 "fonts": [
98 "pixelade": {
99 "embedded-opentype": "pixelade/pixelade-webfont.eot",
100 "woff": "pixelade/pixelade-webfont.woff",
101 "truetype": "pixelade/pixelade-webfont.ttf",
102 "svg": "pixelade/pixelade-webfont.svg#pixeladeregular"
103 }
104 ],
105 "animations": {
106 "player-slide-left": {
107 "strip": "images/player-slide-anim.png",
108 "frames": 8,
109 "msPerFrame": 100
110 }
111 }
112 };
113 var game = new Splat.Game(canvas, manifest);
114 */
115function 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 * The mouse input for the game.
125 * @member {Mouse}
126 */
127 this.mouse = new Mouse(canvas);
128 /**
129 * The keyboard input for the game.
130 * @member {Keyboard}
131 */
132 this.keyboard = new Keyboard(keyMap.US);
133 /**
134 * The accelerometer input for the game.
135 * @member {Accelerometer}
136 */
137 this.accelerometer = new Accelerometer();
138
139 /**
140 * The image assets for the game.
141 * @member {ImageLoader}
142 */
143 this.images = new ImageLoader();
144 loadAssets(this.images, manifest.images);
145
146 /**
147 * The sound assets for the game.
148 * @member {SoundLoader}
149 */
150 this.sounds = new SoundLoader();
151 loadAssets(this.sounds, manifest.sounds);
152
153 /**
154 * The font assets for the game.
155 * @member {FontLoader}
156 */
157 this.fonts = new FontLoader();
158 this.fonts.load(manifest.fonts);
159
160 /**
161 * The animation assets for the game.
162 * @member {AnimationLoader}
163 */
164 this.animations = new AnimationLoader(this.images, manifest.animations);
165
166 /**
167 * The scenes for the game.
168 * @member {SceneManager}
169 */
170 this.scenes = new SceneManager();
171 this.scenes.add("loading", makeLoadingScene(this, canvas, "title"));
172}
173/**
174 * Test if all the game's assets are loaded.
175 * @returns {boolean}
176 */
177Game.prototype.isLoaded = function() {
178 return this.images.allLoaded() &&
179 this.sounds.allLoaded() &&
180 this.fonts.allLoaded() &&
181 this.animations.allLoaded();
182};
183/**
184 * Determine the percent of the game's assets that are loaded. This is useful for drawing a loading bar.
185 * @returns {number} A number between 0 and 1
186 */
187Game.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 * Test if the game is running within a Chrome App.
200 * @returns {boolean}
201 */
202Game.prototype.isChromeApp = function() {
203 return window.chrome && window.chrome.app && window.chrome.app.runtime;
204};
205
206module.exports = Game;