FIX: if a conversion group move to another user, this user will be added to the group

This commit is contained in:
Kharhamel 2021-10-07 11:04:40 +02:00
parent 900c53f499
commit 5255847452
5 changed files with 117 additions and 25 deletions

View file

@ -12,7 +12,7 @@ import { EmoteCallback, EntersCallback, LeavesCallback, MovesCallback, Zone } fr
import { Movable } from "_Model/Movable";
import { PositionInterface } from "_Model/PositionInterface";
import { ZoneSocket } from "../RoomManager";
import { User } from "_Model/User";
import { User } from "../Model/User";
import { EmoteEventMessage } from "../Messages/generated/messages_pb";
interface ZoneDescriptor {
@ -20,6 +20,17 @@ interface ZoneDescriptor {
j: number;
}
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 };
}
}
}
export class PositionNotifier {
// TODO: we need a way to clean the zones if no one is in the zone and no one listening (to free memory!)
@ -41,14 +52,15 @@ export class PositionNotifier {
};
}
public enter(thing: Movable): void {
public enter(thing: Movable): Zone {
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);
return zone;
}
public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): void {
public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): Zone {
// Did we change zone?
const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y);
const newZoneDesc = this.getZoneDescriptorFromCoordinates(newPosition.x, newPosition.y);
@ -62,9 +74,11 @@ export class PositionNotifier {
// Enter new zone
newZone.enter(thing, oldZone, newPosition);
return newZone;
} else {
const zone = this.getZone(oldZoneDesc.i, oldZoneDesc.j);
zone.move(thing, newPosition);
return zone;
}
}
@ -106,4 +120,16 @@ export class PositionNotifier {
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
zone.emitEmoteEvent(emoteEventMessage);
}
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;
}
}
}
}
}