|
| 1 | +import { activeWorld, Position } from '@engine/world'; |
| 2 | +import { Actor } from '@engine/world/actor'; |
| 3 | +import { LandscapeObject } from '@runejs/filestore'; |
| 4 | +import { ActorWalkToTask } from './actor-walk-to-task'; |
| 5 | + |
| 6 | +/** |
| 7 | + * A task for an actor to interact with a {@link LandscapeObject}. |
| 8 | + * |
| 9 | + * This task extends {@link ActorWalkToTask} and will walk the actor to the object. |
| 10 | + * Once the actor is within range of the object, the task will expose the {@link landscapeObject} property |
| 11 | + * |
| 12 | + * @author jameskmonger |
| 13 | + */ |
| 14 | +export abstract class ActorLandscapeObjectInteractionTask<TActor extends Actor = Actor> extends ActorWalkToTask<TActor> { |
| 15 | + private _landscapeObject: LandscapeObject; |
| 16 | + |
| 17 | + /** |
| 18 | + * Gets the {@link LandscapeObject} that this task is interacting with. |
| 19 | + * |
| 20 | + * @returns If the object is still present, and the actor is at the destination, the object. |
| 21 | + * Otherwise, `null`. |
| 22 | + * |
| 23 | + * TODO (jameskmonger) unit test this |
| 24 | + */ |
| 25 | + protected get landscapeObject(): LandscapeObject | null { |
| 26 | + // TODO (jameskmonger) consider if we want to do these checks rather than delegating to the child task |
| 27 | + // as currently the subclass has to store it in a subclass property if it wants to use it |
| 28 | + // without these checks |
| 29 | + if (!this.atDestination) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + if (!this._landscapeObject) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + return this._landscapeObject; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param actor The actor executing this task. |
| 42 | + * @param landscapeObject The landscape object to interact with. |
| 43 | + * @param sizeX The size of the LandscapeObject in the X direction. |
| 44 | + * @param sizeY The size of the LandscapeObject in the Y direction. |
| 45 | + */ |
| 46 | + constructor ( |
| 47 | + actor: TActor, |
| 48 | + landscapeObject: LandscapeObject, |
| 49 | + sizeX: number = 1, |
| 50 | + sizeY: number = 1 |
| 51 | + ) { |
| 52 | + super( |
| 53 | + actor, |
| 54 | + new Position(landscapeObject.x, landscapeObject.y, landscapeObject.level), |
| 55 | + Math.max(sizeX, sizeY) |
| 56 | + ); |
| 57 | + |
| 58 | + if (!landscapeObject) { |
| 59 | + this.stop(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + this._landscapeObject = landscapeObject; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Checks for the continued presence of the {@link LandscapeObject} and stops the task if it is no longer present. |
| 68 | + * |
| 69 | + * TODO (jameskmonger) unit test this |
| 70 | + */ |
| 71 | + public execute() { |
| 72 | + super.execute(); |
| 73 | + |
| 74 | + if (!this.isActive || !this.atDestination) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (!this._landscapeObject) { |
| 79 | + this.stop(); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const { object: worldObject } = activeWorld.findObjectAtLocation(this.actor, this._landscapeObject.objectId, this.destination); |
| 84 | + |
| 85 | + if (!worldObject) { |
| 86 | + this.stop(); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments