Skip to content

Commit 2531d94

Browse files
committed
and input bar
1 parent a315b3f commit 2531d94

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/game/InputBar.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Phaser from "phaser";
2+
3+
export class InputBar {
4+
private scene: Phaser.Scene;
5+
private graphics: Phaser.GameObjects.Graphics;
6+
private width: number;
7+
private height: number;
8+
private barHeight: number = 6;
9+
private barColor: number = 0x0f380f;
10+
11+
constructor(scene: Phaser.Scene) {
12+
this.scene = scene;
13+
this.width = scene.cameras.main.width;
14+
this.height = scene.cameras.main.height;
15+
16+
this.graphics = scene.add.graphics().setDepth(999);
17+
this.graphics.setScrollFactor(0);
18+
}
19+
20+
update(lValue: number, rValue: number) {
21+
const centerX = this.width / 2;
22+
const barY = this.height - this.barHeight;
23+
const barWidth = this.width / 2;
24+
const leftWidth = barWidth * lValue;
25+
const rightWidth = barWidth * rValue;
26+
27+
this.graphics.clear();
28+
29+
if (lValue > 0) {
30+
this.graphics.fillStyle(this.barColor);
31+
this.graphics.fillRect(
32+
centerX - leftWidth - 1,
33+
barY,
34+
leftWidth,
35+
this.barHeight
36+
);
37+
}
38+
if (rValue > 0) {
39+
this.graphics.fillStyle(this.barColor);
40+
this.graphics.fillRect(centerX + 1, barY, rightWidth, this.barHeight);
41+
}
42+
}
43+
44+
destroy() {
45+
this.graphics.destroy();
46+
}
47+
}

src/game/scenes/GameScene.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PlaceholderAssets } from "../createPlaceholderAssets";
44
import { AnalogKey, AnalogReport } from "../../components/ConnectDevice";
55
import { SpriteAssets } from "../createSpriteAssets";
66
import { Obstacle } from "../Obstacle";
7+
import { InputBar } from "../InputBar";
78

89
export class GameScene extends Phaser.Scene {
910
private player!: Player;
@@ -29,6 +30,10 @@ export class GameScene extends Phaser.Scene {
2930

3031
private gameEnded: boolean = false;
3132

33+
private inputBar!: InputBar;
34+
private analogL: number = 0;
35+
private analogR: number = 0;
36+
3237
constructor() {
3338
super("GameScene");
3439
}
@@ -42,10 +47,10 @@ export class GameScene extends Phaser.Scene {
4247

4348
onAnalogReport = (event: AnalogReport) => {
4449
const { data } = event;
45-
const aKey = data.find((d) => d.key === AnalogKey.A)?.value ?? 0;
46-
const dKey = data.find((d) => d.key === AnalogKey.D)?.value ?? 0;
50+
this.analogL = data.find((d) => d.key === AnalogKey.A)?.value ?? 0;
51+
this.analogR = data.find((d) => d.key === AnalogKey.D)?.value ?? 0;
4752

48-
this.horizontal = dKey - aKey;
53+
this.horizontal = this.analogR - this.analogL;
4954
};
5055

5156
preload() {
@@ -122,6 +127,8 @@ export class GameScene extends Phaser.Scene {
122127
);
123128
}
124129

130+
this.inputBar = new InputBar(this);
131+
125132
this.events.on("shutdown", this.cleanup, this);
126133
}
127134

@@ -152,10 +159,7 @@ export class GameScene extends Phaser.Scene {
152159
return o.isAlive();
153160
});
154161

155-
// Jump when space is pressed or screen is tapped (handled via pointer events)
156-
// if (this.input.keyboard && Phaser.Input.Keyboard.JustDown(this.jumpKey)) {
157-
// this.jump();
158-
// }
162+
this.inputBar.update(this.analogL, this.analogR);
159163
}
160164

161165
spawnObstacle() {
@@ -226,6 +230,10 @@ export class GameScene extends Phaser.Scene {
226230
this.player.destroy();
227231
}
228232

233+
if (this.inputBar) {
234+
this.inputBar.destroy();
235+
}
236+
229237
this.obstacles.forEach((o) => o.destroy());
230238
this.obstacles = [];
231239
}

0 commit comments

Comments
 (0)