Switching connection to a barycenter approach

This commit is contained in:
David Négrier 2020-04-29 22:41:48 +02:00
parent 5ffc5a420e
commit 2a8e3ea323
3 changed files with 60 additions and 36 deletions

View file

@ -1,6 +1,6 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import { World } from "./World";
import { UserInterface } from "./UserInterface";
import {PositionInterface} from "_Model/PositionInterface";
export class Group {
static readonly MAX_PER_GROUP = 4;
@ -24,6 +24,25 @@ export class Group {
return this.users;
}
/**
* Returns the barycenter of all users (i.e. the center of the group)
*/
getPosition(): PositionInterface {
let x = 0;
let y = 0;
// Let's compute the barycenter of all users.
this.users.forEach((user: UserInterface) => {
x += user.position.x;
y += user.position.y;
});
x /= this.users.length;
y /= this.users.length;
return {
x,
y
};
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}