Skip to content

Commit 9956d8c

Browse files
committed
cleanup sort of
1 parent 78236ef commit 9956d8c

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/game/createSpriteAssets.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Phaser from "phaser";
2+
3+
export class SpriteAssets {
4+
static loadSpriteSheets(scene: Phaser.Scene): void {
5+
scene.load.spritesheet("switchy", "src/assets/switchy.png", {
6+
frameWidth: 16,
7+
frameHeight: 16,
8+
});
9+
}
10+
11+
static createSprites(scene: Phaser.Scene): void {
12+
scene.anims.create({
13+
key: "switchy-walk",
14+
frames: scene.anims.generateFrameNumbers("switchy", { start: 0, end: 2 }),
15+
frameRate: 6,
16+
repeat: -1,
17+
});
18+
}
19+
}

src/game/scenes/GameScene.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Phaser from "phaser";
22
import { PlaceholderAssets } from "../createPlaceholderAssets";
33
import { AnalogKey, AnalogReport } from "../../components/ConnectDevice";
4+
import { SpriteAssets } from "../createSpriteAssets";
45

56
export class GameScene extends Phaser.Scene {
67
private switchy!: Phaser.GameObjects.Sprite;
@@ -11,6 +12,8 @@ export class GameScene extends Phaser.Scene {
1112

1213
private device!: HIDDevice | undefined;
1314

15+
private horizontal: number = 0;
16+
1417
constructor() {
1518
super("GameScene");
1619
}
@@ -27,28 +30,15 @@ export class GameScene extends Phaser.Scene {
2730
const aKey = data.find((d) => d.key === AnalogKey.A)?.value ?? 0;
2831
const dKey = data.find((d) => d.key === AnalogKey.D)?.value ?? 0;
2932

30-
const horizontal = dKey - aKey;
31-
32-
// The W key moves from center to the top of the screen
33-
// The S key moves from center to the bottom of the screen
34-
// The A key moves from center to the left of the screen
35-
// The D key moves from center to the right of the screen
36-
this.switchy.setPosition(
37-
this.cameras.main.width / 2 + (horizontal * this.cameras.main.width) / 2,
38-
this.cameras.main.height - 16
39-
);
33+
this.horizontal = dKey - aKey;
4034
};
4135

4236
preload() {
4337
// Create placeholder assets directly in the game
4438
PlaceholderAssets.createAssets(this);
4539

46-
// Load specific image asset instead
47-
// this.load.image("switchy", "assets/switchy/idle.png");
48-
this.load.spritesheet("switchy", "src/assets/switchy.png", {
49-
frameWidth: 16,
50-
frameHeight: 16,
51-
});
40+
// Load sprites from asset handler
41+
SpriteAssets.loadSpriteSheets(this);
5242
}
5343

5444
create() {
@@ -65,14 +55,10 @@ export class GameScene extends Phaser.Scene {
6555
// Update physics settings for framerate independence
6656
this.physics.world.setFPS(60);
6757

68-
// Set up Switchy (the player character) using the hello texture to start
69-
// No physics for now
70-
this.anims.create({
71-
key: "switchy-walk",
72-
frames: this.anims.generateFrameNumbers("switchy", { start: 0, end: 2 }),
73-
frameRate: 6,
74-
repeat: -1,
75-
});
58+
// Load sprites from asset handler
59+
SpriteAssets.createSprites(this);
60+
61+
// Set up Switchy (the player character)
7662
this.switchy = this.add.sprite(gameWidth / 2, gameHeight - 16, "switchy");
7763
this.switchy.play("switchy-walk");
7864

@@ -91,6 +77,15 @@ export class GameScene extends Phaser.Scene {
9177
// Convert delta to seconds for easier calculations
9278
const deltaSeconds = delta / 1000;
9379

80+
// The A key moves from center to the left of the screen
81+
// The D key moves from center to the right of the screen
82+
// 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+
);
88+
9489
// Jump when space is pressed or screen is tapped (handled via pointer events)
9590
// if (this.input.keyboard && Phaser.Input.Keyboard.JustDown(this.jumpKey)) {
9691
// this.jump();

0 commit comments

Comments
 (0)