Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Game - create MainRoom to hold verb logic
  • Loading branch information
bobbymcr committed Dec 2, 2018
1 parent 6a44a27 commit 44de275
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
28 changes: 2 additions & 26 deletions sample/src/Game.cs
Expand Up @@ -24,8 +24,9 @@ public void Run()
{ {
using (CancellationTokenSource cts = new CancellationTokenSource()) using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus, this.words)) 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); this.console.Run(cts.Token);
} }
} }
Expand All @@ -38,30 +39,5 @@ private static Words InitializeWords()
w.Add(Verb.Take, "get"); w.Add(Verb.Take, "get");
return w; 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.";
}
}
} }
} }
26 changes: 26 additions & 0 deletions sample/src/MainRoom.cs
@@ -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.