Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the following API a little #182

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/plugins/commands/follow-command.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ActionType } from '../plugin';
import { world } from '../../game-server';
import { followActor } from '../../world/actor/player/action/follow-action';

module.exports = {
type: ActionType.COMMAND,
commands: 'follow',
action: details => followActor(details.player, world.npcList[0])
action: details => details.player.follow(world.npcList[0])
};
3 changes: 1 addition & 2 deletions src/plugins/player/follow-player-plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ActionType } from '../plugin';
import { followActor } from '../../world/actor/player/action/follow-action';

module.exports = {
type: ActionType.PLAYER_ACTION,
options: 'follow',
action: details => followActor(details.player, details.otherPlayer)
action: details => details.player.follow(details.otherPlayer)
};
94 changes: 84 additions & 10 deletions src/world/actor/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ import { CombatAction } from '@server/world/actor/player/action/combat-action';
import { Pathfinding } from '@server/world/actor/pathfinding';
import { Subject } from 'rxjs';
import { ActionCancelType } from '@server/world/actor/player/action/action';
import { filter, take } from 'rxjs/operators';

/**
* Handles an actor within the game world.
*/
export abstract class Actor {

public readonly updateFlags: UpdateFlags;
public readonly skills: Skills;
public readonly metadata: { [key: string]: any } = {};
public readonly actionsCancelled: Subject<ActionCancelType>;
public readonly movementEvent: Subject<Position>;
public pathfinding: Pathfinding;
public lastMovementPosition: Position;
private readonly _walkingQueue: WalkingQueue;
private readonly _inventory: ItemContainer;
private readonly _bank: ItemContainer;
private _position: Position;
private _lastMapRegionUpdatePosition: Position;
private _worldIndex: number;
public readonly updateFlags: UpdateFlags;
private readonly _walkingQueue: WalkingQueue;
private _walkDirection: number;
private _runDirection: number;
private _faceDirection: number;
private readonly _inventory: ItemContainer;
private readonly _bank: ItemContainer;
public readonly skills: Skills;
private _busy: boolean;
public readonly metadata: { [key: string]: any } = {};
private _combatActions: CombatAction[];
public pathfinding: Pathfinding;
public lastMovementPosition: Position;
public readonly actionsCancelled: Subject<ActionCancelType>;
public readonly movementEvent: Subject<Position>;

protected constructor() {
this.updateFlags = new UpdateFlags();
Expand All @@ -52,7 +53,80 @@ export abstract class Actor {
}

public damage(amount: number, damageType: DamageType = DamageType.DAMAGE): void {
}

public async moveBehind(target: Actor): Promise<boolean> {
const distance = Math.floor(this.position.distanceBetween(target.position));
if(distance > 16) {
this.clearFaceActor();
return false;
}

let ignoreDestination = true;
let desiredPosition = target.position;
if(target.lastMovementPosition) {
desiredPosition = target.lastMovementPosition;
ignoreDestination = false;
}

await this.pathfinding.walkTo(desiredPosition, {
pathingSearchRadius: distance + 2,
ignoreDestination
});

return true;
}

public follow(target: Actor): void {
this.face(target, false, false, false);

this.moveBehind(target);
const subscription = target.movementEvent.subscribe(() => this.moveBehind(target));

this.actionsCancelled.pipe(
filter(type => type !== 'pathing-movement'),
take(1)
).subscribe(() => {
subscription.unsubscribe();
this.face(null);
});
}

public async walkTo(target: Actor): Promise<boolean> {
const distance = Math.floor(this.position.distanceBetween(target.position));
if(distance > 16) {
this.clearFaceActor();
this.metadata.faceActorClearedByWalking = true;
return false;
}

if(distance <= 1) {
return false;
}

const desiredPosition = target.position;

await this.pathfinding.walkTo(desiredPosition, {
pathingSearchRadius: distance + 2,
ignoreDestination: true
});

return true;
}

public tail(target: Actor): void {
this.face(target, false, false, false);

this.walkTo(target);
const subscription = target.movementEvent.subscribe(() => this.walkTo(target));

this.actionsCancelled.pipe(
filter(type => type !== 'pathing-movement'),
take(1)
).subscribe(() => {
subscription.unsubscribe();
this.face(null);
});
}

public face(face: Position | Actor | null, clearWalkingQueue: boolean = true, autoClear: boolean = true, clearedByWalking: boolean = true): void {
Expand Down
42 changes: 0 additions & 42 deletions src/world/actor/player/action/follow-action.ts

This file was deleted.