Adding an "out-of-bounds" notion for a group.

This allows hiding a group when some players are out of the radius but the group still exists because of the "follow" feature.
This commit is contained in:
David Négrier 2021-12-24 13:20:49 +01:00
parent 524339a3a0
commit 05bedf0c22
2 changed files with 34 additions and 5 deletions

View file

@ -219,8 +219,8 @@ export class GameRoom {
if (user.silent) {
return;
}
if (user.group === undefined) {
const group = user.group;
if (group === undefined) {
// If the user is not part of a group:
// should he join a group?
@ -251,18 +251,29 @@ export class GameRoom {
} else {
// If the user is part of a group:
// should he leave the group?
const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following);
users.forEach((foreignUser: User) => {
let noOneOutOfBounds = true;
group.getUsers().forEach((foreignUser: User) => {
if (foreignUser.group === undefined) {
return;
}
const usrPos = foreignUser.getPosition();
const grpPos = foreignUser.group.getPosition();
const distance = GameRoom.computeDistanceBetweenPositions(usrPos, grpPos);
if (distance > this.groupRadius) {
this.leaveGroup(foreignUser);
if (foreignUser.hasFollowers() || foreignUser.following) {
// If one user is out of the group bounds BUT following, the group still exists... but should be hidden.
// We put it in 'outOfBounds' mode
group.setOutOfBounds(true);
noOneOutOfBounds = false;
} else {
this.leaveGroup(foreignUser);
}
}
});
if (noOneOutOfBounds && !user.group?.isEmpty()) {
group.setOutOfBounds(false);
}
}
}

View file

@ -16,6 +16,10 @@ export class Group implements Movable {
private wasDestroyed: boolean = false;
private roomId: string;
private currentZone: Zone | null = null;
/**
* When outOfBounds = true, a user if out of the bounds of the group BUT still considered inside it (because we are in following mode)
*/
private outOfBounds = false;
constructor(
roomId: string,
@ -78,6 +82,10 @@ export class Group implements Movable {
this.x = x;
this.y = y;
if (this.outOfBounds) {
return;
}
if (oldX === undefined) {
this.currentZone = this.positionNotifier.enter(this);
} else {
@ -154,4 +162,14 @@ export class Group implements Movable {
}
return undefined;
}
setOutOfBounds(outOfBounds: boolean): void {
if (this.outOfBounds === true && outOfBounds === false) {
this.positionNotifier.enter(this);
this.outOfBounds = false;
} else if (this.outOfBounds === false && outOfBounds === true) {
this.positionNotifier.leave(this);
this.outOfBounds = true;
}
}
}