partey_workadventure/back/src/Model/Group.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-04-07 10:08:04 +02:00
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
2020-04-08 20:40:44 +02:00
import { World } from "./World";
2020-04-07 10:08:04 +02:00
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
constructor(users: MessageUserPosition[]) {
this.users = users;
}
getUsers(): MessageUserPosition[] {
return this.users;
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
2020-04-08 20:40:44 +02:00
isPartOfGroup(user: MessageUserPosition): boolean
{
return this.users.indexOf(user) !== -1;
}
isStillIn(user: MessageUserPosition): boolean
{
if(!this.isPartOfGroup(user)) {
return false;
}
let stillIn = true;
for(let i = 0; i <= this.users.length; i++) {
let userInGroup = this.users[i];
let distance = World.computeDistance(user.position, userInGroup.position);
if(distance > World.MIN_DISTANCE) {
stillIn = false;
break;
}
}
return stillIn;
}
removeFromGroup(users: MessageUserPosition[]): void
{
for(let i = 0; i < users.length; i++) {
let user = users[i];
const index = this.users.indexOf(user, 0);
if (index > -1) {
this.users.splice(index, 1);
}
}
}
2020-04-07 10:08:04 +02:00
}