-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpart2.js
More file actions
413 lines (340 loc) · 10.8 KB
/
part2.js
File metadata and controls
413 lines (340 loc) · 10.8 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
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
let arrow;
let bubble;
let bubblesGroup;
let initialBubble;
let nextBubble;
let leftWall;
let rightWall;
let topWall;
let grid = [];
const bubbleSize = 32;
const canvasHeight = 500;
const canvasWidth = 592;
let colors = ['red','pink','blue','green','yellow','purple'];
const rows =15;
const columns =17;
const filledRows = 9;
const marginX = bubbleSize;
const marginY = bubbleSize;
let currentBubbleColor = Phaser.Utils.Array.GetRandom(colors);
let nextBubbleColor = Phaser.Utils.Array.GetRandom(colors);
function preload() {
this.load.image('red','assets/red.png');
this.load.image('pink','assets/pink.png');
this.load.image('blue','assets/blue.png');
this.load.image('green','assets/green.png');
this.load.image('yellow','assets/yellow.png');
this.load.image('purple','assets/purple.png');
this.load.image('arrow','assets/arrow.png');
this.load.image('backgroundImage','assets/background.png');
this.load.image('restartButtonImage','assets/restart.png');
this.load.audio('bubblepop','assets/bubble.mp3');
this.load.audio('bubbleshoot','assets/shoot.mp3');
}
function create() {
setCanvasStyle();
createBackground.call(this);
createWalls.call(this);
createRestartButton.call(this);
createBubbleElements.call(this);
setupInputHandlers.call(this);
loadSounds.call(this);
}
function setCanvasStyle() {
const canvas = document.querySelector("canvas");
canvas.style.borderRadius= "20px";
}
function createBackground() {
const sectionBackground = this.add.image(311,264,"backgroundImage");
sectionBackground.setDisplaySize(599,496);
sectionBackground.setOrigin(0.5);
}
function createWalls() {
this.createWall = (x,y,displayWidth,displayHeight) => {
const wall = this.add.rectangle(x,y,displayWidth,displayHeight,0x000000,0);
this.physics.add.existing(wall,true);
wall.body.setSize(displayWidth,displayHeight);
return wall;
};
leftWall = this.createWall(20,510,10,1000);
rightWall = this.createWall(600,510,10,1000);
topWall = this.createWall(295,11,575,10);
}
function createRestartButton() {
const restartButton = this.add.image(675,60,"restartButtonImage").setInteractive();
restartButton.on("pointerdown",()=>{
this.scene.restart();
grid = [];
});
restartButton.on("pointerover",()=> this.input.setDefaultCursor("pointer"));
restartButton.on("pointerout",()=> this.input.setDefaultCursor("default"));
}
function createBubbleElements() {
bubblesGroup = this.physics.add.group();
createBubbles(this);
arrow = this.add.image(config.width/2-50,546,"arrow").setOrigin(0.5,1).setScale(0.3);
initialBubble = this.add.image(config.width/2-50,550-bubbleSize/2,currentBubbleColor).setOrigin(0.5,0).setAlpha(1);
nextBubble = this.add.image(bubbleSize,550-bubbleSize/2,nextBubbleColor).setOrigin(0.5,0).setAlpha(1);
}
function setupInputHandlers() {
this.input.on("pointermove",(pointer)=>{
let angle = Phaser.Math.Angle.Between(arrow.x,arrow.y,pointer.x,pointer.y)+Math.PI/2;
arrow.setRotation(angle);
});
this.input.on("pointerdown",(pointer)=> {
if(pointer.y>(rows+0.5)*bubbleSize) return;
shootBubble.call(this);
});
}
function loadSounds() {
bubbleSound = this.sound.add("bubblepop");
bubbleshoot = this.sound.add("bubbleshoot");
}
function createBubbles(scene) {
for(let row=1;row<=rows;row++) {
const rowArray = [];
for(let col=1;col<=columns;col++) {
const x = col*bubbleSize-(row%2 === 0 ? bubbleSize/2:bubbleSize)+marginX;
const y = row*bubbleSize-bubbleSize+marginY;
const centerX = x+bubbleSize/2;
const centerY = y+bubbleSize/2;
if(row<= filledRows) {
const color = Phaser.Utils.Array.GetRandom(colors);
const bubble = scene.physics.add.image(centerX,centerY,color);
bubble.setOrigin(0.5,0.5);
bubble.body.setImmovable(true);
bubblesGroup.add(bubble);
rowArray.push({centerX,centerY,color,bubble});
} else {
rowArray.push({centerX,centerY,color:"blank",bubble});
}
}
grid.push(rowArray);
}
}
function shootBubble() {
bubbleshoot.play();
const angle = arrow.rotation;
const bubble = createMovingBubble.call(this,angle);
updateNextBubbleTextures();
this.physics.add.collider(bubble,leftWall);
this.physics.add.collider(bubble,rightWall);
this.physics.add.overlap(bubble,[bubblesGroup,topWall],async(movingBubble,stationaryObject)=>{
handleBubbleCollision.call(this,movingBubble,stationaryObject);
});
}
function createMovingBubble(angle) {
const bubble = this.physics.add.image(arrow.x,arrow.y,currentBubbleColor);
bubble.body.setCircle(bubbleSize/4);
bubble.body.setVelocity(
Math.cos(angle-Math.PI/2)*800,
Math.sin(angle-Math.PI/2)*800
);
bubble.body.setBounce(1);
return bubble;
}
function updateNextBubbleTextures() {
if(colors.includes(nextBubbleColor)) {
currentBubbleColor = nextBubbleColor;
} else {
currentBubbleColor = Phaser.Utils.Array.GetRandom(colors);
}
initialBubble.setTexture(currentBubbleColor);
nextBubbleColor = Phaser.Utils.Array.GetRandom(colors);
nextBubble.setTexture(nextBubbleColor);
}
async function handleBubbleCollision(movingBubble,stationaryObject) {
const dx = movingBubble.x - stationaryObject.x;
const dy = movingBubble.y - stationaryObject.y;
const distance = Math.sqrt(dx*dx+dy*dy);
if(distance>bubbleSize && stationaryObject !== topWall)
return;
movingBubble.body.stop();
let closestSquare = findClosestEmptySquare(movingBubble.x,movingBubble.y);
let snappedX = closestSquare.centerX;
let snappedY = closestSquare.centerY;
movingBubble.destroy();
const snappedBubble = this.physics.add.image(snappedX,snappedY,movingBubble.texture.key);
snappedBubble.setOrigin(0.5,0.5);
snappedBubble.setImmovable(true);
snappedBubble.body.setCircle(bubbleSize/2);
bubblesGroup.add(snappedBubble);
updateGridWithBubble(snappedX,snappedY,movingBubble.texture.key,snappedBubble);
let connectedBalls = findConnectedBalls(snappedX,snappedY,movingBubble.texture.key);
await removeConnectedBalls(connectedBalls,bubbleSound);
await removeFloatingIslands();
updateAvailableColors();
}
function findClosestEmptySquare(x,y) {
let closestSquare = null;
let closestDistance = Infinity;
for(let row=0; row<grid.length; row++) {
for (let col = 0; col<grid[row].length;col++) {
const square = grid[row][col];
if(square.color === "blank") {
const distance = Math.sqrt(
Math.pow(square.centerX-x,2) + Math.pow(square.centerY-y,2)
);
if(distance<closestDistance) {
closestDistance = distance;
closestSquare = square;
}
}
}
}
return closestSquare;
}
function updateGridWithBubble(x,y,color,bubble) {
const col = Math.floor(((x-marginX)- bubbleSize/2)/bubbleSize);
const row = Math.floor(((y-marginY)-bubbleSize)/bubbleSize)+1;
if(row >=0 && row<rows && col>=0 && col<columns) {
grid[row][col] = {
centerX: col*bubbleSize+bubbleSize/2+(row%2 === 0 ? 0 : bubbleSize/2)+marginX,
centerY: row*bubbleSize+bubbleSize/2+marginY,
color:color,
bubble:bubble
};
} else {
console.warn("Bubble us outside the grid bounds!");
}
}
function findConnectedBalls(x,y,color) {
const col = Math.floor(((x-marginX)-bubbleSize/2)/bubbleSize);
const row = Math.floor(((y-marginY)-bubbleSize)/bubbleSize)+1;
const visited = new Set();
const connectedBalls = [];
function isValid(row,col) {
return row >=0 && row < rows && col >=0 && col < columns;
}
function dfs(r,c) {
const key = `${r},${c}`;
if(!isValid(r,c) || visited.has(key) || grid[r][c].color !== color) {
return;
}
visited.add(key);
connectedBalls.push({row:r,col:c, ...grid[r][c]});
const offsets = r%2 === 0
? [[-1,-1],[-1,0],[0,-1],[0,1],[1,-1],[1,0]]
: [[-1,0],[-1,1],[0,-1],[0,1],[1,0],[1,1]];
for(const [dr,dc] of offsets) {
dfs(r+dr,c+dc);
}
}
dfs(row,col);
return connectedBalls.length>2 ? connectedBalls : [];
}
function removeConnectedBalls(connectedBalls, sound) {
return new Promise((resolve)=>{
if(connectedBalls.length === 0) {
sound.play();
resolve();
return;
}
let delay = 0;
let completed =0;
connectedBalls.forEach(({row,col}) => {
setTimeout(()=> {
const bubble = grid[row][col].bubble;
if(bubble) {
sound.play();
bubble.destroy();
}
grid[row][col] = {
centerX: grid[row][col].centerX,
centerY: grid[row][col].centerY,
color: "blank",
bubble: null,
};
completed +=1;
if(completed === connectedBalls.length) {
resolve();
}
},delay);
delay +=80;
});
});
}
function removeFloatingIslands() {
const rows = grid.length;
const columns = grid[0].length;
const visited = createVisitedArray(rows,columns);
markConnectedBubbles();
const floatingBalls = removeUnvisitedBubbles();
function createVisitedArray(rows,columns) {
return Array.from({length: rows},()=> Array(columns).fill(false));
}
function getNeighbors(row,col) {
return [
[row-1,col],
[row+1,col],
[row,col-1],
[row,col+1],
[row-1,col+(row%2 === 0 ? -1 : 1)],
[row+1,col+(row%2 === 0 ? -1 : 1)],
];
}
function dfs(row,col) {
if(
row<0 || row >=rows ||
col<0 || col >=columns ||
visited[row][col] ||
!grid[row][col].bubble
)return;
visited[row][col] = true;
for (const [neighborRow, neighborCol] of getNeighbors(row,col)) {
dfs(neighborRow,neighborCol);
}
}
function markConnectedBubbles() {
for( let col=0; col<columns; col++) {
if(grid[0][col].bubble && !visited[0][col]) {
dfs(0,col);
}
}
}
function removeUnvisitedBubbles() {
const floatingBalls = [];
for(let row=0; row<rows; row++) {
for(let col=0; col<columns;col++) {
if(grid[row][col].bubble && !visited[row][col]) {
floatingBalls.push({row,col,bubble: grid[row][col].bubble});
grid[row][col].bubble.destroy();
grid[row][col] = {centerX: grid[row][col].centerX,centerY: grid[row][col].centerY,color: "blank",bubble: null};
}
}
}
return floatingBalls;
}
}
function updateAvailableColors() {
const presentColors = new Set();
for(let row of grid) {
for(let cell of row) {
if(cell.bubble) {
presentColors.add(cell.color);
}
}
}
if(colors.length!=presentColors.size) {
colors = colors.filter(color=>presentColors.has(color));
updateNextBubbleTextures();
}
}
const config = {
type:Phaser.AUTO,
width:750,
height:600,
backgroundColor: "#E8CDEA",
scene: {
preload: preload,
create: create
},
physics: {
default: "arcade",
arcade: {
gravity: {y:0},
debug: false,
},
},
};
const game = new Phaser.Game(config);