2021-06-24 10:09:10 +02:00
|
|
|
import { User } from "./User";
|
|
|
|
import { PositionInterface } from "_Model/PositionInterface";
|
|
|
|
import { Movable } from "./Movable";
|
|
|
|
import { Group } from "./Group";
|
|
|
|
import { ZoneSocket } from "../RoomManager";
|
|
|
|
import { EmoteEventMessage } from "../Messages/generated/messages_pb";
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
export type EntersCallback = (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) => void;
|
2020-11-13 18:00:22 +01:00
|
|
|
export type MovesCallback = (thing: Movable, position: PositionInterface, listener: ZoneSocket) => void;
|
2021-06-24 10:09:10 +02:00
|
|
|
export type LeavesCallback = (thing: Movable, newZone: Zone | null, listener: ZoneSocket) => void;
|
2021-03-31 11:21:06 +02:00
|
|
|
export type EmoteCallback = (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => void;
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
export class Zone {
|
2020-09-16 16:06:43 +02:00
|
|
|
private things: Set<Movable> = new Set<Movable>();
|
2020-11-13 18:00:22 +01:00
|
|
|
private listeners: Set<ZoneSocket> = new Set<ZoneSocket>();
|
2021-06-24 10:09:10 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private onEnters: EntersCallback,
|
|
|
|
private onMoves: MovesCallback,
|
|
|
|
private onLeaves: LeavesCallback,
|
|
|
|
private onEmote: EmoteCallback,
|
|
|
|
public readonly x: number,
|
|
|
|
public readonly y: number
|
|
|
|
) {}
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-16 16:06:43 +02:00
|
|
|
* A user/thing leaves the zone
|
2020-09-15 10:06:11 +02:00
|
|
|
*/
|
2021-06-24 10:09:10 +02:00
|
|
|
public leave(thing: Movable, newZone: Zone | null) {
|
2020-09-25 13:48:02 +02:00
|
|
|
const result = this.things.delete(thing);
|
|
|
|
if (!result) {
|
|
|
|
if (thing instanceof User) {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("Could not find user in zone " + thing.id);
|
2020-09-25 13:48:02 +02:00
|
|
|
}
|
|
|
|
if (thing instanceof Group) {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error(
|
|
|
|
"Could not find group " +
|
|
|
|
thing.getId() +
|
|
|
|
" in zone (" +
|
|
|
|
this.x +
|
|
|
|
"," +
|
|
|
|
this.y +
|
|
|
|
"). Position of group: (" +
|
|
|
|
thing.getPosition().x +
|
|
|
|
"," +
|
|
|
|
thing.getPosition().y +
|
|
|
|
")"
|
|
|
|
);
|
2020-09-25 13:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 16:06:43 +02:00
|
|
|
this.notifyLeft(thing, newZone);
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-16 16:06:43 +02:00
|
|
|
* Notify listeners of this zone that this user/thing left
|
2020-09-15 10:06:11 +02:00
|
|
|
*/
|
2021-06-24 10:09:10 +02:00
|
|
|
private notifyLeft(thing: Movable, newZone: Zone | null) {
|
2020-09-15 10:06:11 +02:00
|
|
|
for (const listener of this.listeners) {
|
2021-03-31 11:21:06 +02:00
|
|
|
this.onLeaves(thing, newZone, listener);
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
public enter(thing: Movable, oldZone: Zone | null, position: PositionInterface) {
|
2020-09-16 16:06:43 +02:00
|
|
|
this.things.add(thing);
|
2020-09-25 13:48:02 +02:00
|
|
|
this.notifyEnter(thing, oldZone, position);
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify listeners of this zone that this user entered
|
|
|
|
*/
|
2021-06-24 10:09:10 +02:00
|
|
|
private notifyEnter(thing: Movable, oldZone: Zone | null, position: PositionInterface) {
|
2020-09-15 10:06:11 +02:00
|
|
|
for (const listener of this.listeners) {
|
2020-11-13 18:00:22 +01:00
|
|
|
this.onEnters(thing, oldZone, listener);
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
public move(thing: Movable, position: PositionInterface) {
|
|
|
|
if (!this.things.has(thing)) {
|
|
|
|
this.things.add(thing);
|
2020-09-25 13:48:02 +02:00
|
|
|
this.notifyEnter(thing, null, position);
|
2020-09-15 16:21:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
for (const listener of this.listeners) {
|
2020-11-13 18:00:22 +01:00
|
|
|
//if (listener !== thing) {
|
2021-06-24 10:09:10 +02:00
|
|
|
this.onMoves(thing, position, listener);
|
2020-11-13 18:00:22 +01:00
|
|
|
//}
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
public getThings(): Set<Movable> {
|
|
|
|
return this.things;
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
public addListener(socket: ZoneSocket): void {
|
|
|
|
this.listeners.add(socket);
|
|
|
|
// TODO: here, we should trigger in some way the sending of current items
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeListener(socket: ZoneSocket): void {
|
|
|
|
this.listeners.delete(socket);
|
|
|
|
}
|
2021-03-31 11:21:06 +02:00
|
|
|
|
|
|
|
public emitEmoteEvent(emoteEventMessage: EmoteEventMessage) {
|
|
|
|
for (const listener of this.listeners) {
|
|
|
|
this.onEmote(emoteEventMessage, listener);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|