Change group radius management
This commit is contained in:
parent
242411889a
commit
4a5ef4ab86
2 changed files with 133 additions and 36 deletions
|
@ -59,6 +59,39 @@ export class Group implements Movable {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of users of the group, ignoring any "followers".
|
||||
* Useful to compute the position of the group if a follower is "trapped" far away from the the leader.
|
||||
*/
|
||||
getGroupHeads(): User[] {
|
||||
return Array.from(this.users).filter((user) => user.group?.leader === user || !user.following);
|
||||
}
|
||||
|
||||
/**
|
||||
* Preview the position of the group but don't update it
|
||||
*/
|
||||
previewGroupPosition(): { x: number; y: number } | undefined {
|
||||
const users = this.getGroupHeads();
|
||||
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
|
||||
if (users.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
users.forEach((user: User) => {
|
||||
const position = user.getPosition();
|
||||
x += position.x;
|
||||
y += position.y;
|
||||
});
|
||||
|
||||
x /= users.length;
|
||||
y /= users.length;
|
||||
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the barycenter of all users (i.e. the center of the group)
|
||||
*/
|
||||
|
@ -66,19 +99,15 @@ export class Group implements Movable {
|
|||
const oldX = this.x;
|
||||
const oldY = this.y;
|
||||
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
// Let's compute the barycenter of all users.
|
||||
this.users.forEach((user: User) => {
|
||||
const position = user.getPosition();
|
||||
x += position.x;
|
||||
y += position.y;
|
||||
});
|
||||
x /= this.users.size;
|
||||
y /= this.users.size;
|
||||
if (this.users.size === 0) {
|
||||
throw new Error("EMPTY GROUP FOUND!!!");
|
||||
const newPosition = this.previewGroupPosition();
|
||||
|
||||
if (!newPosition) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { x, y } = newPosition;
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
|
@ -97,10 +126,12 @@ export class Group implements Movable {
|
|||
if (!this.currentZone) return;
|
||||
|
||||
for (const user of this.positionNotifier.getAllUsersInSquareAroundZone(this.currentZone)) {
|
||||
// Todo: Merge two groups with a leader
|
||||
if (user.group || this.isFull()) return; //we ignore users that are already in a group.
|
||||
const distance = GameRoom.computeDistanceBetweenPositions(user.getPosition(), this.getPosition());
|
||||
if (distance < this.groupRadius) {
|
||||
this.join(user);
|
||||
this.setOutOfBounds(false);
|
||||
this.updatePosition();
|
||||
}
|
||||
}
|
||||
|
@ -176,4 +207,8 @@ export class Group implements Movable {
|
|||
this.outOfBounds = true;
|
||||
}
|
||||
}
|
||||
|
||||
get getOutOfBounds() {
|
||||
return this.outOfBounds;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue