Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
refactor player mgmt; add handler for player updates on the presence …
Browse files Browse the repository at this point in the history
…algorithm
  • Loading branch information
d-roak committed Jul 3, 2024
1 parent 938dcc7 commit c4dedd0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/lib/store/fakeP2P.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function initializePlayerData() {
const state = ["idle", "running", "walking", "jumping"][
Math.floor(Math.random() * 3)
] as "idle" | "running" | "walking" | "jumping";
addOrUpdatePlayer(id, x, y, z, rotationX, rotationY, rotationZ, state);
// addOrUpdatePlayer(id, x, y, z, rotationX, rotationY, rotationZ, state);
}
}

Expand Down
44 changes: 33 additions & 11 deletions src/lib/store/playersData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,38 @@ export class PlayerData implements Player {
state: "idle" | "running" | "walking" | "jumping";

constructor(
id: PlayerID,
x: number,
y: number,
z: number,
rotationX: number,
rotationY: number,
rotationZ: number,
state: "idle" | "running" | "walking" | "jumping",
player?: Player,
id?: PlayerID,
x?: number,
y?: number,
z?: number,
rotationX?: number,
rotationY?: number,
rotationZ?: number,
state?: "idle" | "running" | "walking" | "jumping",
) {
if (player) {
this.id = player.id;
this.position = player.position;
this.rotation = player.rotation;
this.state = player.state;
return;
}
if (!id) throw new Error("neither player nor id were provided");
this.id = id;
this.position = new Vector3(x, y, z);
this.rotation = new Euler(rotationX, rotationY, rotationZ);
this.state = state;

this.position = new Vector3(
x ?? randomCoordinate(),
y ?? randomCoordinate(),
z ?? randomCoordinate(),
);
this.rotation = new Euler(
rotationX ?? randomRotation(),
rotationY ?? randomRotation(),
rotationZ ?? randomRotation(),
);

this.state = state ?? "idle";
}

updatePosition(x: number, y: number, z: number) {
Expand All @@ -44,4 +63,7 @@ export class PlayerData implements Player {
}
}

const randomCoordinate = () => Math.random() * 10 - 5;
const randomRotation = () => Math.random() * Math.PI * 2;

export const players: Map<PlayerID, PlayerData> = new Map();
26 changes: 7 additions & 19 deletions src/lib/store/playersManager.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import { players, PlayerData } from "./playersData";
import type { PlayerID } from "./playersData";
import type { Player, PlayerID } from "./playersData";

export function addOrUpdatePlayer(
id: PlayerID,
x: number,
y: number,
z: number,
rotationX: number,
rotationY: number,
rotationZ: number,
state: "idle" | "running" | "walking" | "jumping",
): void {
const player = players.get(id);
export function addOrUpdatePlayer(input: Player): void {
const player = players.get(input.id);
if (player) {
player.updatePosition(x, y, z);
player.updateRotation(rotationX, rotationY, rotationZ);
player.updateState(state);
player.updatePosition(input.position.x, input.position.y, input.position.z);
player.updateRotation(input.rotation.x, input.rotation.y, input.rotation.z);
player.updateState(input.state);
} else {
players.set(
id,
new PlayerData(id, x, y, z, rotationX, rotationY, rotationZ, state),
);
players.set(input.id, new PlayerData(input));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib/topology/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { type ICanvas } from "./objects/canvas";
import { OBJECT_ID, PRESENCE_GROUP, Player } from ".";
import { OBJECT_ID, PRESENCE_GROUP } from ".";
import { Player } from "../store/playersData";
import { addOrUpdatePlayer } from "../store/playersManager";

export function handlePresenceMessages(e: any) {
if (e.detail.msg.topic !== PRESENCE_GROUP) return;
const input = uint8ArrayToString(e.detail.msg.data);
const player: Player = JSON.parse(input);

// TODO process what to do with other players information
addOrUpdatePlayer(player);
}

// TODO: this should be superseded by wasm and main ts-topology library
Expand Down
15 changes: 3 additions & 12 deletions src/lib/topology/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ import { Vector3, Euler } from "three";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { Canvas, type ICanvas } from "./objects/canvas";
import { handleCanvasMessages, handlePresenceMessages } from "./handlers";

export type PlayerID = string;

// TODO import it from src/lib/store/playersData.ts
export interface Player {
id: PlayerID;
position: Vector3;
rotation: Euler;
state: "running" | "walking" | "jumping";
}
import { Player } from "../store/playersData";

// exception just for the EthCC demo, the format is not this at all
export const OBJECT_ID = "topology::counter_splash";
export const PRESENCE_GROUP = "/counter-splash/presence/0.0.1";
export const PRESENCE_GROUP = "counter-splash::presence";
export const WIDTH = 4000;
export const HEIGHT = 3000;

Expand Down Expand Up @@ -65,7 +56,7 @@ export function addSpray(

export function sendPresence(node: TopologyNode, player: Player) {
node.sendGroupMessage(
"/counter-splash/presence/0.0.1",
PRESENCE_GROUP,
uint8ArrayFromString(JSON.stringify(player)),
);
}

0 comments on commit c4dedd0

Please sign in to comment.