Moved GroupUpdateMessage to protobuf
This commit is contained in:
parent
57545a96a5
commit
76d3779438
5 changed files with 76 additions and 33 deletions
|
@ -13,6 +13,8 @@ export class Group implements Movable {
|
|||
private users: Set<User>;
|
||||
private connectCallback: ConnectCallback;
|
||||
private disconnectCallback: DisconnectCallback;
|
||||
private x!: number;
|
||||
private y!: number;
|
||||
|
||||
|
||||
constructor(users: User[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) {
|
||||
|
@ -25,6 +27,8 @@ export class Group implements Movable {
|
|||
users.forEach((user: User) => {
|
||||
this.join(user);
|
||||
});
|
||||
|
||||
this.updatePosition();
|
||||
}
|
||||
|
||||
getUsers(): User[] {
|
||||
|
@ -39,6 +43,16 @@ export class Group implements Movable {
|
|||
* Returns the barycenter of all users (i.e. the center of the group)
|
||||
*/
|
||||
getPosition(): PositionInterface {
|
||||
return {
|
||||
x: this.x,
|
||||
y: this.y
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the barycenter of all users (i.e. the center of the group)
|
||||
*/
|
||||
updatePosition(): void {
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
// Let's compute the barycenter of all users.
|
||||
|
@ -48,10 +62,11 @@ export class Group implements Movable {
|
|||
});
|
||||
x /= this.users.size;
|
||||
y /= this.users.size;
|
||||
return {
|
||||
x,
|
||||
y
|
||||
};
|
||||
if (this.users.size === 0) {
|
||||
throw new Error("EMPTY GROUP FOUND!!!");
|
||||
}
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
isFull(): boolean {
|
||||
|
|
|
@ -90,6 +90,7 @@ export class World {
|
|||
this.positionNotifier.updatePosition(user, userPosition, user.position);
|
||||
|
||||
const oldGroupPosition = user.group?.getPosition();
|
||||
user.group?.updatePosition();
|
||||
|
||||
user.position = userPosition;
|
||||
|
||||
|
@ -126,7 +127,7 @@ export class World {
|
|||
}
|
||||
|
||||
// At the very end, if the user is part of a group, let's call the callback to update group position
|
||||
if (typeof user.group !== 'undefined') {
|
||||
if (user.group !== undefined) {
|
||||
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +159,7 @@ export class World {
|
|||
*/
|
||||
private leaveGroup(user: User): void {
|
||||
const group = user.group;
|
||||
if (typeof group === 'undefined') {
|
||||
if (group === undefined) {
|
||||
throw new Error("The user is part of no group");
|
||||
}
|
||||
group.leave(user);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue