Migrating position notification into the User class
This commit is contained in:
parent
892d1555b8
commit
23cea1c835
7 changed files with 59 additions and 52 deletions
|
@ -57,8 +57,9 @@ export class Group implements Movable {
|
|||
let y = 0;
|
||||
// Let's compute the barycenter of all users.
|
||||
this.users.forEach((user: User) => {
|
||||
x += user.position.x;
|
||||
y += user.position.y;
|
||||
const position = user.getPosition();
|
||||
x += position.x;
|
||||
y += position.y;
|
||||
});
|
||||
x /= this.users.size;
|
||||
y /= this.users.size;
|
||||
|
@ -69,8 +70,7 @@ export class Group implements Movable {
|
|||
this.y = y;
|
||||
|
||||
if (oldX === undefined) {
|
||||
// TODO: do we need a "create"
|
||||
this.positionNotifier.updatePosition(this, {x, y}, {x, y});
|
||||
this.positionNotifier.enter(this);
|
||||
} else {
|
||||
this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY});
|
||||
}
|
||||
|
|
|
@ -74,6 +74,13 @@ export class PositionNotifier {
|
|||
return things;
|
||||
}
|
||||
|
||||
public enter(thing: Movable): void {
|
||||
const position = thing.getPosition();
|
||||
const zoneDesc = this.getZoneDescriptorFromCoordinates(position.x, position.y);
|
||||
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
|
||||
zone.enter(thing, null, position);
|
||||
}
|
||||
|
||||
public updatePosition(thing: Movable, newPosition: PositionInterface, oldPosition: PositionInterface): void {
|
||||
// Did we change zone?
|
||||
const oldZoneDesc = this.getZoneDescriptorFromCoordinates(oldPosition.x, oldPosition.y);
|
||||
|
|
|
@ -3,6 +3,7 @@ import { PointInterface } from "./Websocket/PointInterface";
|
|||
import {Zone} from "_Model/Zone";
|
||||
import {Movable} from "_Model/Movable";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
import {PositionNotifier} from "_Model/PositionNotifier";
|
||||
|
||||
export class User implements Movable {
|
||||
public listenedZones: Set<Zone>;
|
||||
|
@ -10,14 +11,22 @@ export class User implements Movable {
|
|||
|
||||
public constructor(
|
||||
public id: number,
|
||||
public position: PointInterface,
|
||||
private position: PointInterface,
|
||||
public silent: boolean,
|
||||
|
||||
private positionNotifier: PositionNotifier
|
||||
) {
|
||||
this.listenedZones = new Set<Zone>();
|
||||
|
||||
this.positionNotifier.enter(this);
|
||||
}
|
||||
|
||||
public getPosition(): PositionInterface {
|
||||
public getPosition(): PointInterface {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public setPosition(position: PointInterface): void {
|
||||
const oldPosition = this.position;
|
||||
this.position = position;
|
||||
this.positionNotifier.updatePosition(this, position, oldPosition);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,9 +56,11 @@ export class World {
|
|||
}
|
||||
|
||||
public join(socket : Identificable, userPosition: PointInterface): void {
|
||||
this.users.set(socket.userId, new User(socket.userId, userPosition, false));
|
||||
const user = new User(socket.userId, userPosition, false, this.positionNotifier);
|
||||
this.users.set(socket.userId, user);
|
||||
// Let's call update position to trigger the join / leave room
|
||||
this.updatePosition(socket, userPosition);
|
||||
//this.updatePosition(socket, userPosition);
|
||||
this.updateUserGroup(user);
|
||||
}
|
||||
|
||||
public leave(user : Identificable){
|
||||
|
@ -87,16 +89,13 @@ export class World {
|
|||
return;
|
||||
}
|
||||
|
||||
this.positionNotifier.updatePosition(user, userPosition, user.position);
|
||||
user.setPosition(userPosition);
|
||||
|
||||
const oldGroupPosition = user.group?.getPosition();
|
||||
this.updateUserGroup(user);
|
||||
}
|
||||
|
||||
user.position = userPosition;
|
||||
private updateUserGroup(user: User): void {
|
||||
user.group?.updatePosition();
|
||||
/*if (user.group !== undefined) {
|
||||
// TODO: positionNotifier should be notified by the group itself when it moves!!!
|
||||
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
|
||||
}*/
|
||||
|
||||
if (user.silent) {
|
||||
return;
|
||||
|
@ -124,16 +123,11 @@ export class World {
|
|||
} else {
|
||||
// If the user is part of a group:
|
||||
// should he leave the group?
|
||||
const distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition());
|
||||
const distance = World.computeDistanceBetweenPositions(user.getPosition(), user.group.getPosition());
|
||||
if (distance > this.groupRadius) {
|
||||
this.leaveGroup(user);
|
||||
}
|
||||
}
|
||||
|
||||
// At the very end, if the user is part of a group, let's call the callback to update group position
|
||||
/*if (user.group !== undefined) {
|
||||
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
|
||||
}*/
|
||||
}
|
||||
|
||||
setSilent(socket: Identificable, silent: boolean) {
|
||||
|
@ -152,7 +146,7 @@ export class World {
|
|||
}
|
||||
if (!silent) {
|
||||
// If we are back to life, let's trigger a position update to see if we can join some group.
|
||||
this.updatePosition(socket, user.position);
|
||||
this.updatePosition(socket, user.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -251,7 +245,7 @@ export class World {
|
|||
if (group.isFull()) {
|
||||
return;
|
||||
}
|
||||
const distance = World.computeDistanceBetweenPositions(user.position, group.getPosition());
|
||||
const distance = World.computeDistanceBetweenPositions(user.getPosition(), group.getPosition());
|
||||
if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
|
||||
minimumDistanceFound = distance;
|
||||
matchingItem = group;
|
||||
|
@ -263,7 +257,9 @@ export class World {
|
|||
|
||||
public static computeDistance(user1: User, user2: User): number
|
||||
{
|
||||
return Math.sqrt(Math.pow(user2.position.x - user1.position.x, 2) + Math.pow(user2.position.y - user1.position.y, 2));
|
||||
const user1Position = user1.getPosition();
|
||||
const user2Position = user2.getPosition();
|
||||
return Math.sqrt(Math.pow(user2Position.x - user1Position.x, 2) + Math.pow(user2Position.y - user1Position.y, 2));
|
||||
}
|
||||
|
||||
public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number
|
||||
|
|
|
@ -25,10 +25,10 @@ export class Zone {
|
|||
const result = this.things.delete(thing);
|
||||
if (!result) {
|
||||
if (thing instanceof User) {
|
||||
console.error('Could not find user in zone '+thing.id);
|
||||
throw new Error('Could not find user in zone '+thing.id);
|
||||
}
|
||||
if (thing instanceof Group) {
|
||||
console.error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')');
|
||||
throw new Error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue