Skip to content

Commit

Permalink
Adding ActorTeleportTask, teleport method to Actor, and fixing ladders
Browse files Browse the repository at this point in the history
  • Loading branch information
Katilith committed Mar 1, 2024
1 parent c774f35 commit c5cd118
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
53 changes: 53 additions & 0 deletions src/engine/task/impl/actor-teleport-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Position } from '@engine/world';
import { Actor } from '@engine/world/actor';
import { ActorTask } from '@engine/task/impl/actor-task';

/**
* A task for an actor to teleport to a new position.
*
* @author Kat
*/
export class ActorTeleportTask<TActor extends Actor = Actor> extends ActorTask<TActor> {
private readonly _newPosition: Position;

/**
* @param actor The actor executing this task.
* @param newPosition The position to teleport the actor to.
*/
constructor (
actor: TActor,
newPosition: Position
) {
super(actor, {
repeat: false,
immediate: false
});
this._newPosition = newPosition;
}

/**
* Teleports the actor to the new position.
*
* TODO (Kat) unit test this
*/
public execute() {
if (!this.newPosition) {
return;
}

this.actor.teleport(this.newPosition);
}

/**
* Get the position the actor will be teleported to.
*
* @returns The position that the actor will be teleported to.
*/
protected get newPosition(): Position | null {
if (!this._newPosition) {
return null;
}

return this._newPosition;
}
}
15 changes: 12 additions & 3 deletions src/engine/world/actor/actor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Subject } from 'rxjs';
import { filter, take } from 'rxjs/operators';

import { LandscapeObject } from '@runejs/filestore';

import { DefensiveBonuses, OffensiveBonuses, SkillBonuses } from '@engine/config';
import { Position, DirectionData, directionFromIndex, WorldInstance, activeWorld } from '@engine/world';
import { activeWorld, directionFromIndex, Position, WorldInstance } from '@engine/world';
import { Item, ItemContainer } from '@engine/world/items';
import { ActionCancelType, ActionPipeline } from '@engine/action';

Expand Down Expand Up @@ -133,6 +131,17 @@ export abstract class Actor {
this.scheduler.enqueue(task);
}

/**
* Instantly teleports the actor to the specified location.
* @param newPosition The actor's new position.
*/
public teleport(newPosition: Position): void {
this.walkingQueue.clear();
this.metadata['lastPosition'] = this.position.copy();
this.position = newPosition;
this.metadata.teleporting = true;
}

public clearBonuses(): void {
this._bonuses = {
offensive: {
Expand Down
21 changes: 14 additions & 7 deletions src/plugins/objects/ladders/ladder.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { objectInteractionActionHandler } from '@engine/action';
import { dialogueAction } from '@engine/world/actor/player/dialogue-action';
import { World } from '@engine/world';
import { Position } from '@engine/world/position';
import { logger } from '@runejs/common';
import { ActorTeleportTask } from '@engine/task/impl/actor-teleport-task';
import { Task } from '@engine/task';
import { ActorTask } from '@engine/task/impl';
import { Actor, Player } from '@engine/world/actor';


const planes = { min: 0, max: 3 };
Expand Down Expand Up @@ -31,7 +34,15 @@ export const action: objectInteractionActionHandler = (details) => {
switch (d.action) {
case 1:
case 2:
action({ ...details, option: `climb-${(d.action === 1 ? 'up' : 'down')}` });
player.enqueueTask(class LadderTask extends ActorTask {
constructor(actor: Actor) {
super(actor, { repeat: false, immediate: false });
}

execute() {
action({ ...details, option: `climb-${(d.action === 1 ? 'up' : 'down')}` });
}
});
return;
}
});
Expand All @@ -57,11 +68,7 @@ export const action: objectInteractionActionHandler = (details) => {
player.playAnimation(up ? 828 : 827);
}
player.sendMessage(`You climb ${option.slice(6)} the ${ladderObjectName.toLowerCase()}.`);

// this used to use `setInterval` but will need rewriting to be synced with ticks
// see https://github.com/runejs/server/issues/417
details.player.sendMessage('[debug] see issue #417');
// setTimeout(() => details.player.teleport(newPosition), World.TICK_LENGTH);
player.enqueueTask(ActorTeleportTask, [newPosition]);
};

export default {
Expand Down

0 comments on commit c5cd118

Please sign in to comment.