From 44de275c6efe6289ee1086b284a9935ee31574af Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Sun, 2 Dec 2018 07:56:58 -0800 Subject: [PATCH] Game - create MainRoom to hold verb logic --- sample/src/Game.cs | 28 ++-------------------------- sample/src/MainRoom.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 sample/src/MainRoom.cs diff --git a/sample/src/Game.cs b/sample/src/Game.cs index 067157e..470fa25 100644 --- a/sample/src/Game.cs +++ b/sample/src/Game.cs @@ -24,8 +24,9 @@ public void Run() { using (CancellationTokenSource cts = new CancellationTokenSource()) using (new SentenceParser(this.bus, this.words)) - using (this.bus.Subscribe(m => this.ProcessSentence(cts, m))) { + Room room = new MainRoom(this.bus, cts); + room.Enter(); this.console.Run(cts.Token); } } @@ -38,30 +39,5 @@ private static Words InitializeWords() w.Add(Verb.Take, "get"); return w; } - - private void ProcessSentence(CancellationTokenSource cts, SentenceMessage sentence) - { - string output = this.Process(cts, sentence.Verb, sentence.Noun); - if (output != null) - { - this.bus.Send(new OutputMessage(output)); - } - } - - private string Process(CancellationTokenSource cts, Word verb, Word noun) - { - switch (verb.Primary) - { - case Verb.Greet: - return "You say, \"Hello,\" to no one in particular. No one answers."; - case Verb.Take: - return "There is no " + noun.Actual.ToLowerInvariant() + " here."; - case Verb.Quit: - cts.Cancel(); - return null; - default: - return "I don't know what '" + verb + "' means."; - } - } } } diff --git a/sample/src/MainRoom.cs b/sample/src/MainRoom.cs new file mode 100644 index 0000000..4799233 --- /dev/null +++ b/sample/src/MainRoom.cs @@ -0,0 +1,26 @@ +// +// Copyright (c) Brian Rogers. All rights reserved. +// + +namespace Adventure.Sample +{ + using System.Threading; + + internal sealed class MainRoom : Room + { + private readonly CancellationTokenSource cts; + + public MainRoom(MessageBus bus, CancellationTokenSource cts) + : base(bus) + { + this.cts = cts; + } + + protected override void EnterCore() + { + this.Register(Verb.Greet, (_, __) => this.Output("You say, \"Hello,\" to no one in particular. No one answers.")); + this.Register(Verb.Quit, (_, n) => this.cts.Cancel()); + this.Register(Verb.Take, (_, n) => this.Output("There is no " + n.Actual.ToLowerInvariant() + " here.")); + } + } +}