forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.js
More file actions
301 lines (266 loc) · 8.77 KB
/
Copy pathscene.js
File metadata and controls
301 lines (266 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict';
goog.provide('app.Scene');
goog.require('app.AnimationPlayer');
goog.require('app.BlockRunner');
goog.require('app.InputEvent');
goog.require('app.ResultType');
goog.require('goog.style');
/**
* The main view for the maze game. Manages the gameplay viewport and
* graphics which appear to the right of the blockly scene.
*
* @param {Element} el root .scene element.
* @param {!app.Game} game instance.
* @param {!app.Blockly} blockly wrapper.
* @constructor
*/
app.Scene = class {
constructor(el, game, blockly) {
this.blockly_ = blockly;
this.player = new app.AnimationPlayer(el);
this.blockRunner_ = new app.BlockRunner(this, blockly);
this.cachedWindowHeight_ = null;
this.cachedWindowWidth_ = null;
this.el_ = el;
this.game = game;
/** @type {app.Level} */
this.level = null;
this.portraitMode_ = false;
this.scaleRatio_ = 1;
this.visible_ = false;
this.width_ = 0;
// The world stage
this.parentEl_ = el.parentNode;
this.underlayEl_ = el.parentNode.querySelector('.scene-underlay');
this.charactersEl_ = el.querySelector('.scene__characters');
this.buttonEl_ = el.querySelector('.scene__play');
// Bind handlers
this.calculateViewport_ = this.calculateViewport_.bind(this);
this.onClickRun_ = this.onClickRun_.bind(this);
this.onClickScene_ = this.onClickScene_.bind(this);
this.onClickUnderlay_ = this.onClickUnderlay_.bind(this);
// Calculate the viewport now and whenever the browser resizes.
window.addEventListener('resize', this.calculateViewport_, false);
window.requestAnimationFrame(() => this.calculateViewport_(true));
this.calculateViewport_();
// Other events
this.buttonEl_.addEventListener('click', this.onClickRun_, false);
this.el_.addEventListener('click', this.onClickScene_, false);
this.underlayEl_.addEventListener('click', this.onClickUnderlay_, false);
}
/**
* Clean up resources. Not really used atm as our iframe will be destroyed anyways when
* leaving this game.
*/
dispose() {
window.removeEventListener('resize', this.calculateViewport_, false);
this.buttonEl_.removeEventListener('click', this.onClickRun_, false);
this.el_.removeEventListener('click', this.onClickScene_, false);
this.underlayEl_.removeEventListener('click', this.onClickUnderlay_, false);
}
/**
* Resets the state of the scene for a new game.
*/
reset() {
this.level = null;
}
/**
* @return {boolean} whether we're in portrait mode
*/
getPortraitMode() {
return this.portraitMode_;
};
/**
* Changes the current level.
*
* @param {app.DanceLevel} level
*/
setLevel(level) {
this.level = level;
this.player.setLevel(level);
// Show the scene in portrait, then hide it after 3 seconds.
this.portraitToggleScene(true);
let introAnimation = !level.freestyle && level.introAnimation();
if (introAnimation) {
this.blockRunner_.runAnimation(introAnimation);
} else {
// show immediately, we're in freestyle mode
this.showTutorials_();
}
}
/**
* Resets state to the current level. Need to reset graphics as well when changing levels
* or restarting the level. Does not need to reset graphics after doing a dry run of
* blocks.
*/
restartLevel() {
this.blockRunner_.restartLevel();
}
/**
* Configures scaling and width of scene elements. Runs on init and resize.
* @private
*/
calculateViewport_(force = false) {
// Blockly spams window.onresize for their scrollbar logic. Let's ignore those.
if (!force &&
window.innerHeight === this.cachedWindowHeight_ &&
window.innerWidth === this.cachedWindowWidth_) {
return;
}
this.cachedWindowHeight_ = window.innerHeight;
this.cachedWindowWidth_ = window.innerWidth;
// Calculate width and scaling for the scene, with special handling for portrait-like
// windows.
var sceneHeight = window.innerHeight;
var sceneAspectRatio = Math.max(
Math.min(window.innerWidth / 2 / sceneHeight,
app.Scene.CONTENT_ASPECT_RATIO_MAX),
app.Scene.CONTENT_ASPECT_RATIO_MIN
);
var sceneWidth = sceneHeight * sceneAspectRatio;
var portraitMode = false;
var workspaceWidth = window.innerWidth - this.blockly_.getToolbarWidth();
if (workspaceWidth - sceneWidth < app.Constants.BLOCKLY_MIN_WIDTH) {
portraitMode = true;
sceneWidth = window.innerWidth - app.Constants.EDGE_MIN_WIDTH;
}
this.portraitMode_ = portraitMode;
this.parentEl_.classList.toggle('responsive', this.portraitMode_);
this.width_ = sceneWidth;
this.scaleRatio_ = Math.min(sceneWidth / 540, sceneHeight / app.Scene.CONTENT_HEIGHT);
// Apply width and scaling in DOM.
this.el_.style.fontSize = this.scaleRatio_ * 10 + 'px';
this.el_.style.width = sceneWidth + 'px';
this.charactersEl_.style.transform = 'scale(' + Math.min(1, this.scaleRatio_) + ')';
}
/**
* Click handler for scene. Shows tools.
* @private
*/
onClickScene_() {
this.portraitToggleScene(true, true);
}
/**
* Click handler for overlay. Shows play area.
* @private
*/
onClickUnderlay_() {
this.portraitToggleScene(false, true);
}
/**
* Conditionally show or hide the scene with animation in portrait mode.
* @param {boolean} visible true if the scene should be shown.
*/
portraitToggleScene(visible, userAction) {
if (userAction) {
this.game.dismissTutorial('codeboogie_tray.mp4');
}
this.parentEl_.classList.toggle('show', !visible);
}
/**
* Click handler on play button. Starts execution of the blockly code.
* @private
*/
onClickRun_(ev) {
ev.stopPropagation(); // don't trigger scene click
this.buttonEl_.blur();
this.game.dismissTutorial();
if (this.portraitMode_) {
this.portraitToggleScene(true);
window.setTimeout(() => this.blockRunner_.execute(), app.Constants.SCENE_TOGGLE_DURATION);
} else {
this.blockRunner_.execute();
Klang.triggerEvent('generic_button_click');
}
}
showTutorials_() {
const tutorials = ['codeboogie.gif'];
if (this.portraitMode_) {
// This isn't perfect because transitions back from mobile mode will retain the tray, but it's
// fine for now.
tutorials.unshift('codeboogie_tray.mp4'); // put before regular tutorial
}
this.game.showTutorial(tutorials);
}
/**
* Callback after running the blockly code. Presents user with smart
* success or failure messages.
*
* @param {app.LevelResult} result of execution.
*/
onFinishExecution(result) {
if (!result.showResult()) {
// this occurs on the "follow these steps" guide
this.showTutorials_();
return;
}
if (this.level === this.game.levels[this.game.levels.length - 1]) {
result.isFinalLevel = true;
}
if (result.levelComplete) {
Klang.triggerEvent('cb_win');
this.game.successResult.show(result);
} else {
Klang.triggerEvent('cb_fail');
this.game.failureResult.show(result);
}
}
/**
* Returns the width the scene steals from the blockly workspace.
* @return {number} minimum size of scene in pixels.
*/
getWidth() {
if (!this.visible_) {
return 0;
} else if (this.portraitMode_) {
return app.Constants.EDGE_MIN_WIDTH;
} else {
return this.width_;
}
}
/**
* Sets if the maze viewport should be visible or not. Depends on the active level.
* @param {boolean} visible should be true to show the maze.
*/
toggleVisibility(visible) {
this.visible_ = visible;
// Keep it simple for now. Translation animation might conflict with portrait dragging.
this.el_.hidden = !visible;
this.underlayEl_.hidden = !visible;
}
};
/**
* Base height of scene contents. Characters will be scaled when smaller.
* @type {number}
*/
app.Scene.CONTENT_HEIGHT = 800;
/**
* Maximum aspect ratio for scene.
* @type {number}
*/
app.Scene.CONTENT_ASPECT_RATIO_MAX = 1;
/**
* Minimum aspect ratio for scene before going into portrait mode.
* @type {number}
*/
app.Scene.CONTENT_ASPECT_RATIO_MIN = 1 / 2;
/**
* Minimum margin from scene contents to edge.
* @type {number}
*/
app.Scene.SCENE_PADDING = 20;