diff --git a/src/avatar.ts b/src/avatar.ts index 33cd61e..dbb7302 100644 --- a/src/avatar.ts +++ b/src/avatar.ts @@ -1,6 +1,5 @@ import { Tweens } from "phaser"; import constants from "./constants"; -import emitter from "./emitter"; import { hs } from "./util"; export default class Avatar { @@ -10,7 +9,7 @@ export default class Avatar { container: Phaser.GameObjects.Container; customImage = false; label: Phaser.GameObjects.Text | null; - rect?: Phaser.GameObjects.Rectangle; + rect?: Phaser.GameObjects.Rectangle | null; score: integer; scoreLabel: Phaser.GameObjects.Text | null; sprite: Phaser.GameObjects.Image; @@ -77,6 +76,8 @@ export default class Avatar { } const body = this.container.body as Phaser.Physics.Arcade.Body; + body.setCollideWorldBounds(true, 0.9, 0, true); + const direction = Math.random() < 0.5 ? -1 : 1; const velocity = Math.random() * @@ -114,13 +115,6 @@ export default class Avatar { this.rect.setPosition(body.x, body.y); } - if ( - this.container.y + Math.ceil(this.container.height / 2) >= - constants.SCREEN_HEIGHT - ) { - return emitter.emit("lose", this); - } - if (this.chute.visible) { if (this.container.body.velocity.y > this.chuteGravity) this.container.body.velocity.y = this.chuteGravity; diff --git a/src/game.ts b/src/game.ts index 617f235..71de93d 100644 --- a/src/game.ts +++ b/src/game.ts @@ -42,7 +42,7 @@ export default class Game extends Phaser.Scene { emitter.on("resetdrop", this.onResetDrop, this); emitter.on("startdrop", this.onStartDrop, this); - setTimeout(this.tidyScores.bind(this), constants.TIDY_SCHEDULE); + setInterval(this.tidyScores.bind(this), constants.TIDY_SCHEDULE); this.tidyScores(); } @@ -61,13 +61,6 @@ export default class Game extends Phaser.Scene { this.load.image("drop4", "drop4.png"); this.load.image("drop5", "drop5.png"); this.load.image("pad", "pad.png"); - /* - this.load.setBaseURL(); - this.load.image( - "emotesv2_ffbe3ae7fb2e47bbafa9d9557bb117ed", - "https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_ffbe3ae7fb2e47bbafa9d9557bb117ed/default/dark/3.0", - ); - */ } create() { @@ -80,8 +73,7 @@ export default class Game extends Phaser.Scene { .setOrigin(0.5, 1) .setVisible(false) .setPosition(0, constants.SCREEN_HEIGHT); - - setTimeout(this.ready.bind(this), 100); + this.ready(); } ready(): void { @@ -90,17 +82,22 @@ export default class Game extends Phaser.Scene { return; } - this.dropGroup = this.physics.add.group({ - bounceX: 1, - bounceY: 1, - collideWorldBounds: true, - }); + this.dropGroup = this.physics.add.group(); this.physics.add.collider( this.dropGroup, this.dropGroup, this.crash.bind(this), ); + this.physics.world.on( + Phaser.Physics.Arcade.Events.WORLD_BOUNDS, + (obj: Phaser.Physics.Arcade.Body, _up: boolean, down: boolean) => { + if (!down) return; + const avatar = obj.gameObject.getData("avatar") as Avatar; + emitter.emit("lose", avatar); + }, + ); + this.pad.body.immovable = true; this.pad.body.setSize(this.pad.width, this.pad.height, true); this.physics.add.collider( @@ -117,16 +114,21 @@ export default class Game extends Phaser.Scene { this.pad.body.width, this.pad.body.height, ) - .setOrigin(0.5, 0) + .setOrigin(0.5, 1) .setDepth(1) - .setStrokeStyle(2, 0xff0ff); + .setStrokeStyle(2, 0xff0ff) + .setVisible(false); } update() { + if (!this.active) return; + for (const drop of this.droppersArray) { if (!drop.active) continue; drop.update(); } + + this.rect?.setPosition(this.pad?.x, this.pad?.y); } tidyScores() { @@ -150,9 +152,10 @@ export default class Game extends Phaser.Scene { Math.random() * (constants.SCREEN_WIDTH - this.pad.width), ); - if (hs.debug && this.rect) this.rect.x = this.pad.x; - this.pad.setVisible(true); + + if (hs.debug) this.rect?.setVisible(true); + console.debug(`Pad X Position: ${this.pad.x}`); } @@ -161,8 +164,12 @@ export default class Game extends Phaser.Scene { this.queue = false; this.droppersQueue.clear(); this.pad?.setVisible(false); + this.rect?.setVisible(false); - for (const drop of this.droppersArray) drop.container.destroy(); + for (const drop of this.droppersArray) { + drop.rect?.destroy(); + drop.container.destroy(); + } } resetTimer() { @@ -383,6 +390,7 @@ export default class Game extends Phaser.Scene { avatar.container.setActive(false); avatar.label?.destroy(); avatar.label = null; + avatar.rect?.setVisible(false); avatar.scoreLabel?.destroy(); avatar.scoreLabel = null; avatar.sprite.angle = 0;