update distance values on remotePlayer movement

This commit is contained in:
Hanusiak Piotr 2022-01-26 15:38:08 +01:00
parent dbd06bda4e
commit b88ebbdf08

View File

@ -195,6 +195,7 @@ export class GameScene extends DirtyScene {
private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>(); private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>();
// The item that can be selected by pressing the space key. // The item that can be selected by pressing the space key.
private selectedActivatableObject?: ActivatableInterface; private selectedActivatableObject?: ActivatableInterface;
private activatableObjectsDistances: Map<ActivatableInterface, number> = new Map<ActivatableInterface, number>();
private outlinedItem: ActionableItem | null = null; private outlinedItem: ActionableItem | null = null;
public userInputManager!: UserInputManager; public userInputManager!: UserInputManager;
private isReconnecting: boolean | undefined = undefined; private isReconnecting: boolean | undefined = undefined;
@ -1686,7 +1687,12 @@ ${escapedMessage}
//listen event to share position of user //listen event to share position of user
this.pushPlayerPosition(event); this.pushPlayerPosition(event);
this.gameMap.setPosition(event.x, event.y); this.gameMap.setPosition(event.x, event.y);
this.updateActivatableObjectsDistances();
this.deduceSelectedActivatableObject();
// this.outlineItem(event);
}
private deduceSelectedActivatableObject(): void {
const newNearestObject = this.findNearestActivatableObject(); const newNearestObject = this.findNearestActivatableObject();
if (this.selectedActivatableObject === newNearestObject) { if (this.selectedActivatableObject === newNearestObject) {
return; return;
@ -1694,16 +1700,28 @@ ${escapedMessage}
this.outlineManager.tryRemoveOutline(this.selectedActivatableObject); this.outlineManager.tryRemoveOutline(this.selectedActivatableObject);
this.selectedActivatableObject = newNearestObject; this.selectedActivatableObject = newNearestObject;
this.outlineManager.tryAddOutline(this.selectedActivatableObject); this.outlineManager.tryAddOutline(this.selectedActivatableObject);
// this.outlineItem(event); }
private updateDistanceForSingleActivatableObject(object: ActivatableInterface): void {
this.activatableObjectsDistances.set(
object,
MathUtils.distanceBetween(this.CurrentPlayer.getPosition(), object.getPosition()),
);
}
private updateActivatableObjectsDistances(): void {
const currentPlayerPos = this.CurrentPlayer.getPosition();
for (const object of [...Array.from(this.MapPlayersByKey.values()), ...this.actionableItems.values()]) {
const distance = MathUtils.distanceBetween(currentPlayerPos, object.getPosition());
this.activatableObjectsDistances.set(object, distance);
}
} }
private findNearestActivatableObject(): ActivatableInterface | undefined { private findNearestActivatableObject(): ActivatableInterface | undefined {
let shortestDistance: number = Infinity; let shortestDistance: number = Infinity;
let closestObject: ActivatableInterface | undefined = undefined; let closestObject: ActivatableInterface | undefined = undefined;
const currentPlayerPos = this.CurrentPlayer.getPosition();
for (const object of [...Array.from(this.MapPlayersByKey.values()), ...this.actionableItems.values()]) { for (const [object, distance] of this.activatableObjectsDistances.entries()) {
const distance = MathUtils.distanceBetween(currentPlayerPos, object.getPosition());
if (object.activationRadius > distance && shortestDistance > distance) { if (object.activationRadius > distance && shortestDistance > distance) {
shortestDistance = distance; shortestDistance = distance;
closestObject = object; closestObject = object;
@ -1880,6 +1898,11 @@ ${escapedMessage}
break; break;
case "UserMovedEvent": case "UserMovedEvent":
this.doUpdatePlayerPosition(event.event); this.doUpdatePlayerPosition(event.event);
const remotePlayer = this.MapPlayersByKey.get(event.event.userId);
if (remotePlayer) {
this.updateDistanceForSingleActivatableObject(remotePlayer);
this.deduceSelectedActivatableObject();
}
break; break;
case "GroupCreatedUpdatedEvent": case "GroupCreatedUpdatedEvent":
this.doShareGroupPosition(event.event); this.doShareGroupPosition(event.event);