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; + } + } }