UNPKG

doggo-quest-logic

Version:

The game logic for the Doggo Quest text-based game sample project

72 lines (71 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const CommandContext_1 = require("./CommandContext"); const NounMapper_1 = require("./Parsing/NounMapper"); const Parser_1 = require("./Parsing/Parser"); const Sentence_1 = require("./Parsing/Sentence"); const VerbHandler_1 = require("./Parsing/VerbHandler"); const GameWorld_1 = require("./World/GameWorld"); class StoryEngine { constructor() { this.score = 0; this.parser = new Parser_1.Parser(); this.state = new GameWorld_1.GameWorld(); this.verbs = new VerbHandler_1.VerbHandler(); this.mapper = new NounMapper_1.NounMapper(); } getInitialEntries() { const entries = []; const context = new CommandContext_1.CommandContext(entries, new Sentence_1.Sentence(), this.state); context.addInitialEntries(); context.describeCurrentRoom(true); this.score = context.world.score; return entries; } handlePlayerInput(text) { const entries = []; const sentence = this.parser.parse(text); const context = new CommandContext_1.CommandContext(entries, sentence, this.state); this.mapper.mapNouns(context); context.addPlayerCommand(); const validationResult = sentence.validate(); if (validationResult) { context.addError(validationResult); } else { this.handleCommand(context); } if (console && console.debug) { console.debug(`Time Elapsed ${context.world.timeAdvanced}`); } this.score = context.world.score; return entries; } getResponseState(command) { const results = this.handlePlayerInput(command); return { state: this.state, responses: results, responseText: results.map(r => r.Text).join('\r\n') }; } getResponse(command) { return this.getResponseState(command).responseText; } handleCommands(commands) { commands.forEach(c => this.handlePlayerInput(c)); return this.state; } handleCommand(context) { if (!context.sentence.verb) return; const handler = this.verbs.getHandler(context.sentence.verb); if (handler) { handler(context); } else { context.addSystem(`You can't ${context.sentence.verb} in this game.`); } } } exports.StoryEngine = StoryEngine;