UNPKG

doggo-quest-logic

Version:

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

106 lines (105 loc) 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const StoryEntry_1 = require("./StoryEntry"); const StoryEntryType_1 = require("./StoryEntryType"); const Room_1 = require("./World/Room"); class CommandContext { constructor(entries, sentence, world) { this.entries = entries; this.sentence = sentence; this.world = world; } get currentRoom() { return this.world.currentRoom; } get currentRoomObject() { return this.world.getRoom(this.world.currentRoom); } get currentRoomName() { const room = this.currentRoomObject; if (!room) { return 'NO ROOM'; } return room.displayName; } describeCurrentRoom(isFullDescribe) { const gameRoom = this.world.getRoom(this.world.currentRoom); if (!gameRoom) { return; } this.addRoomName(gameRoom.displayName); if (isFullDescribe) { if (gameRoom) { gameRoom.describe(this); } else { this.addError(`No description exists for room ${this.world.currentRoom}`); } } } changeRoom(newRoom, directionName) { const currentRoom = this.world.getRoom(this.currentRoom); if (!currentRoom) { return; } if (currentRoom.tryGo(directionName.toLowerCase(), this)) { return; } if (newRoom == Room_1.Room.CantGo) { this.addText('You can\'t go that way.'); return; } this.addText(`You go ${directionName}`); this.world.currentRoom = newRoom; this.describeCurrentRoom(true); } checkVerb(expectedVerb) { if (this.sentence.verb !== expectedVerb) { this.addSystem(`[Handling as '${expectedVerb}' instead of '${this.sentence.verb}']`); } } addPlayerCommand() { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.PlayerCommand, this.sentence.text, this.sentence, this.currentRoomObject)); } addText(message) { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.StoryNarrative, message, this.sentence, this.currentRoomObject)); } addRoomName(message) { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.RoomName, message, this.sentence, this.currentRoomObject)); } addError(message) { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.CommandError, message, this.sentence, this.currentRoomObject)); } addSystem(message) { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.SystemText, message, this.sentence, this.currentRoomObject)); } addDontSee(target) { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.CommandError, `You don't see a ${target.reduced} here.`, this.sentence, this.currentRoomObject)); } increaseScore(amount) { this.addSystem(`Your score has gone up by ${amount}`); this.world.score += amount; } addLocalObjects() { this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.ObjectList, 'Objects in this room', this.sentence, this.currentRoomObject)); } advanceTime() { if (this.world.isCrateOpen) { this.world.timeAdvanced++; } } restart() { this.world.reset(); while (this.entries.length > 0) { this.entries.pop(); } this.addInitialEntries(); } addInitialEntries() { this.addSystem('Welcome to Doggo Quest!'); this.addSystem('Doggo Quest is an Interactive Fiction game created by Matt Eland (@IntegerMan)'); this.addSystem('This game is implemented in Angular / TypeScript using Angular Material for styling with Compromise-NLP for text parsing.'); this.entries.push(new StoryEntry_1.StoryEntry(StoryEntryType_1.StoryEntryType.Divider, '')); } } exports.CommandContext = CommandContext;