From 17e428c37fff7e3b29a6d1ec07d93c7b79af7624 Mon Sep 17 00:00:00 2001 From: SomewhatMay Date: Thu, 31 Oct 2024 15:10:16 -0400 Subject: [PATCH] world bounds --- src/scenes/game/grass-container.ts | 2 +- src/scenes/game/index.ts | 4 ++-- src/scenes/game/mover.ts | 31 +++++++++++++++++++++++------- src/scenes/game/player.ts | 7 ++++--- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/scenes/game/grass-container.ts b/src/scenes/game/grass-container.ts index 3397d29..fc24029 100644 --- a/src/scenes/game/grass-container.ts +++ b/src/scenes/game/grass-container.ts @@ -20,7 +20,7 @@ export class GrassContainer { } populateGrass(width: number) { - for (let i = 0; i < width; i++) { + for (let i = -20; i < width-20; i++) { this.createGrass(i); } } diff --git a/src/scenes/game/index.ts b/src/scenes/game/index.ts index 3f9f50c..f79b9b1 100644 --- a/src/scenes/game/index.ts +++ b/src/scenes/game/index.ts @@ -85,8 +85,8 @@ export class Game extends Scene { this.interactables.push(new Interactable(this, this.worldGroup, info)); }); - this.player = new Player(this, this.cursors); this.mover = new Mover(this, this.worldGroup); + this.player = new Player(this, this.cursors, this.mover); this.ui = new UI(this); @@ -124,7 +124,7 @@ export class Game extends Scene { ((previous as unknown as number) + (value ? 1 : 0)) as unknown as boolean ) as unknown as number / Object.values(info.discovered).length; - return Math.floor(discovered * 100) / 100; + return Math.floor(discovered * 100); } return 0; diff --git a/src/scenes/game/mover.ts b/src/scenes/game/mover.ts index 3c612aa..81c5df8 100644 --- a/src/scenes/game/mover.ts +++ b/src/scenes/game/mover.ts @@ -1,10 +1,14 @@ import { GameObjects, Physics } from "phaser"; import { Game as MainGame } from "./index"; +import { screenSize } from "../../constants"; +import { GrassContainer } from "./grass-container"; export class Mover { static playerSpeed = 10; // pixels per 100ms - worldPos = 0; + worldPos = 0; // player oriented + lowerWorldBound = -500; // px, exclusive + upperWorldBound = 2000; // px, exclusive constructor( private scene: MainGame, @@ -12,7 +16,7 @@ export class Mover { ) {} moveWorld(offset: number) { - this.worldPos += offset; + this.worldPos -= offset; this.worldGroup.getChildren().forEach((_child) => { const child = _child as GameObjects.Image; @@ -20,11 +24,24 @@ export class Mover { }); } - update(_: number, delta: number) { - if (this.scene.movingLeft) { - this.moveWorld((Mover.playerSpeed * delta) / 10); - } else if (this.scene.movingRight) { - this.moveWorld((-Mover.playerSpeed * delta) / 10); + /** + * direction: number - player oriented (opposite of world oriented) + */ + canMove(direction: number): boolean { + if (this.worldPos > this.lowerWorldBound && this.worldPos < this.upperWorldBound) { + return true; + } else if (this.worldPos <= this.lowerWorldBound && direction === 1 || this.worldPos >= this.upperWorldBound && direction === -1) { + return true; } + + return false; + } + + update(_: number, delta: number) { + if (this.scene.movingLeft && this.canMove(-1)) { + this.moveWorld((Mover.playerSpeed * delta) / 10); + } else if (this.scene.movingRight && this.canMove(1)) { + this.moveWorld((-Mover.playerSpeed * delta) / 10); + } } } diff --git a/src/scenes/game/player.ts b/src/scenes/game/player.ts index 889f6c0..a30f716 100644 --- a/src/scenes/game/player.ts +++ b/src/scenes/game/player.ts @@ -10,7 +10,8 @@ export class Player { constructor( private scene: MainGame, - private cursors: Phaser.Types.Input.Keyboard.CursorKeys | undefined + private cursors: Phaser.Types.Input.Keyboard.CursorKeys | undefined, + private mover: Mover ) { this.sprite = scene.add .sprite( @@ -41,7 +42,7 @@ export class Player { update(_: number, __: number): void { if (this.cursors) { - if (this.scene.movingLeft) { + if (this.scene.movingLeft && this.mover.canMove(-1)) { this.sprite.setOrigin(0.53, 1); this.sprite.flipX = true; @@ -49,7 +50,7 @@ export class Player { this.sprite.play("run"); this.wasWalking = true; } - } else if (this.scene.movingRight) { + } else if (this.scene.movingRight && this.mover.canMove(1)) { this.sprite.setOrigin(0.47, 1); this.sprite.flipX = false;