Skip to content

Commit 781d648

Browse files
committed
refactor: create strongly type metadata property on Actor and Player
1 parent 2f30be1 commit 781d648

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/engine/world/actor/actor.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Pathfinding } from './pathfinding';
1616
import { Attack, AttackDamageType } from './player/attack';
1717
import { Behavior } from './behaviors';
1818
import { Effect, EffectType } from './effect';
19+
import { ActorMetadata } from './metadata';
1920

2021

2122
export type ActorType = 'player' | 'npc';
@@ -33,7 +34,16 @@ export abstract class Actor {
3334
public readonly inventory: ItemContainer = new ItemContainer(28);
3435
public readonly bank: ItemContainer = new ItemContainer(376);
3536
public readonly actionPipeline = new ActionPipeline(this);
36-
public readonly metadata: { [key: string]: any } = {};
37+
38+
/**
39+
* The map of available metadata for this actor.
40+
*
41+
* You cannot guarantee that this will be populated with data, so you should always check for the existence of the
42+
* metadata you are looking for before using it.
43+
*
44+
* @author jameskmonger
45+
*/
46+
public readonly metadata: Partial<ActorMetadata> = {};
3747

3848
/**
3949
* @deprecated - use new action system instead

src/engine/world/actor/metadata.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* The definition of the metadata available on an {@link Actor}.
3+
*
4+
* You cannot guarantee that all of these properties will be present on an actor,
5+
* so you should always check for their existence before using them.
6+
*
7+
* @author jameskmonger
8+
*/
9+
export type ActorMetadata = {
10+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* The definition of the metadata directly available on a {@link Player}.
3+
*
4+
* This is a subset of the metadata available on an {@link Actor}. See {@link ActorMetadata} for more information.
5+
*
6+
* You cannot guarantee that all of these properties will be present on an actor,
7+
* so you should always check for their existence before using them.
8+
*
9+
* @author jameskmonger
10+
*/
11+
export type PlayerMetadata = {
12+
};

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { dialogue } from '../dialogue';
4545
import { Npc } from '../npc';
4646
import { combatStyles } from '../combat';
4747
import { SkillName } from '../skills';
48+
import { PlayerMetadata } from './metadata';
4849

4950

5051
export const playerOptions: { option: string, index: number, placement: 'TOP' | 'BOTTOM' }[] = [
@@ -117,6 +118,18 @@ export class Player extends Actor {
117118
public cutscene: Cutscene = null;
118119
public playerEvents: EventEmitter = new EventEmitter();
119120

121+
/**
122+
* Override the Actor's `metadata` property to provide a more specific type.
123+
*
124+
* You cannot guarantee that this will be populated with data, so you should always check for the existence of the
125+
* metadata you are looking for before using it.
126+
*
127+
* The ! is used to tell the compiler that we know this property will be defined.
128+
*
129+
* @author jameskmonger
130+
*/
131+
public readonly metadata!: (Actor['metadata'] & Partial<PlayerMetadata>);
132+
120133
private readonly _socket: Socket;
121134
private readonly _inCipher: Isaac;
122135
private readonly _outCipher: Isaac;

0 commit comments

Comments
 (0)