UNPKG

doggo-quest-logic

Version:

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

175 lines (174 loc) 6.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class VerbHandler { static handleVerb(context, verbName, nonTargetedResponse, genericResponse) { context.checkVerb(verbName); const target = context.sentence.target; if (target) { return this.handleTargetedVerb(verbName, target, context, genericResponse); } else { context.addText(nonTargetedResponse); return true; } } static handleTargetedVerb(verbName, target, context, genericResponse) { const gameObject = target.gameObject; if (gameObject) { const handler = gameObject[verbName]; if (handler) { switch (typeof handler) { case 'string': context.addText(handler); break; default: const action = handler; action(context); break; } context.advanceTime(); } else { context.addText(genericResponse); } return true; } else { context.addDontSee(target); return false; } } handleLook(context) { context.checkVerb('look'); const target = context.sentence.target; if (target) { return VerbHandler.handleTargetedVerb('look', target, context, `The ${target.reduced} is pretty much as you'd expect.`); } else { context.describeCurrentRoom(true); return true; } } handleEat(context) { return VerbHandler.handleVerb(context, 'eat', `If you want to eat something, you'll need to say what you want to eat. Try saying 'eat the kibble' or similar.`, `On second thought, it doesn't look all that tasty.`); } handlePush(context) { return VerbHandler.handleVerb(context, 'push', `You can't push nothing. Try saying 'push the crate' or similar.`, `Pushing it doesn't result in anything interesting.`); } handleListen(context) { return VerbHandler.handleVerb(context, 'listen', `You stop everything and listen. A few moments pass, but you don't hear anything interesting.`, `It doesn't seem to be making many sound at the moment.`); } handleSmell(context) { return VerbHandler.handleVerb(context, 'smell', `You take a quick mix of shallow and deep sniffs in, but don't smell anything surprising.`, `It's smell isn't very surprising or interesting.`); } handleThinkAbout(context) { return VerbHandler.handleVerb(context, 'think', `You do some thinking and feel like you still have the wiggles. Time to roam the house and make a mess!`, `You stare at it and think, but nothing interesting comes to mind. You find yourself suddenly wanting to destroy things.`); } handleGo(context) { context.checkVerb('go'); const target = context.sentence.target; if (!target) { context.addError(`You need to say which way you want to go. For example, try 'go to the north' or 'go west'`); return false; } let roomTarget = target.room; if (roomTarget === undefined && target && target.gameObject) { roomTarget = target.gameObject.getRoomMapping(); } if (roomTarget !== undefined && roomTarget !== context.currentRoom) { context.changeRoom(roomTarget, target.text); return true; } else { context.addError(`You can't go that way`); return false; } } handleBark(context) { return VerbHandler.handleVerb(context, 'bark', `You tilt your head back and bark loudly at the entire world. The world ignores you.`, `You glare at it and give it your fiercest bark. It makes no move in response. It must be scared.`); } handleDebug(context) { context.addLocalObjects(); return false; } handleGet(context) { return VerbHandler.handleVerb(context, 'take', `Try tying what you'd like to get, for example 'Get blanket'.`, `You can't take that with you, sadly.`); } handleTaste(context) { return VerbHandler.handleVerb(context, 'lick', `Try tying what you'd like to lick, for example 'Lick the chair'.`, `You\'ve licked that before when you were puppy and don\'t want to repeat the experience.`); } handlePotty(context) { context.addText('You don\'t need to go potty right now'); return false; } handleRestart(context) { context.restart(); return true; } getHandler(verb) { switch (verb) { case 'eat': case 'chew': return this.handleEat.bind(this); case 'push': case 'open': return this.handlePush.bind(this); case 'look': case 'examine': case 'inspect': case 'peruse': return this.handleLook.bind(this); case 'listen': return this.handleListen.bind(this); case 'smell': case 'sniff': return this.handleSmell.bind(this); case 'taste': case 'lick': return this.handleTaste.bind(this); case 'bark': case 'growl': case 'howl': case 'yell': case 'talk': case 'yip': case 'arf': case 'roo': return this.handleBark.bind(this); case 'think': case 'remember': case 'consider': return this.handleThinkAbout.bind(this); case 'go': case 'walk': case 'run': case 'jump': case 'climb': return this.handleGo.bind(this); case 'get': case 'take': return this.handleGet.bind(this); case 'pee': case 'poop': case 'poo': case 'shit': case 'piss': case 'urinate': case 'crap': case 'potty': return this.handlePotty.bind(this); case 'save': return undefined; case 'load': case 'restore': return undefined; case 'restart': return this.handleRestart.bind(this); case 'debug': return this.handleDebug.bind(this); default: return undefined; } } } exports.VerbHandler = VerbHandler;