Skip to content

Commit 4b17597

Browse files
authored
Merge pull request #444 from DrJacobHolden/is-actor-type-guards
Convert isNpc and isPlayer to be type guards
2 parents 353e9cb + 157d1a4 commit 4b17597

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

src/engine/action/hook/task.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export class TaskExecutor<T> {
149149
public getDetails(): TaskDetails<T> {
150150
return {
151151
actor: this.actor,
152-
player: this.actor.isPlayer ? this.actor as Player : undefined,
153-
npc: this.actor.isNpc ? this.actor as Npc : undefined,
152+
player: this.actor.isPlayer() ? this.actor : undefined,
153+
npc: this.actor.isNpc() ? this.actor : undefined,
154154
actionData: this.actionData,
155155
session: this.session
156156
};

src/engine/world/actor/actor.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { Task, TaskScheduler } from '@engine/task';
1515
import { logger } from '@runejs/common';
1616
import { ObjectConfig } from '@runejs/filestore';
1717
import { QueueableTask } from '@engine/action/pipe/task/queueable-task';
18+
import { Npc } from '@engine/world/actor/npc';
19+
import { Player } from '@engine/world/actor/player';
1820

1921

2022
export type ActorType = 'player' | 'npc';
@@ -355,8 +357,8 @@ export abstract class Actor {
355357
return;
356358
}
357359

358-
if(this.isNpc) {
359-
const nearbyPlayers = activeWorld.findNearbyPlayers(this.position, 24, this.instance?.instanceId);
360+
if(this.isNpc()) {
361+
const nearbyPlayers = activeWorld.findNearbyPlayers(this.position, 24, this.instance.instanceId);
360362
if(nearbyPlayers.length === 0) {
361363
// No need for this actor to move if there are no players nearby to witness it, save some memory. :)
362364
return;
@@ -483,6 +485,14 @@ export abstract class Actor {
483485
this.scheduler.tick();
484486
}
485487

488+
public isPlayer(): this is Player {
489+
return this.type === 'player';
490+
}
491+
492+
public isNpc(): this is Npc {
493+
return this.type === 'npc';
494+
}
495+
486496
public get position(): Position {
487497
return this._position;
488498
}
@@ -551,14 +561,6 @@ export abstract class Actor {
551561
this._instance = value;
552562
}
553563

554-
public get isPlayer(): boolean {
555-
return this.type === 'player';
556-
}
557-
558-
public get isNpc(): boolean {
559-
return this.type === 'npc';
560-
}
561-
562564
public get bonuses(): { offensive: OffensiveBonuses, defensive: DefensiveBonuses, skill: SkillBonuses } {
563565
return this._bonuses;
564566
}

src/engine/world/actor/dialogue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ async function runParsedDialogue(player: Player, dialogueTree: ParsedDialogueTre
666666

667667
export async function dialogue(participants: (Player | NpcParticipant)[], dialogueTree: DialogueTree,
668668
additionalOptions?: AdditionalOptions): Promise<boolean> {
669-
const player = participants.find(p => p instanceof Player) as Player;
669+
const player: Player | undefined = participants.find(p => p instanceof Player);
670670

671671
if (!player) {
672672
throw new Error('Player instance not provided to dialogue action.');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export function syncTrackedActors(packet: Packet, playerPosition: Position, appe
8686
const trackedActor: Actor = trackedActors[i];
8787
let exists = true;
8888

89-
if(trackedActor instanceof Player) {
90-
if(!activeWorld.playerOnline(trackedActor as Player)) {
89+
if(trackedActor.isPlayer()) {
90+
if(!activeWorld.playerOnline(trackedActor)) {
9191
exists = false;
9292
}
9393
} else {

src/engine/world/actor/skills.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ export class Skills extends SkillShortcuts {
226226

227227
this.setExp(skill, finalExp);
228228

229-
if(this.actor instanceof Player) {
229+
if(this.actor.isPlayer()) {
230230
this.actor.outgoingPackets.updateSkill(this.getSkillId(skill), finalLevel, finalExp);
231231
}
232232

233233
if(currentLevel !== finalLevel) {
234234
this.setLevel(skill, finalLevel);
235235

236-
if(this.actor instanceof Player) {
236+
if(this.actor.isPlayer()) {
237237
const achievementDetails = skillDetails[this.getSkillId(skill)];
238238
if(!achievementDetails) {
239239
return;
@@ -262,11 +262,11 @@ export class Skills extends SkillShortcuts {
262262
}
263263

264264
public showLevelUpDialogue(skill: number | SkillName, level: number): void {
265-
if(!(this.actor instanceof Player)) {
265+
if(!(this.actor.isPlayer())) {
266266
return;
267267
}
268268

269-
const player = this.actor as Player;
269+
const player = this.actor;
270270
const achievementDetails = skillDetails[this.getSkillId(skill)];
271271
const widgetId = achievementDetails.advancementWidgetId;
272272

src/engine/world/world.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ export class World {
113113
let tileModifications;
114114
let personalTileModifications;
115115

116-
if(actor.isPlayer) {
117-
const instance = (actor as Player).instance;
116+
if(actor.isPlayer()) {
117+
const instance = actor.instance;
118118

119119
if (!instance) {
120-
throw new Error(`Player ${(actor as Player).username} has no instance.`);
120+
throw new Error(`Player ${actor.username} has no instance.`);
121121
}
122122

123123
tileModifications = instance.getTileModifications(objectPosition);
124-
personalTileModifications = (actor as Player).personalInstance.getTileModifications(objectPosition);
124+
personalTileModifications = actor.personalInstance.getTileModifications(objectPosition);
125125
} else {
126126
tileModifications = this.globalInstance.getTileModifications(objectPosition);
127127
}
@@ -130,7 +130,7 @@ export class World {
130130
if(!landscapeObject) {
131131
const tileObjects = [ ...tileModifications.mods.spawnedObjects ];
132132

133-
if(actor.isPlayer) {
133+
if(actor.isPlayer()) {
134134
tileObjects.push(...personalTileModifications.mods.spawnedObjects);
135135
}
136136

@@ -146,7 +146,7 @@ export class World {
146146

147147
const hiddenTileObjects = [ ...tileModifications.mods.hiddenObjects ];
148148

149-
if(actor.isPlayer) {
149+
if(actor.isPlayer()) {
150150
hiddenTileObjects.push(...personalTileModifications.mods.hiddenObjects);
151151
}
152152

0 commit comments

Comments
 (0)