Skip to content

Commit

Permalink
✨ reduce horizontal speed when bumping into wall
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Oct 4, 2023
1 parent 973a4c6 commit d298e0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
12 changes: 3 additions & 9 deletions src/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Tweens } from "phaser";
import constants from "./constants";
import emitter from "./emitter";
import { hs } from "./util";

export default class Avatar {
Expand All @@ -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;
Expand Down Expand Up @@ -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() *
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 28 additions & 20 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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() {
Expand All @@ -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 {
Expand All @@ -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(
Expand All @@ -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() {
Expand All @@ -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}`);
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d298e0f

Please sign in to comment.