Prints area after moving between rooms
parent
3f37dd087d
commit
7e1b423b55
@ -1,12 +1,49 @@
|
|||||||
import { ValidCommandDetails } from "./types/ParsedCommand";
|
import { ValidCommandDetails } from "./types/ParsedCommand";
|
||||||
|
import Game from "./Game";
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import printArea from "../utils/printArea"
|
||||||
|
|
||||||
// This class allows for hooking up "global events" for things such as checking
|
// This class allows for hooking up "global events" for things such as checking
|
||||||
// victory conditions, acting when play begins, or other such things. These
|
// victory conditions, acting when play begins, or other such things. These
|
||||||
// event types are different from player actions, which are all considered
|
// event types are different from player actions, which are all considered
|
||||||
// "command" events
|
// "command" events
|
||||||
export default class RulesEngine {
|
export default class RulesEngine extends EventEmitter{
|
||||||
|
private game : Game
|
||||||
|
|
||||||
|
private lastLocation : string = ''
|
||||||
|
|
||||||
|
constructor(game : Game) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
this.game = game
|
||||||
|
this.on('beforeCommand', () => {this.lastLocation = game.getState().player.location})
|
||||||
|
this.on('afterCommand', () => {
|
||||||
|
if(this.lastLocation !== game.getState().player.location)
|
||||||
|
this.emit('locationChange')
|
||||||
|
})
|
||||||
|
|
||||||
|
this.on('locationChange', () => {
|
||||||
|
printArea(game.getCurrentRoom(), game.say.bind(game))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
gameStart() {
|
||||||
|
this.emit('gameStart', this.game)
|
||||||
|
this.emit('locationChange')
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Potentially refactor to not use EventEmitter
|
||||||
|
// since that would maybe allow us to _cancel_ actions?
|
||||||
runCommand(action: ValidCommandDetails) {
|
runCommand(action: ValidCommandDetails) {
|
||||||
|
this.emit('beforeCommand', action, this.game)
|
||||||
|
|
||||||
|
console.log('doing action')
|
||||||
|
|
||||||
|
this.emit('afterCommand', action, this.game)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onGameStart = (cb : (game : Game) => void) => this.on('gameStart', cb)
|
||||||
|
onBeforeCommand = (cb : (command : ValidCommandDetails, game : Game) => void) => this.on('beforeCommand', cb)
|
||||||
|
onAfterCommand = (cb : (command : ValidCommandDetails, game : Game) => void) => this.on('afterCommand', cb)
|
||||||
|
onLocationChange = (cb : (game : Game) => void) => this.on('locationChange', cb)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
export default function capitalize(string) {
|
||||||
|
return string.slice(0,1).toUpperCase() + string.slice(1)
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
|
import capitalize from "./capitalize"
|
||||||
|
|
||||||
export default function printArea(location, say) {
|
export default function printArea(location, say) {
|
||||||
say(`**${location.name}**`)
|
say(`**${location.printableName || capitalize(location.name)}**`)
|
||||||
say(location.description)
|
say(location.description)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue