Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
150 lines (133 loc) · 4.55 KB
/
Copy pathgame.js
File metadata and controls
150 lines (133 loc) · 4.55 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
const GameLayout = {
// Reference short-edge the art was sized against.
REFERENCE_UNIT: 420,
unit(width, height) {
return Math.min(width, height);
},
font(width, height, ratio, min, max) {
return Phaser.Math.Clamp(Math.round(this.unit(width, height) * ratio), min, max);
},
isLandscape(width, height) {
return width > height;
},
// How much to grow/shrink sprites for this screen, so a phone and a
// desktop show items at a comparable proportion of the playfield.
spriteScale(width, height) {
return Phaser.Math.Clamp(this.unit(width, height) / this.REFERENCE_UNIT, 0.7, 1.9);
},
/**
* Scale a sprite so its rendered width is exactly `targetWidth` pixels,
* keeping the artwork's aspect ratio. Works with art of any resolution,
* which is what lets GameData describe sizes in design units.
*/
fitWidth(sprite, targetWidth) {
const sourceWidth = sprite.width || 1;
sprite.setScale(targetWidth / sourceWidth);
return sprite;
},
/**
* Draw a tiled background that covers the screen without squashing the
* source image: the tile is scaled so one copy is at least as tall as the
* viewport, then repeats horizontally.
*/
coverTileSprite(scene, key) {
const { width, height } = scene.scale;
const tile = scene.add.tileSprite(width / 2, height / 2, width, height, key);
const source = scene.textures.get(key).getSourceImage();
const scale = Math.max(height / (source.height || height), 1);
tile.setTileScale(scale, scale);
return tile;
},
/**
* Run `handler` whenever the canvas is resized, and detach on shutdown so
* restarted scenes don't stack up listeners.
*/
onResize(scene, handler) {
scene.scale.on('resize', handler);
scene.events.once('shutdown', () => scene.scale.off('resize', handler));
scene.events.once('destroy', () => scene.scale.off('resize', handler));
},
/**
* Menu screens are laid out once in create(), so the simplest correct
* response to a rotation is to rebuild them. Debounced, because dragging a
* desktop window fires resize continuously.
*/
rebuildOnResize(scene) {
let pending = null;
this.onResize(scene, () => {
if (pending) pending.remove(false);
pending = scene.time.delayedCall(180, () => scene.scene.restart(scene.sys.settings.data));
});
}
};
const gameContainer = document.getElementById('game-container');
// The container is inset by the device's safe area, so measuring it (rather
// than the window) keeps the canvas backing store and its CSS size in step.
const viewportSize = () => ({
width: gameContainer.clientWidth || window.innerWidth,
height: gameContainer.clientHeight || window.innerHeight
});
const config = {
type: Phaser.AUTO,
parent: 'game-container',
backgroundColor: '#04202c',
scale: {
mode: Phaser.Scale.RESIZE,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: viewportSize().width,
height: viewportSize().height
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
input: {
activePointers: 3
},
scene: [
BootScene,
MainMenu,
CharacterSelection,
AchievementsScene,
SettingsScene,
CreditsScene,
GameScene,
PauseScene,
GameOverScene
]
};
const game = new Phaser.Game(config);
// Phaser's RESIZE mode already listens for window resizes, but Safari on iOS
// reports the new size a beat late after an orientation change.
let resizeTimer = null;
const syncGameSize = () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
const { width, height } = viewportSize();
game.scale.resize(width, height);
}, 60);
};
window.addEventListener('resize', syncGameSize);
window.addEventListener('orientationchange', syncGameSize);
// Audio can only start inside a user gesture. Unlock on the first real input
// and then get out of the way.
const unlockAudio = () => {
GameAudio.unlock();
window.removeEventListener('pointerdown', unlockAudio);
window.removeEventListener('keydown', unlockAudio);
window.removeEventListener('touchstart', unlockAudio);
};
window.addEventListener('pointerdown', unlockAudio);
window.addEventListener('keydown', unlockAudio);
window.addEventListener('touchstart', unlockAudio);
// Suspend audio while the tab is hidden so the game doesn't hum in the
// background on mobile.
document.addEventListener('visibilitychange', () => {
if (!GameAudio.ctx) return;
if (document.hidden) GameAudio.ctx.suspend();
else GameAudio.ctx.resume();
});
document.addEventListener('touchmove', (e) => e.preventDefault(), { passive: false });