Beginnings of verb hooks and rules engine

main
Ashelyn Dawn 4 years ago
parent 5f9c76ba72
commit 3f37dd087d

@ -2,6 +2,7 @@ import Game from "./Game";
import RulesEngine from './RulesEngine'
import ParsedCommand, { InvalidCommandDetails } from "./types/ParsedCommand";
import Verb from './types/Verb';
import VerbBuilder from "./types/VerbBuilder";
export default class Parser {
private game : Game
@ -27,7 +28,7 @@ export default class Parser {
if(validationResult.validCommands.length < 1) {
this.handleError(validationResult.invalidCommands)
} else {
// TODO: Do the thing
this.engine.runCommand(validationResult.validCommands[0])
}
this.game.saveDraft()
@ -71,16 +72,3 @@ export default class Parser {
return new VerbBuilder(verb)
}
}
class VerbBuilder {
private verb : Verb
constructor(verb : Verb) {
this.verb = verb
}
as(template : string) : VerbBuilder {
this.verb.understand(template)
return this
}
}

@ -1,4 +1,12 @@
import { ValidCommandDetails } from "./types/ParsedCommand";
// This class allows for hooking up "global events" for things such as checking
// victory conditions, acting when play begins, or other such things. These
// event types are different from player actions, which are all considered
// "command" events
export default class RulesEngine {
// constructor() {
// }
runCommand(action: ValidCommandDetails) {
}
}

@ -43,7 +43,7 @@ export enum ParsingErrorSeverity {
NoSuchObject = 1
}
export type ValidCommandDetails = {isValid: true, command: ParsedCommand, subject: GameObject | null, object: GameObject | null}
export type ValidCommandDetails = {isValid: true, verb: Verb, subject: GameObject | null, object: GameObject | null}
export type InvalidCommandDetails = {isValid: false, command: ParsedCommand, reason: string, severity: ParsingErrorSeverity}
export default class ParsedCommand {
@ -99,7 +99,7 @@ export default class ParsedCommand {
return {
isValid: true,
command: this,
verb: this.verb,
subject,
object
}

@ -1,17 +1,30 @@
import ParsedCommand, {TokenType, ParsedTokenLiteral, ParsedTokenExpression} from './ParsedCommand'
import ParsedCommand, {TokenType, ParsedTokenLiteral, ParsedTokenExpression, ValidCommandDetails} from './ParsedCommand'
import NounPosition from './NounPosition'
import { ObjectType } from './GameState'
import Game from "../Game"
export type VerbAction = (action : ValidCommandDetails, game : Game) => void
export default class Verb {
static expressionRegex = /^\[([a-z|]+)\]$/
private templates : Template[] = []
readonly name : string
private templates : Template[] = []
private hooks : {before: VerbAction[], after : VerbAction[], carryOut: VerbAction[]} = {
before: [],
after: [],
carryOut: []
}
constructor(name : string) {
this.name = name
}
on(type : 'before' | 'after' | 'carryOut', action : VerbAction) {
this.hooks[type].push(action)
}
understand(templateString : string) {
const words : string[] = templateString.split(' ')
const tokens : TemplateToken[] = words.map((word : string) => {

@ -0,0 +1,29 @@
import Verb, { VerbAction } from "./Verb"
export default class VerbBuilder {
private verb : Verb
constructor(verb : Verb) {
this.verb = verb
}
as(template : string) : VerbBuilder {
this.verb.understand(template)
return this
}
before(callback : VerbAction) : VerbBuilder {
this.verb.on('before', callback)
return this
}
carryOut(callback : VerbAction) : VerbBuilder {
this.verb.on('carryOut', callback)
return this
}
after(callback : VerbAction) : VerbBuilder {
this.verb.on('after', callback)
return this
}
}
Loading…
Cancel
Save