Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/rooms/GameRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ export class GameRoom extends Room<RoomState> {
console.log(this.state.getMapInfo());
client.send("mapInfo", this.state.getMapInfo());
});

this.setSimulationInterval((deltaTime) => {
this.state.updateCapybara();

if (this.state.capybara) {
this.broadcast("capybaraUpdate", {
x: this.state.capybara.position.x,
y: this.state.capybara.position.y,
state: this.state.capybara.state
});
}
}, 500);
}

onJoin(client: Client, options: any) {
Expand Down
16 changes: 16 additions & 0 deletions src/rooms/schema/Capybara.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Schema, type } from "@colyseus/schema";
import { Position } from "./Position";

export class Capybara extends Schema {
@type("number") id: number;
@type(Position) position: Position;
@type("string") state: string;

constructor(x: number, y: number, state: string = "idle") {
super();
this.position = new Position()
this.position.x = x;
this.position.y = y;
this.state = state;
}
}
124 changes: 124 additions & 0 deletions src/rooms/schema/RoomState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { PlayerState } from "./PlayerState.js";
import { CrateState } from "./CrateState.js";
import { ButtonState } from "./ButtonState.js";
import { DoorState } from "./DoorState.js";
import { VentState } from "./VentState.js";
import { Capybara } from "./Capybara.js";

export class RoomState extends Schema {
@type(["string"]) grid = new ArraySchema<string>();
Expand All @@ -16,6 +18,10 @@ export class RoomState extends Schema {
@type(CrateState) crateState: CrateState = new CrateState();
@type(DoorState) doorState: DoorState = new DoorState();
@type(ButtonState) buttonState: ButtonState = new ButtonState();
@type(VentState) ventState: VentState = new VentState();
@type(Capybara) capybara: Capybara;

private capybaraPath: { x: number; y: number }[] = [];

loadRoomFromJson(jsonData: any) {
try {
Expand All @@ -35,6 +41,9 @@ export class RoomState extends Schema {
new Position().assign({ x: playerData.x, y: playerData.y })
);
}
this.ventState.spawnInitialVents();
this.spawnCapybara();

} catch (error) {
throw `Error loading room data: ${error}`;
}
Expand Down Expand Up @@ -108,6 +117,103 @@ export class RoomState extends Schema {
return true;
}

isWalkableForCapybara(x: number, y: number): boolean {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) return false;

const cell = this.getCellValue(x, y);

if (cell.startsWith("w")) return false;

if (this.crateState.getCrateAt(x, y)) return false;

if (!this.doorState.isOpenOrEmptyAt(x, y)) return false;

return true;
}

reconstructPath(parents: Map<string, {x: number; y: number}>, endNode: {x: number; y: number}): { x: number; y: number}[] {
const path = [endNode];
let current = endNode;
let parent = parents.get(`${current.x}_${current.y}`)

while (parent) {
current = parent;
const key = `${current.x}_${current.y}`
parent = parents.get(key)
path.push(current)
}

return path.reverse();
}

findPathToVent(): {x: number; y: number}[] | null {
const startNode = {
x: this.capybara.position.x,
y: this.capybara.position.y,
}

const queue: {x: number, y: number}[] = [];
queue.push(startNode);

const visited = new Set<string>();
visited.add(`${startNode.x}_${startNode.y}`);

const parents = new Map<string, { x: number, y: number }>();
const delta = [{x:0, y:1}, {x:0, y:-1}, {x:1, y:0}, {x:-1, y:0}];

while (!(queue.length === 0)) {
let current = queue.shift()!;
if (this.ventState.getVentAt(current.x, current.y) && this.ventState.isOpenOrEmptyAt(current.x, current.y)) {
return this.reconstructPath(parents, current);
}


for (const nextMove of delta) {
let [nextX, nextY] = [current.x + nextMove.x, current.y + nextMove.y];
let nextKey: string = `${nextX}_${nextY}`
if (visited.has(nextKey)) continue;
if (!this.isWalkableForCapybara(nextX, nextY)) continue;
if (!this.ventState.isOpenOrEmptyAt(nextX, nextY)) continue;

visited.add(nextKey);
parents.set(nextKey, current);
queue.push({x: nextX, y: nextY});
}
}
// console.log("no possible way :(")
return null
}

updateCapybara() {
if (!this.capybara) return;

if (this.capybaraPath.length === 0) {
const path = this.findPathToVent();

if (path && path.length > 1) {
path.shift();
this.capybaraPath = path;
}
}

if (this.capybaraPath.length > 0) {
const nextStep = this.capybaraPath.shift();

if (nextStep) {
if (!this.isWalkableForCapybara(nextStep.x, nextStep.y) || !this.ventState.isOpenOrEmptyAt(nextStep.x, nextStep.y)) {
this.capybaraPath = [];
return;
}

this.capybara.position.x = nextStep.x;
this.capybara.position.y = nextStep.y;
this.capybara.state = "run";
}
} else {
this.capybara.state = "idle";
}
}

spawnNewPlayer(sessionId: string, name: string = null) {
this.playerState.createPlayer(sessionId, name);
const player = this.playerState.players.get(sessionId);
Expand Down Expand Up @@ -185,6 +291,17 @@ export class RoomState extends Schema {
y: button.position.y,
pressed: button.pressed,
})),
vents: Array.from(this.ventState.vents.values()).map((vent) => ({
id: vent.id,
x: vent.position.x,
y: vent.position.y,
open: vent.open
})),
capybara: this.capybara ? {
x: this.capybara.position.x,
y: this.capybara.position.y,
state: this.capybara.state
} : null
};
}

