Skip to content

Commit 1c1662e

Browse files
committed
object-oriented switchy
1 parent 9956d8c commit 1c1662e

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/game/Player.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Phaser from "phaser";
2+
3+
export class Player {
4+
private scene: Phaser.Scene;
5+
private sprite: Phaser.GameObjects.Sprite;
6+
private horizontal: number = 0;
7+
8+
constructor(scene: Phaser.Scene, x: number, y: number, spriteName: string) {
9+
this.scene = scene;
10+
this.sprite = this.scene.add.sprite(x, y, spriteName);
11+
this.sprite.play("walk");
12+
}
13+
14+
update(horizontal: number) {
15+
this.horizontal = horizontal;
16+
const newX =
17+
this.scene.cameras.main.width / 2 +
18+
(this.horizontal * this.scene.cameras.main.width) / 2;
19+
this.sprite.setPosition(newX, this.scene.cameras.main.height - 16);
20+
}
21+
}

src/game/createSpriteAssets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class SpriteAssets {
1010

1111
static createSprites(scene: Phaser.Scene): void {
1212
scene.anims.create({
13-
key: "switchy-walk",
13+
key: "walk",
1414
frames: scene.anims.generateFrameNumbers("switchy", { start: 0, end: 2 }),
1515
frameRate: 6,
1616
repeat: -1,

src/game/scenes/GameScene.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Phaser from "phaser";
2+
import { Player } from "../Player";
23
import { PlaceholderAssets } from "../createPlaceholderAssets";
34
import { AnalogKey, AnalogReport } from "../../components/ConnectDevice";
45
import { SpriteAssets } from "../createSpriteAssets";
56

67
export class GameScene extends Phaser.Scene {
8+
private player!: Player;
9+
710
private switchy!: Phaser.GameObjects.Sprite;
811

912
private background!: Phaser.GameObjects.TileSprite;
@@ -59,8 +62,7 @@ export class GameScene extends Phaser.Scene {
5962
SpriteAssets.createSprites(this);
6063

6164
// Set up Switchy (the player character)
62-
this.switchy = this.add.sprite(gameWidth / 2, gameHeight - 16, "switchy");
63-
this.switchy.play("switchy-walk");
65+
this.player = new Player(this, gameWidth / 2, gameHeight - 16, "switchy");
6466

6567
// Collision optimizations
6668
// this.switchy.setCollideWorldBounds(true);
@@ -80,11 +82,7 @@ export class GameScene extends Phaser.Scene {
8082
// The A key moves from center to the left of the screen
8183
// The D key moves from center to the right of the screen
8284
// Update position according to horizontal
83-
this.switchy.setPosition(
84-
this.cameras.main.width / 2 +
85-
(this.horizontal * this.cameras.main.width) / 2,
86-
this.cameras.main.height - 16
87-
);
85+
this.player.update(this.horizontal);
8886

8987
// Jump when space is pressed or screen is tapped (handled via pointer events)
9088
// if (this.input.keyboard && Phaser.Input.Keyboard.JustDown(this.jumpKey)) {

0 commit comments

Comments
 (0)