-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from rune-js/npcs
NPCs!
- Loading branch information
Showing
15 changed files
with
518 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- npcId: 0 | ||
x: 3222 | ||
y: 3222 | ||
radius: 10 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { logger } from '@runejs/logger/dist/logger'; | ||
import { JSON_SCHEMA, safeLoad } from 'js-yaml'; | ||
import { readFileSync } from "fs"; | ||
import { join } from "path"; | ||
import { serverDir } from '../../game-server'; | ||
import { Direction } from '../world'; | ||
|
||
export interface NpcSpawn { | ||
npcId: number; | ||
x: number; | ||
y: number; | ||
level?: number; | ||
radius?: number; | ||
face?: Direction; | ||
} | ||
|
||
export function parseNpcSpawns(): NpcSpawn[] { | ||
try { | ||
logger.info('Parsing npc spawns...'); | ||
|
||
const npcSpawns = safeLoad(readFileSync(join(serverDir, 'data/config/npc-spawns.yaml'), 'utf8'), { schema: JSON_SCHEMA }) as NpcSpawn[]; | ||
|
||
if(!npcSpawns || npcSpawns.length === 0) { | ||
throw 'Unable to read npc spawns.'; | ||
} | ||
|
||
logger.info(`${npcSpawns.length} npc spawns found.`); | ||
|
||
return npcSpawns; | ||
} catch(error) { | ||
logger.error('Error parsing npc spawns: ' + error); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Mob } from '../mob'; | ||
import { NpcSpawn } from '../../../config/npc-spawn'; | ||
import { NpcDefinition } from '@runejs/cache-parser'; | ||
import uuidv4 from 'uuid/v4'; | ||
import { Position } from '../../../position'; | ||
import { Direction } from '../../../world'; | ||
import { world } from '../../../../game-server'; | ||
|
||
interface NpcAnimations { | ||
walk: number; | ||
stand: number; | ||
turnAround: number; | ||
turnRight: number; | ||
turnLeft: number; | ||
} | ||
|
||
/** | ||
* Represents a non-player character within the game world. | ||
*/ | ||
export class Npc extends Mob { | ||
|
||
public readonly id: number; | ||
public readonly uuid: string; | ||
private _name: string; | ||
private _combatLevel: number; | ||
private _animations: NpcAnimations; | ||
private _movementRadius: number = 0; | ||
private _initialFaceDirection: Direction = 'NORTH'; | ||
public readonly initialPosition: Position; | ||
|
||
public constructor(npcSpawn: NpcSpawn, cacheData: NpcDefinition) { | ||
super(); | ||
this.id = cacheData.id; | ||
this.uuid = uuidv4(); | ||
this._name = cacheData.name; | ||
this._combatLevel = cacheData.combatLevel; | ||
this._animations = cacheData.animations as NpcAnimations; | ||
this.position = new Position(npcSpawn.x, npcSpawn.y, npcSpawn.level); | ||
this.initialPosition = new Position(npcSpawn.x, npcSpawn.y, npcSpawn.level); | ||
|
||
if(npcSpawn.radius) { | ||
this._movementRadius = npcSpawn.radius; | ||
} | ||
|
||
if(npcSpawn.face) { | ||
this._initialFaceDirection = npcSpawn.face; | ||
} | ||
} | ||
|
||
public init(): void { | ||
world.chunkManager.getChunkForWorldPosition(this.position).addNpc(this); | ||
this.initiateRandomMovement(); | ||
} | ||
|
||
public tick(): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
this.walkingQueue.process(); | ||
resolve(); | ||
}); | ||
} | ||
|
||
public reset(): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
this.updateFlags.reset(); | ||
resolve(); | ||
}); | ||
} | ||
|
||
public equals(other: Npc): boolean { | ||
if(!other) { | ||
return false; | ||
} | ||
|
||
return other.id === this.id && other.uuid === this.uuid; | ||
} | ||
|
||
public get name(): string { | ||
return this._name; | ||
} | ||
|
||
public get combatLevel(): number { | ||
return this._combatLevel; | ||
} | ||
|
||
public get animations(): NpcAnimations { | ||
return this._animations; | ||
} | ||
|
||
public get movementRadius(): number { | ||
return this._movementRadius; | ||
} | ||
|
||
public get initialFaceDirection(): Direction { | ||
return this._initialFaceDirection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.