Expand Down Expand Up @@ -266,4 +383,11 @@ export class RoomState extends Schema {
}
return doorsAndButtonsToUpdate;
}

spawnCapybara() {
const startingPos = new Position();
startingPos.x = 3;
startingPos.y = 4;
this.capybara = new Capybara(startingPos.x, startingPos.y)
}
}
79 changes: 79 additions & 0 deletions src/rooms/schema/VentState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Schema, MapSchema, type } from "@colyseus/schema";
import { Position } from "./Position";

export class Vent extends Schema {
@type("number") id: number;
@type(Position) position: Position = new Position();
@type("boolean") open: boolean = false;
}

export class VentState extends Schema {
@type( {map: Vent} )
vents = new MapSchema<Vent>();

private usedIDs = new Set<number>();
private nextAvailableId: number = 0;

private positionToVentId = new Map<string, string>();
private getPositionKey(x: number, y: number) {
return `${x}_${y}`;
}

createVent(x: number, y: number): Vent {
const id = this.nextAvailableId++;
this.usedIDs.add(id);

const vent = new Vent();
vent.id = id;
vent.position = new Position();
vent.position.x = x;
vent.position.y = y;

this.vents.set(vent.id.toString(), vent)
const key = this.getPositionKey(x, y);
this.positionToVentId.set(key, vent.id.toString())

return vent
}

removeVent(id: string): void {
const vent = this.vents.get(id);
if (!vent) return;
const key = this.getPositionKey(vent.position.x, vent.position.y);
this.positionToVentId.delete(key);
this.vents.delete(id);
}

onRoomDispose(): void {
this.vents.clear();
this.usedIDs.clear();
}

getVentAt(x: number, y: number): Vent | undefined {
const key = this.getPositionKey(x, y);
const ventId = this.positionToVentId.get(key);
return ventId ? this.vents.get(ventId) : null;
}

openVent(ventId: number) : void {
const vent = this.vents.get(ventId.toString());
if (!vent) return;
vent.open = true;
}

isOpenOrEmptyAt(x: number, y: number): boolean {
const vent = this.getVentAt(x, y);
if (vent) {
return vent.open;
}
return true;
}

spawnInitialVents() {
const vent1 = this.createVent(8, 1);
const vent2 = this.createVent(1, 2);

this.openVent(vent1.id);
}

}