Skip to content

Commit

Permalink
Merge pull request #23 from rune-js/face-dir
Browse files Browse the repository at this point in the history
Making player updating send player face direction upon adding new players.
  • Loading branch information
TheBlackParade authored Jan 5, 2020
2 parents 2b21c92 + 28c04d5 commit 1a3f8b2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/world/entity/mob/mob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export abstract class Mob extends Entity {
private readonly _walkingQueue: WalkingQueue;
private _walkDirection: number;
private _runDirection: number;
private _faceDirection: number;
private readonly _inventory: ItemContainer;

protected constructor() {
super();
this._walkingQueue = new WalkingQueue(this);
this._walkDirection = -1;
this._runDirection = -1;
this._faceDirection = -1;
this._inventory = new ItemContainer(28);
}

Expand Down Expand Up @@ -94,6 +96,14 @@ export abstract class Mob extends Entity {
this._runDirection = value;
}

public get faceDirection(): number {
return this._faceDirection;
}

public set faceDirection(value: number) {
this._faceDirection = value;
}

public get inventory(): ItemContainer {
return this._inventory;
}
Expand Down
8 changes: 8 additions & 0 deletions src/world/entity/mob/player/task/player-update-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export class PlayerUpdateTask extends Task<void> {
if(updateFlags.appearanceUpdateRequired || forceUpdate) {
mask |= 0x4;
}
if(updateFlags.facePosition || forceUpdate) {
mask |= 0x2;
}

if(mask >= 0xff) {
mask |= 0x20;
Expand All @@ -157,6 +160,11 @@ export class PlayerUpdateTask extends Task<void> {
updateMaskData.writeByte(mask);
}

if(updateFlags.facePosition || forceUpdate) {
const position = updateFlags.facePosition ? updateFlags.facePosition : player.position.fromDirection(player.faceDirection);
updateMaskData.writeShortBE(position.x * 2 + 1);
updateMaskData.writeShortBE(position.y * 2 + 1);
}
if(updateFlags.appearanceUpdateRequired || forceUpdate) {
const equipment = player.equipment;
const appearanceData: RsBuffer = RsBuffer.create();
Expand Down
14 changes: 13 additions & 1 deletion src/world/entity/mob/player/update-flags.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Position } from '../../../position';

/**
* Various player updating flags.
*/
export class UpdateFlags {

private _mapRegionUpdateRequired: boolean;
private _appearanceUpdateRequired: boolean;
private _facePosition: Position;

public constructor() {
this.reset();
Expand All @@ -13,10 +16,11 @@ export class UpdateFlags {
public reset(): void {
this._mapRegionUpdateRequired = false;
this._appearanceUpdateRequired = false;
this._facePosition = null;
}

get updateBlockRequired(): boolean {
return this._appearanceUpdateRequired;
return this._appearanceUpdateRequired || this._facePosition !== null;
}

get mapRegionUpdateRequired(): boolean {
Expand All @@ -34,4 +38,12 @@ export class UpdateFlags {
set appearanceUpdateRequired(value: boolean) {
this._appearanceUpdateRequired = value;
}

public get facePosition(): Position {
return this._facePosition;
}

public set facePosition(value: Position) {
this._facePosition = value;
}
}
6 changes: 6 additions & 0 deletions src/world/entity/mob/walking-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ export class WalkingQueue {
this.mob.walkDirection = walkDir;
this.mob.runDirection = runDir;

if(runDir !== -1) {
this.mob.faceDirection = runDir;
} else {
this.mob.faceDirection = walkDir;
}

// @TODO NPC map region changing
if(this.mob instanceof Player) {
const newChunk = world.chunkManager.getChunkForWorldPosition(this.mob.position);
Expand Down
7 changes: 7 additions & 0 deletions src/world/position.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const directionDeltaX = [-1, 0, 1, -1, 1, -1, 0, 1];
const directionDeltaY = [1, 1, 1, 0, 0, -1, -1, -1];

/**
* Represents a single position, or coordinate, within the game world.
*/
Expand Down Expand Up @@ -49,6 +52,10 @@ export class Position {
return Math.abs(Math.sqrt((this.x - other.x) * (this.x - other.x) + (this.y - other.y) * (this.y - other.y)));
}

public fromDirection(direction: number): Position {
return new Position(this.x + directionDeltaX[direction], this.y + directionDeltaY[direction], this.level);
}

public equals(position: Position | { x: number, y: number, level: number }): boolean {
if(!(position instanceof Position)) {
position = new Position(position.x, position.y, position.level);
Expand Down

0 comments on commit 1a3f8b2

Please sign in to comment.