Skip to content

Commit fbb9999

Browse files
feat: add vents and capybara (#9)
* feat: add vents and capybara * feat: add speech bubbles * chore: apply prettier --------- Co-authored-by: jatoothless <hania.drozdowska@icloud.com>
1 parent 6f13a86 commit fbb9999

File tree

9 files changed

+125
-13
lines changed

9 files changed

+125
-13
lines changed

public/images/vent/vent-closed.png

583 Bytes
Loading

public/images/vent/vent-open.png

467 Bytes
Loading

src/game/hooks/useCables.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function useCables(room: Room | null) {
5656
if (!room) return;
5757
room.onMessage("mapInfo", applyMapInfo);
5858
room.onMessage("cablesUpdate", applyCablesUpdate);
59-
room.onMessage("playerDamaged", (payload: any) => {
59+
room.onMessage("playerDamaged", () => {
6060
// opcjonalnie: efekt kliencki (np. flash) - tutaj tylko log
6161
// console.log("playerDamaged", payload);
6262
});
@@ -68,8 +68,11 @@ export function useCables(room: Room | null) {
6868

6969
return () => {
7070
try {
71-
room?.offMessage("mapInfo", applyMapInfo);
72-
room?.offMessage("cablesUpdate", applyCablesUpdate);
71+
const maybeRoom = room as Room & {
72+
offMessage?: (type: string, cb: (...args: any[]) => void) => void;
73+
};
74+
maybeRoom.offMessage?.("mapInfo", applyMapInfo);
75+
maybeRoom.offMessage?.("cablesUpdate", applyCablesUpdate);
7376
} catch (e) {}
7477
};
7578
}, [room, applyMapInfo, applyCablesUpdate]);

src/phaser/entities/capybara.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Entity } from "./entity";
2+
3+
export class Capybara extends Entity {
4+
constructor(scene: Phaser.Scene, x: number, y: number) {
5+
super(scene, x, y, "capybara");
6+
}
7+
}

src/phaser/mechanics/vent.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Mechanic } from "./mechanic";
2+
3+
export class Vent extends Mechanic {
4+
public id: number;
5+
private _isOpen: boolean;
6+
7+
constructor(
8+
scene: Phaser.Scene,
9+
x: number,
10+
y: number,
11+
id: number,
12+
isOpen: boolean,
13+
) {
14+
super(scene, x, y, isOpen ? "vent-open" : "vent-closed");
15+
this.id = id;
16+
this._isOpen = isOpen;
17+
}
18+
public set isOpen(value: boolean) {
19+
this._isOpen = value;
20+
this.changeTexture(value ? "vent-open" : "vent-closed");
21+
this.id = this.id;
22+
this._isOpen = this.isOpen;
23+
}
24+
25+
public get isOpen(): boolean {
26+
return this._isOpen;
27+
}
28+
}

src/phaser/scenes/main.ts

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import type { Laser as LaserType } from "../../types/laser";
88
import type {
99
MessageCratesUpdate,
1010
MessageDoorsAndButtonsUpdate,
11-
MessageLasersUpdate,
1211
MessageGenerateLines,
12+
MessageLasersUpdate,
1313
MessageMapInfo,
1414
MessageOnAddPlayer,
1515
MessageOnRemovePlayer,
1616
MessagePositionUpdate,
1717
} from "../../types/messages";
1818
import type { Player as PlayerType } from "../../types/player";
19+
import { Capybara } from "../entities/capybara";
1920
import { Crate } from "../entities/crate";
2021
import { Player } from "../entities/player";
2122
import { TILE_SIZE } from "../lib/const";
@@ -30,16 +31,19 @@ import { Button } from "../mechanics/button";
3031
import { Cable } from "../mechanics/cable";
3132
import { Door } from "../mechanics/door";
3233
import { Laser } from "../mechanics/laser";
34+
import { Vent } from "../mechanics/vent";
3335
import { SpeechBubble } from "../speech-bubbles/display-speech-bubble";
3436

3537
export class Main extends Phaser.Scene {
3638
private room!: Room;
39+
private capybara: Capybara | null = null;
3740
private players = new Map<string, Player>();
3841
private crates = new Map<number, Crate>();
3942
private buttons = new Map<string, Button>();
4043
private doors = new Map<string, Door>();
4144
private lasers = new Map<string, Laser>();
4245
private cables = new Map<string, Cable>();
46+
private vents = new Map<number, Vent>();
4347
private speechBubbles = new Map<string, SpeechBubble>();
4448
private bubbleTimer!: Phaser.Time.TimerEvent;
4549
private cursors!: Phaser.Types.Input.Keyboard.CursorKeys;
@@ -89,6 +93,13 @@ export class Main extends Phaser.Scene {
8993
"data/speech-bubble-data.json",
9094
);
9195

96+
// vents
97+
this.load.image("vent-open", "images/vent/vent-open.png");
98+
this.load.image("vent-closed", "images/vent/vent-closed.png");
99+
100+
// capybara
101+
this.load.image("capybara", "images/capybara/back_1.png");
102+
92103
// player textures
93104
for (const [index, textureKey] of PLAYER_TEXTURE_KEYS.entries()) {
94105
this.load.spritesheet(
@@ -160,6 +171,16 @@ export class Main extends Phaser.Scene {
160171
);
161172
this.cables.set(cable.cableId, c);
162173
}
174+
175+
if (message.vents) {
176+
for (const vent of message.vents) {
177+
this.addVent(vent);
178+
}
179+
}
180+
181+
if (message.capybara) {
182+
this.addCapybara(message.capybara);
183+
}
163184
});
164185

165186
room.onMessage("onAddPlayer", (message: MessageOnAddPlayer) => {
@@ -230,6 +251,24 @@ export class Main extends Phaser.Scene {
230251
}
231252
},
232253
);
254+
room.onMessage("line", (message: MessageGenerateLines) => {
255+
const player = this.players.get(message.sessionId);
256+
if (player !== undefined) {
257+
this.displayBubble(message.text, player, message.sessionId);
258+
}
259+
});
260+
261+
room.onMessage(
262+
"capybaraUpdate",
263+
(message: { x: number; y: number; state: string }) => {
264+
if (this.capybara) {
265+
this.capybara.setPosition(
266+
message.x * TILE_SIZE + TILE_SIZE / 2,
267+
message.y * TILE_SIZE + TILE_SIZE / 2,
268+
);
269+
}
270+
},
271+
);
233272

234273
room.onMessage("line", (message: MessageGenerateLines) => {
235274
const player = this.players.get(message.sessionId);
@@ -384,17 +423,30 @@ export class Main extends Phaser.Scene {
384423
this.doors.set(doorInfo.doorId, door);
385424
}
386425

387-
private addCable(cableInfo: CableType) {
388-
const cable = new Cable(
426+
private addVent(ventInfo: {
427+
id: number;
428+
x: number;
429+
y: number;
430+
open: boolean;
431+
}) {
432+
const vent = new Vent(
389433
this,
390-
cableInfo.x,
391-
cableInfo.y,
392-
cableInfo.cableId,
393-
cableInfo.color,
394-
cableInfo.pressed,
434+
ventInfo.x,
435+
ventInfo.y,
436+
ventInfo.id,
437+
ventInfo.open,
395438
);
396-
this.add.existing(cable);
397-
this.cables.set(cableInfo.cableId, cable);
439+
this.add.existing(vent);
440+
this.vents.set(ventInfo.id, vent);
441+
}
442+
443+
private addCapybara(capybaraInfo: { x: number; y: number }) {
444+
if (this.capybara) {
445+
this.capybara.destroy();
446+
}
447+
448+
this.capybara = new Capybara(this, capybaraInfo.x, capybaraInfo.y);
449+
this.add.existing(this.capybara);
398450
}
399451

400452
createMap(grid: string[][], width: number, height: number) {

src/types/capybara.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface Capybara {
2+
x: number;
3+
y: number;
4+
state: string;
5+
}

src/types/messages.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Button } from "../types/button";
2+
import type { Capybara } from "../types/capybara";
23
import type { Crate } from "../types/crate";
34
import type { Door } from "../types/door";
45
import type { Laser } from "../types/laser";
56
import type { Player } from "../types/player";
7+
import type { Vent } from "../types/vent";
68

79
export interface MessageMapInfo {
810
grid: string[][];
@@ -13,6 +15,15 @@ export interface MessageMapInfo {
1315
doors: Door[];
1416
buttons: Button[];
1517
lasers: Laser[];
18+
vents?: Vent[];
19+
capybara?: Capybara;
20+
cables?: {
21+
cableId: string;
22+
x: number;
23+
y: number;
24+
damage?: boolean;
25+
timer?: number;
26+
}[];
1627
}
1728

1829
export interface MessageCratesUpdate {

src/types/vent.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Vent {
2+
id: number;
3+
x: number;
4+
y: number;
5+
open: boolean;
6+
}

0 commit comments

Comments
 (0)