Skip to content

Commit b736979

Browse files
committed
refactor: add types for position-related metadata
1 parent 781d648 commit b736979

File tree

7 files changed

+37
-14
lines changed

7 files changed

+37
-14
lines changed

src/engine/world/actor/metadata.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ConstructedRegion } from '../map';
2+
13
/**
24
* The definition of the metadata available on an {@link Actor}.
35
*
@@ -7,4 +9,10 @@
79
* @author jameskmonger
810
*/
911
export type ActorMetadata = {
12+
/**
13+
* The custom constructed map region for this actor.
14+
*
15+
* TODO Should this live on Actor rather than on {@link Player}? I don't think NPCs can have a custom map.
16+
*/
17+
customMap: ConstructedRegion;
1018
};

src/engine/world/actor/player/metadata.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Chunk } from '@engine/world/map';
2+
import { Position } from '@engine/world/position';
3+
14
/**
25
* The definition of the metadata directly available on a {@link Player}.
36
*
@@ -9,4 +12,16 @@
912
* @author jameskmonger
1013
*/
1114
export type PlayerMetadata = {
15+
/**
16+
* The player's current and previous chunks.
17+
*/
18+
updateChunk: {
19+
oldChunk: Chunk;
20+
newChunk: Chunk;
21+
};
22+
23+
/**
24+
* The player's last position before teleporting.
25+
*/
26+
lastPosition: Position;
1227
};

src/engine/world/actor/player/player.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,16 @@ export class Player extends Actor {
429429

430430
this.outgoingPackets.flushQueue();
431431

432-
if(this.metadata['updateChunk']) {
433-
const { newChunk, oldChunk } = this.metadata['updateChunk'];
432+
if(this.metadata.updateChunk) {
433+
const { newChunk, oldChunk } = this.metadata.updateChunk;
434434
oldChunk.removePlayer(this);
435435
newChunk.addPlayer(this);
436436
this.chunkChanged(newChunk);
437-
this.metadata['updateChunk'] = null;
437+
this.metadata.updateChunk = null;
438438
}
439439

440-
if(this.metadata['teleporting']) {
441-
this.metadata['teleporting'] = null;
440+
if(this.metadata.teleporting) {
441+
this.metadata.teleporting = null;
442442
}
443443

444444
resolve();
@@ -677,9 +677,9 @@ export class Player extends Actor {
677677
public teleport(newPosition: Position, updateRegion: boolean = true): void {
678678
this.walkingQueue.clear();
679679
const originalPosition = this.position.copy();
680-
this.metadata['lastPosition'] = originalPosition;
680+
this.metadata.lastPosition = originalPosition;
681681
this.position = newPosition;
682-
this.metadata['teleporting'] = true;
682+
this.metadata.teleporting = true;
683683

684684
this.updateFlags.mapRegionUpdateRequired = updateRegion;
685685
this.lastMapRegionUpdatePosition = newPosition;
@@ -690,7 +690,7 @@ export class Player extends Actor {
690690
if(!oldChunk.equals(newChunk)) {
691691
oldChunk.removePlayer(this);
692692
newChunk.addPlayer(this);
693-
this.metadata['updateChunk'] = { newChunk, oldChunk };
693+
this.metadata.updateChunk = { newChunk, oldChunk };
694694

695695
if(updateRegion) {
696696
this.actionPipeline.call('region_change', regionChangeActionFactory(

src/engine/world/actor/player/sync/actor-sync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function syncTrackedActors(packet: Packet, playerPosition: Position, appe
9898

9999
if(exists && nearbyActors.findIndex(m => m.actor.equals(trackedActor)) !== -1
100100
&& trackedActor.position.withinViewDistance(playerPosition)
101-
&& !trackedActor.metadata['teleporting']) {
101+
&& !trackedActor.metadata.teleporting) {
102102
appendMovement(trackedActor, packet);
103103
appendUpdateMaskData(trackedActor);
104104
existingTrackedActors.push(trackedActor);

src/engine/world/actor/player/sync/player-sync-task.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ export class PlayerSyncTask extends SyncTask<void> {
3030

3131
const updateMaskData = new ByteBuffer(5000);
3232

33-
if(updateFlags.mapRegionUpdateRequired || this.player.metadata['teleporting']) {
33+
if(updateFlags.mapRegionUpdateRequired || this.player.metadata.teleporting) {
3434
playerUpdatePacket.putBits(1, 1); // Update Required
3535
playerUpdatePacket.putBits(2, 3); // Map Region changed (movement type - 0=nomove, 1=walk, 2=run, 3=mapchange
36-
playerUpdatePacket.putBits(1, this.player.metadata['teleporting'] ? 1 : 0); // Whether or not the client should discard the current walking queue (1 if teleporting, 0 if not)
36+
playerUpdatePacket.putBits(1, this.player.metadata.teleporting ? 1 : 0); // Whether or not the client should discard the current walking queue (1 if teleporting, 0 if not)
3737
playerUpdatePacket.putBits(2, this.player.position.level); // Player Height
3838
playerUpdatePacket.putBits(1, updateFlags.updateBlockRequired ? 1 : 0); // Whether or not an update flag block follows
3939
playerUpdatePacket.putBits(7, this.player.position.chunkLocalX); // Player Local Chunk X

src/engine/world/actor/walking-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class WalkingQueue {
213213

214214
if(!oldChunk.equals(newChunk)) {
215215
if(this.actor instanceof Player) {
216-
this.actor.metadata['updateChunk'] = { newChunk, oldChunk };
216+
this.actor.metadata.updateChunk = { newChunk, oldChunk };
217217

218218
this.actor.actionPipeline.call('region_change', regionChangeActionFactory(
219219
this.actor, originalPosition, this.actor.position));

src/plugins/commands/travel-back-command.plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Skill } from '@engine/world/actor/skills';
44
const action: commandActionHandler = (details) => {
55
const { player } = details;
66

7-
if (player.metadata['lastPosition']) {
8-
player.teleport(player.metadata['lastPosition']);
7+
if (player.metadata.lastPosition) {
8+
player.teleport(player.metadata.lastPosition);
99
}
1010
};
1111

0 commit comments

Comments
 (0)