Making the group radius distinct from the minimum distance to connect 2 players

Also, changed default settings from 160px for Group Radius to 120px
(minimum distance to connect 2 players remains 160px)
This commit is contained in:
David Négrier 2020-05-03 16:56:19 +02:00
parent dcaf9a8e46
commit 3b27f8b000
5 changed files with 28 additions and 35 deletions

View file

@ -7,7 +7,8 @@ import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface";
export class World {
static readonly MIN_DISTANCE = 160;
private minDistance: number;
private groupRadius: number;
// Users, sorted by ID
private users: Map<string, UserInterface>;
@ -16,12 +17,16 @@ export class World {
private connectCallback: (user1: string, user2: string) => void;
private disconnectCallback: (user1: string, user2: string) => void;
constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void)
constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void,
minDistance: number,
groupRadius: number)
{
this.users = new Map<string, UserInterface>();
this.groups = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
this.minDistance = minDistance;
this.groupRadius = groupRadius;
}
public join(userPosition: MessageUserPosition): void {
@ -73,7 +78,7 @@ export class World {
// If the user is part of a group:
// should he leave the group?
let distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition());
if (distance > World.MIN_DISTANCE) {
if (distance > this.groupRadius) {
this.leaveGroup(user);
}
}
@ -103,15 +108,16 @@ export class World {
/**
* Looks for the closest user that is:
* - close enough (distance <= MIN_DISTANCE)
* - not in a group OR in a group that is not full
* - close enough (distance <= minDistance)
* - not in a group
* OR
* - close enough to a group (distance <= groupRadius)
*/
private searchClosestAvailableUserOrGroup(user: UserInterface): UserInterface|Group|null
{
let usersToBeGroupedWith: Distance[] = [];
let minimumDistanceFound: number = World.MIN_DISTANCE;
let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius);
let matchingItem: UserInterface | Group | null = null;
this.users.forEach(function(currentUser, userId) {
this.users.forEach((currentUser, userId) => {
// Let's only check users that are not part of a group
if (typeof currentUser.group !== 'undefined') {
return;
@ -122,7 +128,7 @@ export class World {
let distance = World.computeDistance(user, currentUser); // compute distance between peers.
if(distance <= minimumDistanceFound) {
if(distance <= minimumDistanceFound && distance <= this.minDistance) {
minimumDistanceFound = distance;
matchingItem = currentUser;
}
@ -162,12 +168,12 @@ export class World {
*/
});
this.groups.forEach(function(group: Group) {
this.groups.forEach((group: Group) => {
if (group.isFull()) {
return;
}
let distance = World.computeDistanceBetweenPositions(user.position, group.getPosition());
if(distance <= minimumDistanceFound) {
if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
minimumDistanceFound = distance;
matchingItem = group;
}