-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.html
413 lines (353 loc) · 13.4 KB
/
float.html
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Float</title>
<style>* { padding: 0; margin: 0; }</style>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.22.0/dist/phaser.min.js"></script>
</head>
<body>
<canvas id='float'></canvas>
<script>
var colorHexMap = {
red: '#FF0000',
yellow: '#FFFF00',
blue: '#0000FF',
green: '#008000',
orange: '#FFA500',
purple: '#800080'
}
var gameConfig = {
radius: 25,
letters: {minYDelta: -50, valueDelta: -1},
bubble: {radius: 25, velocityXMin: -5, velocityXMax: 5, velocityYMin: -15, velocityYMax: -5, bounceX: 1, bounceY: 1},
bubbles: {maxCount: 15}
};
var titleConfig = {
bubble: {radius: 15, angleDelta: .01, length: 80},
};
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
class AudioLoop extends Phaser.Scene {
constructor () {
super({key: 'audioLoop'});
}
preload () {
this.load.audio('music', '/audio/DancyChords.mp3');
}
create () {
var gameMusic = this.sound.add('music');
gameMusic.play({loop: true});
}
}
class TitleScene extends Phaser.Scene {
constructor () {
super({key: 'titleScene'});
}
init () {
this.bubbles = [];
this.length = titleConfig['bubble']['length'];
this.angleDelta = titleConfig['bubble']['angleDelta'];
this.titleColors = {
red: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['red']), angle: 0},
orange: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['orange']), angle: 2*Math.PI/6},
yellow: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['yellow']), angle: 2*2*Math.PI/6},
green: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['green']), angle: 2*3*Math.PI/6},
blue: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['blue']), angle: 2*4*Math.PI/6},
purple: {hex: Phaser.Display.Color.ValueToColor(colorHexMap['purple']), angle: 2*5*Math.PI/6},
}
}
create () {
var x;
var y;
var bubble;
var titleText;
for (var key in this.titleColors) {
x = this.length * Math.cos(this.titleColors[key]['angle']) + this.scale.width / 2;
y = this.length * Math.sin(this.titleColors[key]['angle']) + this.scale.width / 2;
bubble = this.add.circle(x, y, titleConfig['bubble']['radius'], this.titleColors[key]['hex'].color);
bubble.setInteractive();
bubble.on('pointerdown', function (pointer, localX, localY, event) {
this.scene.launch('audioLoop');
this.scene.switch('gameScene');
}, this);
this.bubbles.push(bubble);
}
titleText = this.add.text(this.scale.width / 2, this.scale.height / 2.15, 'Float', { fontSize: '38px'});
titleText.setPosition((this.scale.width - titleText.width)/ 2, titleText.y);
titleText.setInteractive()
titleText.on('pointerdown', function (pointer, localX, localY, event) {
this.scene.launch('audioLoop');
this.scene.switch('gameScene');
}, this);
}
update () {
var i = 0
for (var key in this.titleColors){
this.bubbles[i].x = this.length * Math.cos(this.titleColors[key]['angle']) + this.scale.width / 2;
this.bubbles[i].y = this.length * Math.sin(this.titleColors[key]['angle']) + this.scale.height / 2;
this.titleColors[key]['angle'] += this.angleDelta;
i += 1;
}
}
}
class GameScene extends Phaser.Scene {
constructor () {
super({key: 'gameScene'});
}
init () {
this.splashText;
this.splashColor = {
name: null,
hex: null,
animation: [],
};
this.splashLetters=[];
this.bubbles = [];
this.physics.world.setBounds(0, 0, this.scale.width, this.scale.height, true, true, true, true);
this.activeColorHexMap = {};
for (var key in colorHexMap) {
this.activeColorHexMap[key] = colorHexMap[key];
}
}
create () {
var x;
var y;
var coordinatesXY = [];
var radius = gameConfig['bubble']['radius'];
var overlapping;
//
// pick a splash color
//
// reset the activeColorHexMap if needed
if (Object.keys(this.activeColorHexMap).length == 0) {
console.log('congratulations! you have gone through all of the colors');
// reset the activeColorHexMap (reset the game)
for (var key in colorHexMap) {
this.activeColorHexMap[key] = colorHexMap[key];
}
}
this.pickSplashColor();
//
// floating bubbles
//
// get the coordinates of the bubbles, make sure they aren't overlapping
for (var i=0; i<this.splashColor['name'].length; i++){
x = getRandomInt(radius, this.scale.width - radius);
y = getRandomInt(radius, this.scale.height - radius);
if (coordinatesXY.length == 0) {
coordinatesXY.push({x: x, y: y});
continue;
}
overlapping = false;
for (var j=0; j<coordinatesXY.length; j++) {
if (x - 2 * radius <= coordinatesXY[j]['x'] && coordinatesXY[j]['x'] <= x + 2 * radius && y - 2 * radius <= coordinatesXY[j]['y'] && coordinatesXY[j]['y']<= y + 2 * radius) {
i -= 1; // try again for a non-overlapping coordinate
overlapping = true;
break;
}
}
if (overlapping) {
continue;
}
coordinatesXY.push({x: x, y: y});
}
// then loop through again and add the bubbles at their coordinates
for (var i=0;i<this.splashColor['name'].length;i++){
this.bubbles.push(this.addBubble(coordinatesXY[i]['x'], coordinatesXY[i]['y']));
}
// and add physics between bubbles
for (var i=0;i < this.bubbles.length;i++){
for (var j=0;j<this.bubbles.length;j++){
if (j > i){
this.physics.add.collider(this.bubbles[i], this.bubbles[j]);
}
}
}
//
// add the splash text
//
this.addSplashText()
//
// set up the animation data structure for the splash text
//
for (var i=0; i<this.splashColor['name'].length; i++){
this.splashColor['animation'].push(
{
initialY: this.splashText.y,
minY: this.splashText.y + gameConfig['letters']['minYDelta'],
value: gameConfig['letters']['valueDelta'],
lettersAnimated: false
}
);
}
}
addBubble (coordindateX, coordindateY) {
var randomInt;
var randomColor;
var colorHexExcludeSplashMap = {};
var color;
var bubble;
// randomly choose one of the colors in colorHexMap
// but make sure the color is not the splashColor
for (var key in colorHexMap) {
if (key != this.splashColor['name']) {
colorHexExcludeSplashMap[key] = colorHexMap[key];
}
}
randomInt = getRandomInt(0, Object.keys(colorHexMap).length - 1);
randomColor = {
name: Object.keys(colorHexExcludeSplashMap)[randomInt],
hex: colorHexExcludeSplashMap[Object.keys(colorHexExcludeSplashMap)[randomInt]]
};
color = Phaser.Display.Color.ValueToColor(randomColor['hex']);
bubble = this.add.circle(coordindateX, coordindateY, gameConfig['bubble']['radius'], color.color);
bubble.setInteractive();
bubble.on('pointerdown', function (pointer, localX, localY, event) {
if (bubble.fillColor == Phaser.Display.Color.ValueToColor(this.splashColor['hex']).color) {
bubble.destroy();
} else {
// play an audio sound indicating wrong color
// and make the bubble wiggle or something
// change the color of the bubble to the splashColor
bubble.fillColor = Phaser.Display.Color.ValueToColor(this.splashColor['hex']).color;
}
}, this);
this.physics.add.existing(bubble);
bubble.body.setCollideWorldBounds();
bubble.body.setVelocity(
getRandomInt(gameConfig['bubble']['velocityXMin'], gameConfig['bubble']['velocityXMax']),
getRandomInt(gameConfig['bubble']['velocityYMin'], gameConfig['bubble']['velocityYMax'])).setBounce(
gameConfig['bubble']['bounceX'], gameConfig['bubble']['bounceY']);
return bubble;
}
addSplashText () {
var style = {fontSize: '64px'};
//
// add the splash text
//
// this is only to get the text width
this.splashText = this.add.text(0, 0, this.splashColor['name'], style).setVisible(false);
// reposition to center x & center y
var textWidth = this.splashText.width;
this.splashText.setPosition(
(this.scale.width - this.splashText.width)/2,
(this.scale.height - this.splashText.height)/2
);
// determine width of each letter so we can style each individually
var lettersWidth = this.splashText.width / this.splashColor['name'].length;
for (var i=0; i<this.splashColor['name'].length; i++){
this.splashLetters.push(this.add.text(this.splashText.x + i*lettersWidth, this.splashText.y, this.splashColor['name'][i], style));
}
}
pickSplashColor () {
// randomly pick a color from the activeColorHexMap
var randomInt = getRandomInt(0, Object.keys(this.activeColorHexMap).length);
this.splashColor['name'] = Object.keys(this.activeColorHexMap)[randomInt];
this.splashColor['hex'] = this.activeColorHexMap[Object.keys(this.activeColorHexMap)[randomInt]];
}
update () {
var bubblesColorChanged = true;
var bubblesDestroyed = true;
var lettersAnimated = true;
for (var i=0; i< this.bubbles.length; i++) {
if (this.bubbles[i].active) {
bubblesDestroyed = false;
break;
}
}
for (var i=0; i< this.bubbles.length; i++) {
if (this.bubbles[i].fillColor != Phaser.Display.Color.ValueToColor(this.splashColor['hex']).color) {
bubblesColorChanged = false;
break;
}
}
//
// animation
//
if (bubblesDestroyed) {
for (var i=0; i<this.splashColor['animation'].length; i++){
if (i != 0) { // this animates the letters one at a time
if (!this.splashColor['animation'][i-1]['lettersAnimated']) {
break;
}
}
if (!(this.splashColor['animation'][i]['lettersAnimated'])) {
this.splashLetters[i].setColor(this.splashColor['hex']);
if (this.splashLetters[i].y == this.splashColor['animation'][i]['minY']) {
this.splashColor['animation'][i]['value'] = 1; // reverse direction
}
this.splashLetters[i].y += this.splashColor['animation'][i]['value'];
}
if (this.splashLetters[i].y == this.splashColor['animation'][i]['initialY']) {
this.splashColor['animation'][i]['lettersAnimated'] = true;
}
}
}
//
// reset / next level
//
// check if letters animated
for (var i=0;i<this.splashColor['animation'].length; i++){
if (!this.splashColor['animation'][i]['lettersAnimated']) {
lettersAnimated = false;
break;
}
}
// there should be some word animation so that we can play audio of the word
if (lettersAnimated) {
// before you finish, there needs to be some congrats
this.splashText.destroy();
for (var i=0; i<this.splashLetters.length; i++){
this.splashLetters[i].destroy();
}
for (var i=0; i<this.bubbles.length; i++){
this.bubbles[i].destroy();
}
this.splashLetters=[];
this.bubbles = [];
delete this.activeColorHexMap[this.splashColor['name']];
this.splashColor = {
name: null,
hex: null,
animation: [],
};
this.create();
}
}
}
var titleScene = new TitleScene();
var gameScene = new GameScene();
var audioLoop = new AudioLoop();
const DEFAULT_HEIGHT = 600
const DEFAULT_WIDTH = (window.innerWidth / window.innerHeight) * DEFAULT_HEIGHT
var config = {
type: Phaser.WebGL,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT
},
title: 'Float',
version: '1.0.0',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false,
}
},
canvas: document.getElementById('float'),
};
var game = new Phaser.Game(config);
game.scene.add('titleScene', titleScene);
game.scene.add('gameScene', gameScene);
game.scene.add('audioLoop', audioLoop);
game.scene.start('titleScene');
</script>
</body>
</html>