My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CreatingNewUserCommands  
This page describes how to create a new user command in Snookiepoof
Phase-Implementation
Updated Jun 25, 2009 by torbjorn...@gmail.com

Introduction

UserCommands represent a central concept in Snookiepoof. You can extend the game engine by adding new commands, which represent everything a user can do in the game. This page describes the normal steps you go through to create a new UserCommand.

Remember to start by creating tests first. Tests for UserCommands are created in project Test.Snookiepoof.Core.

Creating a new concrete UserCommand

In the UserCommands foler in the Snookiepoof.Core project you'll find all the existing commands. Add a new class which inherits from the UserCommand interface.

  • Any dependencies / resources the command need should be injected through the constructor.
  • The Execute method will be called on the command when the user wants to perform the action.
  • The Execute method should not fail - it should always return a UserCommandResult. If the command was not able to perform the requested action, it should return a result with Success set to false. The Text property of the result should be set to a text that can be displayed to the user, telling him what happened. In many situations the actions main purpose is to provide the user with some information, and this is provided through that same Text property.

Using the new command in a text based game

In order for the user to run the UserCommand you just created from a text based game, you need to create a UserCommandTextParser for that command. You'll do this in Snookiepoof.Text, under the TextParsing/SpecificTextParsers folder. Remember to write tests for the parser.

  • Create a new class that inherits from IUserCommandTextParser.
  • Implement the GetCommand method so that it will return an instance of your command given some text arguments.
  • Add UserCommandKeywordAttributes to the parser to tell the game which text commands will trigger the parser (and eventually the command itself).
  • Run all the tests in Test.Snookiepoof.Text. They will make sure your text parser complies with all the rules (no duplicate keywords, default constructor with no arguments, etc.)

Here is a sample text parser:

[UserCommandKeyword("look")]
[UserCommandKeyword("l")]
[UserCommandKeyword("see")]
public class LookCommandTextParser : IUserCommandTextParser
{
    public UserCommand GetCommand(FirstPerson person, string[] commandArguments)
    {
        return new LookCommand(person, ParserHelper.GetDirectionFromArguments(commandArguments));
    }
}

Your new command should now be ready to be used in the Console game.


Sign in to add a comment
Powered by Google Project Hosting