From 05bedf0c22ee0ce0f3c512acb3a2815cd6bfcf93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 24 Dec 2021 13:20:49 +0100 Subject: [PATCH] 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. --- back/src/Model/GameRoom.ts | 21 ++++++++++++++++----- back/src/Model/Group.ts | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/back/src/Model/GameRoom.ts b/back/src/Model/GameRoom.ts index 4e7cf441..ff8b7cf0 100644 --- a/back/src/Model/GameRoom.ts +++ b/back/src/Model/GameRoom.ts @@ -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); + } } } diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index c59334ec..c35ac7a8 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -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; + } + } }