Skip to content

Commit 163f7da

Browse files
committed
Fixing circular dependency issues with Actor, cleaning up more imports
1 parent 1358c95 commit 163f7da

File tree

14 files changed

+164
-151
lines changed

14 files changed

+164
-151
lines changed

src/game-engine/world/action/hooks/task.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import { ActionStrength } from '@engine/world/action/action-pipeline';
1212
export type TaskSessionData = { [key: string]: any };
1313

1414

15+
export interface TaskDetails<T> {
16+
actor: Actor;
17+
player: Player | undefined;
18+
npc: Npc | undefined;
19+
actionData: T;
20+
session: TaskSessionData;
21+
}
22+
23+
1524
export interface HookTask<T = any> {
1625
canActivate?: <Q = T>(task: TaskExecutor<Q>, iteration?: number) => boolean | Promise<boolean>;
1726
activate: <Q = T>(task: TaskExecutor<Q>, iteration?: number) => void | undefined | boolean | Promise<void | undefined | boolean>;
@@ -138,23 +147,11 @@ export class TaskExecutor<T> {
138147
this.session = null;
139148
}
140149

141-
public getDetails(): {
142-
actor: Actor;
143-
player: Player | undefined;
144-
npc: Npc | undefined;
145-
actionData: T;
146-
session: TaskSessionData; } {
147-
const {
148-
type: {
149-
player,
150-
npc
151-
}
152-
} = this.actor;
153-
150+
public getDetails(): TaskDetails<T> {
154151
return {
155152
actor: this.actor,
156-
player,
157-
npc,
153+
player: this.actor.isPlayer ? this.actor as Player : undefined,
154+
npc: this.actor.isNpc ? this.actor as Npc : undefined,
158155
actionData: this.actionData,
159156
session: this.session
160157
};

src/game-engine/world/actor/actor.ts

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
import { WalkingQueue } from './walking-queue';
2-
import { ItemContainer } from '@engine/world';
3-
import { Animation, DamageType, Graphic, UpdateFlags } from './update-flags';
4-
import { Npc } from './npc';
5-
import { Skill, Skills } from '@engine/world/actor/skills';
6-
import { Item } from '@engine/world/items/item';
7-
import { Position } from '@engine/world/position';
8-
import { DirectionData, directionFromIndex } from '@engine/world/direction';
9-
import { Pathfinding } from '@engine/world/actor/pathfinding';
101
import { Subject } from 'rxjs';
112
import { filter, take } from 'rxjs/operators';
12-
import { world } from '@engine/game-server';
13-
import { WorldInstance } from '@engine/world/instances';
14-
import { Player } from '@engine/world/actor/player/player';
15-
import { ActionCancelType, ActionPipeline } from '@engine/world/action/action-pipeline';
3+
164
import { LandscapeObject } from '@runejs/filestore';
17-
import { Behavior } from './behaviors/behavior';
18-
import { soundIds } from '../config/sound-ids';
5+
6+
import { world } from '@engine/game-server';
7+
import { DefensiveBonuses, OffensiveBonuses, SkillBonuses } from '@engine/config';
8+
import { Position, DirectionData, directionFromIndex, WorldInstance } from '@engine/world';
9+
import { Item, ItemContainer } from '@engine/world/items';
10+
import { ActionCancelType, ActionPipeline } from '@engine/world/action';
11+
import { soundIds } from '@engine/world/config';
12+
13+
import { WalkingQueue } from './walking-queue';
14+
import { Animation, DamageType, Graphic, UpdateFlags } from './update-flags';
15+
import { Skill, Skills } from './skills';
16+
import { Pathfinding } from './pathfinding';
1917
import { Attack, AttackDamageType } from './player/attack';
18+
import { Behavior } from './behaviors';
2019
import { Effect, EffectType } from './effect';
2120

21+
22+
export type ActorType = 'player' | 'npc';
23+
24+
2225
/**
23-
* Handles an actor within the game world.
26+
* Handles an entity within the game world.
2427
*/
2528
export abstract class Actor {
2629

30+
public readonly type: ActorType;
2731
public readonly updateFlags: UpdateFlags = new UpdateFlags();
2832
public readonly skills: Skills = new Skills(this);
2933
public readonly walkingQueue: WalkingQueue = new WalkingQueue(this);
@@ -47,34 +51,44 @@ export abstract class Actor {
4751
public combatTargets: Actor[] = [];
4852
public hitPoints = this.skills.hitpoints.level * 4;
4953
public maxHitPoints = this.skills.hitpoints.level * 4;
50-
51-
public get damageType() {
52-
return this._damageType;
53-
}
54-
public set damageType(value) {
55-
this._damageType = value;
56-
}
5754
public effects: Effect[] = []; //spells, effects, prayers, etc
5855

5956
protected randomMovementInterval;
57+
protected _instance: WorldInstance = null;
58+
6059
/**
6160
* @deprecated - use new action system instead
6261
*/
63-
private _busy: boolean;
62+
private _busy: boolean = false;
6463
private _position: Position;
6564
private _lastMapRegionUpdatePosition: Position;
6665
private _worldIndex: number;
6766
private _walkDirection: number;
6867
private _runDirection: number;
6968
private _faceDirection: number;
70-
private _instance: WorldInstance = null;
7169
private _damageType = AttackDamageType.Crush;
70+
private _bonuses: { offensive: OffensiveBonuses, defensive: DefensiveBonuses, skill: SkillBonuses };
7271

73-
protected constructor() {
72+
protected constructor(actorType: ActorType) {
73+
this.type = actorType;
7474
this._walkDirection = -1;
7575
this._runDirection = -1;
7676
this._faceDirection = 6;
77-
this._busy = false;
77+
this.clearBonuses();
78+
}
79+
80+
public clearBonuses(): void {
81+
this._bonuses = {
82+
offensive: {
83+
speed: 0, stab: 0, slash: 0, crush: 0, magic: 0, ranged: 0
84+
},
85+
defensive: {
86+
stab: 0, slash: 0, crush: 0, magic: 0, ranged: 0
87+
},
88+
skill: {
89+
strength: 0, prayer: 0
90+
}
91+
};
7892
}
7993

8094
public get highestCombatSkill(): Skill {
@@ -109,12 +123,10 @@ export abstract class Actor {
109123
// add 8
110124
// ToDo: add void bonues (effects)
111125
// round result down
112-
let equipmentBonus = 0;
113-
if (this.isPlayer) {
114-
const player = (this as unknown as Player);
115-
equipmentBonus = player.bonuses.offensive.crush;
126+
let equipmentBonus = this.bonuses.offensive.crush ?? 0;
127+
if (equipmentBonus <= 0) {
128+
equipmentBonus = 1;
116129
}
117-
if (equipmentBonus == 0) equipmentBonus = 1;
118130
/*
119131
* To calculate your maximum hit:
120132
@@ -169,6 +181,7 @@ export abstract class Actor {
169181
attack.damage = Math.round((maximumHit * attack.hitChance) / 2);
170182
return attack;
171183
}
184+
172185
public getDefenseRoll(attack: Attack): Attack {
173186
//attack need to know the damage roll, which is the item bonuses the weapon damage type etc.
174187

@@ -185,7 +198,7 @@ export abstract class Actor {
185198
// ToDo: add void bonuses (effects)
186199
// round result down
187200

188-
const equipmentBonus: number = this.isPlayer ? (this as unknown as Player).bonuses.defensive.crush : 0; //object prototyping to find property by name (JS style =/)
201+
const equipmentBonus: number = this.bonuses.defensive.crush ?? 0; //object prototyping to find property by name (JS style =/)
189202

190203
const stanceModifier: number = _stance_accurate;
191204

@@ -498,10 +511,10 @@ export abstract class Actor {
498511
return;
499512
}
500513

501-
if(this instanceof Npc) {
502-
const nearbyPlayers = world.findNearbyPlayers(this.position, 24, this.instanceId);
514+
if(this.isNpc) {
515+
const nearbyPlayers = world.findNearbyPlayers(this.position, 24, this.instance?.instanceId);
503516
if(nearbyPlayers.length === 0) {
504-
// No need for this NPC to move if there are no players nearby to see it
517+
// No need for this actor to move if there are no players nearby to witness it, save some memory. :)
505518
return;
506519
}
507520
}
@@ -548,11 +561,8 @@ export abstract class Actor {
548561

549562
let valid = true;
550563

551-
if(this instanceof Npc) {
552-
if(px > this.initialPosition.x + this.movementRadius || px < this.initialPosition.x - this.movementRadius
553-
|| py > this.initialPosition.y + this.movementRadius || py < this.initialPosition.y - this.movementRadius) {
554-
valid = false;
555-
}
564+
if(!this.withinBounds(px, py)) {
565+
valid = false;
556566
}
557567

558568
movementAllowed = valid;
@@ -587,11 +597,8 @@ export abstract class Actor {
587597
px += movementDirection.deltaX;
588598
py += movementDirection.deltaY;
589599

590-
if(this instanceof Npc) {
591-
if(px > this.initialPosition.x + this.movementRadius || px < this.initialPosition.x - this.movementRadius
592-
|| py > this.initialPosition.y + this.movementRadius || py < this.initialPosition.y - this.movementRadius) {
593-
valid = false;
594-
}
600+
if(!this.withinBounds(px, py)) {
601+
valid = false;
595602
}
596603

597604
}
@@ -608,6 +615,10 @@ export abstract class Actor {
608615
}
609616
}
610617

618+
public withinBounds(x: number, y: number): boolean {
619+
return true;
620+
}
621+
611622
public abstract getAttackAnimation(): number;
612623
public abstract getBlockAnimation(): number;
613624
public abstract equals(actor: Actor): boolean;
@@ -664,6 +675,14 @@ export abstract class Actor {
664675
this._faceDirection = value;
665676
}
666677

678+
public get damageType() {
679+
return this._damageType;
680+
}
681+
682+
public set damageType(value) {
683+
this._damageType = value;
684+
}
685+
667686
public get busy(): boolean {
668687
return this._busy;
669688
}
@@ -677,34 +696,19 @@ export abstract class Actor {
677696
}
678697

679698
public set instance(value: WorldInstance) {
680-
if(this instanceof Player) {
681-
const currentInstance = this._instance;
682-
if(currentInstance?.instanceId) {
683-
currentInstance.removePlayer(this);
684-
}
685-
686-
if(value) {
687-
value.addPlayer(this);
688-
}
689-
}
690-
691699
this._instance = value;
692700
}
693701

694702
public get isPlayer(): boolean {
695-
return this instanceof Player;
703+
return false;
696704
}
697705

698706
public get isNpc(): boolean {
699-
return this instanceof Npc;
707+
return false;
700708
}
701709

702-
public get type(): { player?: Player, npc?: Npc } {
703-
return {
704-
player: this.isPlayer ? this as unknown as Player : undefined,
705-
npc: this.isNpc ? this as unknown as Npc : undefined
706-
};
710+
public get bonuses(): { offensive: OffensiveBonuses, defensive: DefensiveBonuses, skill: SkillBonuses } {
711+
return this._bonuses;
707712
}
708713

709-
710714
}

src/game-engine/world/actor/behaviors/behavior.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { Actor } from '../actor';
2-
import { regionChangeActionFactory } from '@engine/world/action/region-change.action';
3-
import { Subject } from 'rxjs';
4-
import { Npc } from '../npc';
5-
import { Player } from '../player/player';
2+
63

74
export abstract class Behavior {
85
//because not all interaction between npcs will be combat oriented me/them is on the base class
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export * from './actor';
2-
export * from './player';
3-
export * from './npc';
4-
51
export * from './combat';
62
export * from './dialogue';
73
export * from './effect';
@@ -11,3 +7,7 @@ export * from './prayer';
117
export * from './skills';
128
export * from './update-flags';
139
export * from './walking-queue';
10+
11+
export * from './player';
12+
export * from './npc';
13+
export * from './actor';

0 commit comments

Comments
 (0)