Permalink
Browse files

Game - create MainRoom to hold verb logic

  • Loading branch information...
bobbymcr committed Dec 2, 2018
1 parent 6a44a27 commit 44de275c6efe6289ee1086b284a9935ee31574af
Showing with 28 additions and 26 deletions.
  1. +2 −26 sample/src/Game.cs
  2. +26 −0 sample/src/MainRoom.cs
@@ -24,8 +24,9 @@ public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus, this.words))
using (this.bus.Subscribe<SentenceMessage>(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.";
}
}
}
}
@@ -0,0 +1,26 @@
// <copyright file="MainRoom.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

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."));
}
}
}

0 comments on commit 44de275

Please sign in to comment.