2020-09-15 10:06:11 +02:00
|
|
|
/**
|
|
|
|
* Tracks the position of every player on the map, and sends notifications to the players interested in knowing about the move
|
|
|
|
* (i.e. players that are looking at the zone the player is currently in)
|
|
|
|
*
|
|
|
|
* Internally, the PositionNotifier works with Zones. A zone is a square area of a map.
|
|
|
|
* Each player is in a given zone, and each player tracks one or many zones (depending on the player viewport)
|
|
|
|
*
|
|
|
|
* The PositionNotifier is important for performance. It allows us to send the position of players only to a restricted
|
|
|
|
* number of players around the current player.
|
|
|
|
*/
|
2021-12-22 18:30:23 +01:00
|
|
|
import {
|
|
|
|
EmoteCallback,
|
|
|
|
EntersCallback,
|
|
|
|
LeavesCallback,
|
2022-03-02 10:44:26 +01:00
|
|
|
LockGroupCallback,
|
2021-12-22 18:30:23 +01:00
|
|
|
MovesCallback,
|
|
|
|
PlayerDetailsUpdatedCallback,
|
|
|
|
Zone,
|
|
|
|
} from "./Zone";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { Movable } from "_Model/Movable";
|
|
|
|
import { PositionInterface } from "_Model/PositionInterface";
|
|
|
|
import { ZoneSocket } from "../RoomManager";
|
2021-10-07 11:04:40 +02:00
|
|
|
import { User } from "../Model/User";
|
2022-03-10 12:20:53 +01:00
|
|
|
import { EmoteEventMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb";
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
interface ZoneDescriptor {
|
|
|
|
i: number;
|
|
|
|
j: number;
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:04:40 +02:00
|
|
|
export function* getNearbyDescriptorsMatrix(middleZoneDescriptor: ZoneDescriptor): Generator<ZoneDescriptor> {
|
|
|
|
for (let n = 0; n < 9; n++) {
|
|
|
|
const i = middleZoneDescriptor.i + ((n % 3) - 1);
|
|
|
|
const j = middleZoneDescriptor.j + (Math.floor(n / 3) - 1);
|
|
|
|
|
|
|
|
if (i >= 0 && j >= 0) {
|
|
|
|
yield { i, j };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
export class PositionNotifier {
|
2021-07-27 14:29:09 +02:00
|
|
|
// TODO: we need a way to clean the zones if no one is in the zone and no one listening (to free memory!)
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
private zones: Zone[][] = [];
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
constructor(
|
|
|
|
private zoneWidth: number,
|
|
|
|
private zoneHeight: number,
|
|
|
|
private onUserEnters: EntersCallback,
|
|
|
|
private onUserMoves: MovesCallback,
|
|
|
|
private onUserLeaves: LeavesCallback,
|
2021-12-22 18:30:23 +01:00
|
|
|
private onEmote: EmoteCallback,
|
2022-03-02 10:44:26 +01:00
|
|
|
private onLockGroup: LockGroupCallback,
|
2021-12-22 18:30:23 +01:00
|
|
|
private onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
|
2021-06-24 10:09:10 +02:00
|
|
|
) {}
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor {
|
|
|
|
return {
|
|
|
|
i: Math.floor(x / this.zoneWidth),
|
|
|
|
j: Math.floor(y / this.zoneHeight),
|
2021-06-24 10:09:10 +02:00
|
|
|
};
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:04:40 +02:00
|
|
|
public enter(thing: Movable): Zone {
|
2020-09-25 15:25:06 +02:00
|
|
|
const position = thing.getPosition();
|
|
|
|
const zoneDesc = this.getZoneDescriptorFromCoordinates(position.x, position.y);
|
|
|
|
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
|
|
|
|
zone.enter(thing, null, position);
|
2021-10-07 11:04:40 +02:00
|
|
|
return zone;
|
2020-09-25 15:25:06 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:04:40 +02:00
|
|
|
public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): Zone {
|
2020-09-15 10:06:11 +02:00
|
|
|
// Did we change zone?
|
2020-09-16 16:06:43 +02:00
|
|
|
const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y);
|
|
|
|
const newZoneDesc = this.getZoneDescriptorFromCoordinates(newPosition.x, newPosition.y);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
if (oldZoneDesc.i != newZoneDesc.i || oldZoneDesc.j != newZoneDesc.j) {
|
|
|
|
const oldZone = this.getZone(oldZoneDesc.i, oldZoneDesc.j);
|
|
|
|
const newZone = this.getZone(newZoneDesc.i, newZoneDesc.j);
|
|
|
|
|
|
|
|
// Leave old zone
|
2020-09-16 16:06:43 +02:00
|
|
|
oldZone.leave(thing, newZone);
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
// Enter new zone
|
2020-09-16 16:06:43 +02:00
|
|
|
newZone.enter(thing, oldZone, newPosition);
|
2021-10-07 11:04:40 +02:00
|
|
|
return newZone;
|
2020-09-15 10:06:11 +02:00
|
|
|
} else {
|
|
|
|
const zone = this.getZone(oldZoneDesc.i, oldZoneDesc.j);
|
2020-09-16 16:06:43 +02:00
|
|
|
zone.move(thing, newPosition);
|
2021-10-07 11:04:40 +02:00
|
|
|
return zone;
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
public leave(thing: Movable): void {
|
|
|
|
const oldPosition = thing.getPosition();
|
|
|
|
const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y);
|
2020-09-15 10:06:11 +02:00
|
|
|
const oldZone = this.getZone(oldZoneDesc.i, oldZoneDesc.j);
|
2020-09-16 16:06:43 +02:00
|
|
|
oldZone.leave(thing, null);
|
|
|
|
}
|
2020-09-15 16:21:41 +02:00
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
private getZone(i: number, j: number): Zone {
|
|
|
|
let zoneRow = this.zones[j];
|
|
|
|
if (zoneRow === undefined) {
|
|
|
|
zoneRow = new Array<Zone>();
|
|
|
|
this.zones[j] = zoneRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
let zone = this.zones[j][i];
|
|
|
|
if (zone === undefined) {
|
2021-12-22 18:30:23 +01:00
|
|
|
zone = new Zone(
|
|
|
|
this.onUserEnters,
|
|
|
|
this.onUserMoves,
|
|
|
|
this.onUserLeaves,
|
|
|
|
this.onEmote,
|
2022-03-02 10:44:26 +01:00
|
|
|
this.onLockGroup,
|
2021-12-22 18:30:23 +01:00
|
|
|
this.onPlayerDetailsUpdated,
|
|
|
|
i,
|
|
|
|
j
|
|
|
|
);
|
2020-09-15 10:06:11 +02:00
|
|
|
this.zones[j][i] = zone;
|
|
|
|
}
|
|
|
|
return zone;
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
public addZoneListener(call: ZoneSocket, x: number, y: number): Set<Movable> {
|
|
|
|
const zone = this.getZone(x, y);
|
|
|
|
zone.addListener(call);
|
|
|
|
return zone.getThings();
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeZoneListener(call: ZoneSocket, x: number, y: number): void {
|
|
|
|
const zone = this.getZone(x, y);
|
|
|
|
zone.removeListener(call);
|
|
|
|
}
|
2021-03-31 11:21:06 +02:00
|
|
|
|
|
|
|
public emitEmoteEvent(user: User, emoteEventMessage: EmoteEventMessage) {
|
|
|
|
const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y);
|
|
|
|
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
|
|
|
|
zone.emitEmoteEvent(emoteEventMessage);
|
|
|
|
}
|
2021-10-07 11:04:40 +02:00
|
|
|
|
2022-03-10 12:20:53 +01:00
|
|
|
public emitLockGroupEvent(user: User, groupId: number) {
|
2022-03-02 10:44:26 +01:00
|
|
|
const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y);
|
|
|
|
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
|
2022-03-10 12:20:53 +01:00
|
|
|
zone.emitLockGroupEvent(groupId);
|
2022-03-02 10:44:26 +01:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:04:40 +02:00
|
|
|
public *getAllUsersInSquareAroundZone(zone: Zone): Generator<User> {
|
|
|
|
const zoneDescriptor = this.getZoneDescriptorFromCoordinates(zone.x, zone.y);
|
|
|
|
for (const d of getNearbyDescriptorsMatrix(zoneDescriptor)) {
|
|
|
|
const zone = this.getZone(d.i, d.j);
|
|
|
|
for (const thing of zone.getThings()) {
|
|
|
|
if (thing instanceof User) {
|
|
|
|
yield thing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 18:30:23 +01:00
|
|
|
|
|
|
|
public updatePlayerDetails(user: User, playerDetails: SetPlayerDetailsMessage) {
|
|
|
|
const position = user.getPosition();
|
|
|
|
const zoneDesc = this.getZoneDescriptorFromCoordinates(position.x, position.y);
|
|
|
|
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
|
|
|
|
zone.updatePlayerDetails(user, playerDetails);
|
|
|
|
}
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|