Skip to content

Commit

Permalink
world bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
SomewhatMay committed Oct 31, 2024
1 parent b3b39ad commit 17e428c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/scenes/game/grass-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/scenes/game/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
31 changes: 24 additions & 7 deletions src/scenes/game/mover.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
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,
private worldGroup: Physics.Arcade.StaticGroup
) {}

moveWorld(offset: number) {
this.worldPos += offset;
this.worldPos -= offset;

this.worldGroup.getChildren().forEach((_child) => {
const child = _child as GameObjects.Image;
child.setPosition(child.x + offset, child.y);
});
}

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);
}
}
}
7 changes: 4 additions & 3 deletions src/scenes/game/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -41,15 +42,15 @@ 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;

if (!this.wasWalking) {
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;

Expand Down

0 comments on commit 17e428c

Please sign in to comment.