forked from phaserjs/template-vite-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3b39ad
commit 17e428c
Showing
4 changed files
with
31 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters