Engine:
- Changed the IGameCommand Interface constructor for the Execute method. - Updated all of the game commands to make use of the new Execute Method Constructor requirements set by the updated interface. - Look command now returns a description of the players current Room. - Walk command now supports moving players from one Room to another. Use 'Walk Direction' where Direction equals the direction you want to travel (Example: 'Walk North") - TravelDirections.GetTravelDirectionValue now checks the supplied direction value in a case-insensitive manor. - Add a new CommandEngine that handles the commands inputed from the user. - Modified CommandResult to return an array of objects rather than a single object. Runtime: - Now scans the supplied collection of objects returned to the runtime after executing a game command, and adjusts the runtime components as needed, including printing information to the console. - Now displays various warnings during startup to let the user know if certain content hasn't been set within the ProjectInformation yet. - Now executes the 'Look' command on startup to display the users current location. - Fully supports the 'Look' and 'Walk' commands.
This commit is contained in:
parent
efc49e35ce
commit
79f6d36083
10 changed files with 334 additions and 24 deletions
|
@ -83,8 +83,10 @@
|
|||
<Compile Include="MudEngine\Characters\NPC\NPCHostile.cs" />
|
||||
<Compile Include="MudEngine\Characters\NPC\NPCFriendly.cs" />
|
||||
<Compile Include="MudEngine\FileSystem\SaveDataTypes.cs" />
|
||||
<Compile Include="MudEngine\GameCommands\CommandEngine.cs" />
|
||||
<Compile Include="MudEngine\GameCommands\CommandGMTeleport.cs" />
|
||||
<Compile Include="MudEngine\GameCommands\CommandLook.cs" />
|
||||
<Compile Include="MudEngine\GameCommands\CommandResults.cs" />
|
||||
<Compile Include="MudEngine\GameCommands\CommandWalk.cs" />
|
||||
<Compile Include="MudEngine\GameManagement\GameScript.cs" />
|
||||
<Compile Include="MudEngine\GameObjects\Bag.cs" />
|
||||
|
@ -97,7 +99,7 @@
|
|||
<Compile Include="MudEngine\GameObjects\Environment\TravelDirections.cs" />
|
||||
<Compile Include="MudEngine\GameObjects\Environment\Zone.cs" />
|
||||
<Compile Include="MudEngine\GameObjects\Items\BaseItem.cs" />
|
||||
<Compile Include="MudEngine\Interfaces\ICommand.cs" />
|
||||
<Compile Include="MudEngine\Interfaces\IGameCommand.cs" />
|
||||
<Compile Include="MudEngine\Interfaces\IFileIO.cs" />
|
||||
<Compile Include="MudEngine\Interfaces\IGameObject.cs" />
|
||||
<Compile Include="MudEngine\Interfaces\IPlayer.cs" />
|
||||
|
|
122
Mud Designer/MudEngine/GameCommands/CommandEngine.cs
Normal file
122
Mud Designer/MudEngine/GameCommands/CommandEngine.cs
Normal file
|
@ -0,0 +1,122 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using MudDesigner.MudEngine.Interfaces;
|
||||
using MudDesigner.MudEngine.Characters;
|
||||
using MudDesigner.MudEngine.GameManagement;
|
||||
using MudDesigner.MudEngine.GameObjects.Environment;
|
||||
|
||||
namespace MudDesigner.MudEngine.GameCommands
|
||||
{
|
||||
public class CommandEngine
|
||||
{
|
||||
#region ====== Public Enumerators, Structures & Properties ======
|
||||
/// <summary>
|
||||
/// Gets or Sets a Dictionary list of available commands to use.
|
||||
/// </summary>
|
||||
static internal Dictionary<string, IGameCommand> Commands { get; set; }
|
||||
|
||||
public List<string> GetCommands
|
||||
{
|
||||
get
|
||||
{
|
||||
List<string> temp = new List<string>();
|
||||
foreach (string name in Commands.Keys)
|
||||
{
|
||||
temp.Add(name);
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ====== Public Methods ======
|
||||
public bool GetCommand(string Name)
|
||||
{
|
||||
if (Commands.ContainsKey(Name.ToLower()))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Executes the specified command name if it exists in the Commands Dictionary.
|
||||
/// </summary>
|
||||
/// <param name="Name"></param>
|
||||
/// <param name="Parameter"></param>
|
||||
/// <returns></returns>
|
||||
public static CommandResults ExecuteCommand(string Name, BaseCharacter player, ProjectInformation project, Room room, string command)
|
||||
{
|
||||
Name = Name.Insert(0, "Command");
|
||||
foreach (string key in Commands.Keys)
|
||||
{
|
||||
if (Name.ToLower().Contains(key.ToLower()))
|
||||
{
|
||||
return Commands[key.ToLower()].Execute(player, project, room, command);
|
||||
}
|
||||
}
|
||||
|
||||
return new CommandResults();
|
||||
}
|
||||
/// <summary>
|
||||
/// Dynamically loads the specified library into memory and stores all of the
|
||||
/// classess inheriting from MudCreator.InputCommands.ICommand into the CommandEngines
|
||||
/// commands dictionary for use with the project
|
||||
/// </summary>
|
||||
/// <param name="CommandLibrary"></param>
|
||||
public static void LoadAllCommands()
|
||||
{
|
||||
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
|
||||
Commands = new Dictionary<string, IGameCommand>();
|
||||
foreach (Type t in assembly.GetTypes())
|
||||
{
|
||||
if (t.GetInterface(typeof(IGameCommand).FullName) != null)
|
||||
{
|
||||
//Use activator to create an instance
|
||||
IGameCommand command = (IGameCommand)Activator.CreateInstance(t);
|
||||
|
||||
if (command != null)
|
||||
{
|
||||
if (command.Name == null)
|
||||
command.Name = t.Name.ToLower();
|
||||
else //Make sure the command is always in lower case.
|
||||
command.Name = command.Name.ToLower();
|
||||
|
||||
//Add the command to the commands list if it does not already exist
|
||||
if (Commands.ContainsKey(command.Name))
|
||||
{
|
||||
//Command exists, check if the command is set to override existing commands or not
|
||||
if (command.Override)
|
||||
{
|
||||
Commands[command.Name] = command;
|
||||
}
|
||||
}
|
||||
//Command does not exist, add it to the commands list
|
||||
else
|
||||
Commands.Add(command.Name, command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetCommand(object Parameter)
|
||||
{
|
||||
List<object> objectList = (List<object>)Parameter;
|
||||
|
||||
foreach (object obj in objectList)
|
||||
{
|
||||
if (obj is string)
|
||||
return (string)obj;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -4,7 +4,10 @@ using System.Linq;
|
|||
using System.Text;
|
||||
|
||||
using MudDesigner.MudEngine.Interfaces;
|
||||
using MudDesigner.MudEngine.Characters;
|
||||
using MudDesigner.MudEngine.Characters.Controlled;
|
||||
using MudDesigner.MudEngine.GameManagement;
|
||||
using MudDesigner.MudEngine.GameObjects.Environment;
|
||||
|
||||
namespace MudDesigner.MudEngine.GameCommands
|
||||
{
|
||||
|
@ -18,20 +21,16 @@ namespace MudDesigner.MudEngine.GameCommands
|
|||
Override = true;
|
||||
}
|
||||
|
||||
public object Execute(params object[] parameters)
|
||||
public CommandResults Execute(BaseCharacter player, ProjectInformation project, Room room, string command)
|
||||
{
|
||||
PlayerGM playerGM = new PlayerGM();
|
||||
bool foundGM = false;
|
||||
|
||||
foreach (object obj in parameters)
|
||||
{
|
||||
if (obj is PlayerGM)
|
||||
if (player is PlayerGM)
|
||||
{
|
||||
foundGM = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!foundGM)
|
||||
return null;
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using MudDesigner.MudEngine.Characters;
|
||||
using MudDesigner.MudEngine.Characters.Controlled;
|
||||
using MudDesigner.MudEngine.FileSystem;
|
||||
using MudDesigner.MudEngine.GameCommands;
|
||||
using MudDesigner.MudEngine.GameManagement;
|
||||
|
@ -17,20 +19,16 @@ namespace MudDesigner.MudEngine.GameCommands
|
|||
public string Name { get; set; }
|
||||
public bool Override { get; set; }
|
||||
|
||||
public object Execute(params object[] parameters)
|
||||
public CommandResults Execute(BaseCharacter player, ProjectInformation project, Room room, string command)
|
||||
{
|
||||
Room room = new Room() ;
|
||||
StringBuilder desc = new StringBuilder();
|
||||
|
||||
foreach (object obj in parameters)
|
||||
if (room == null)
|
||||
{
|
||||
if (obj is Room)
|
||||
room = (Room)obj;
|
||||
return new CommandResults("Not within a created Room.");
|
||||
}
|
||||
|
||||
if (room == null)
|
||||
return null;
|
||||
|
||||
desc.AppendLine(room.Description);
|
||||
foreach (Door door in room.Doorways)
|
||||
{
|
||||
if (door.TravelDirection != MudDesigner.MudEngine.GameObjects.AvailableTravelDirections.Down && door.TravelDirection != MudDesigner.MudEngine.GameObjects.AvailableTravelDirections.Up)
|
||||
|
@ -39,7 +37,7 @@ namespace MudDesigner.MudEngine.GameCommands
|
|||
}
|
||||
}
|
||||
|
||||
return desc.ToString();
|
||||
return new CommandResults(desc.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
32
Mud Designer/MudEngine/GameCommands/CommandResults.cs
Normal file
32
Mud Designer/MudEngine/GameCommands/CommandResults.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace MudDesigner.MudEngine.GameCommands
|
||||
{
|
||||
public class CommandResults
|
||||
{
|
||||
/// <summary>
|
||||
/// Result of the command.
|
||||
/// </summary>
|
||||
public object[] Result { get; set; }
|
||||
|
||||
public CommandResults()
|
||||
{
|
||||
}
|
||||
|
||||
public CommandResults(object[] Result)
|
||||
{
|
||||
this.Result = Result;
|
||||
}
|
||||
|
||||
public CommandResults(string message)
|
||||
{
|
||||
this.Result = new object[] { message };
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,8 +2,14 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
using MudDesigner.MudEngine.Interfaces;
|
||||
using MudDesigner.MudEngine.Characters;
|
||||
using MudDesigner.MudEngine.GameManagement;
|
||||
using MudDesigner.MudEngine.GameObjects.Environment;
|
||||
using MudDesigner.MudEngine.GameObjects;
|
||||
using MudDesigner.MudEngine.FileSystem;
|
||||
|
||||
namespace MudDesigner.MudEngine.GameCommands
|
||||
{
|
||||
|
@ -12,9 +18,50 @@ namespace MudDesigner.MudEngine.GameCommands
|
|||
public string Name { get; set; }
|
||||
public bool Override { get; set; }
|
||||
|
||||
public object Execute(params object[] parameters)
|
||||
public CommandResults Execute(BaseCharacter player, ProjectInformation project, Room room, string command)
|
||||
{
|
||||
return null;
|
||||
string[] words = command.Split(' ');
|
||||
List<string> directions = new List<string>();
|
||||
|
||||
if (words.Length == 1)
|
||||
return new CommandResults("No direction supplied");
|
||||
else
|
||||
{
|
||||
foreach (Door door in room.Doorways)
|
||||
{
|
||||
AvailableTravelDirections direction = TravelDirections.GetTravelDirectionValue(words[1]);
|
||||
|
||||
if (door.TravelDirection == direction)
|
||||
{
|
||||
string zonePath = "";
|
||||
string roomPath = "";
|
||||
string roomFilename = "";
|
||||
|
||||
if (room.Realm == "No Realm Associated.")
|
||||
{
|
||||
zonePath = FileManager.GetDataPath(SaveDataTypes.Zones);
|
||||
zonePath = Path.Combine(zonePath, room.Zone);
|
||||
}
|
||||
else
|
||||
{
|
||||
zonePath = FileManager.GetDataPath(room.Realm, room.Zone);
|
||||
}
|
||||
|
||||
roomPath = Path.Combine(zonePath, "Rooms");
|
||||
roomFilename = Path.Combine(roomPath, door.ConnectedRoom + ".room");
|
||||
room = (Room)room.Load(roomFilename);
|
||||
CommandResults cmd = CommandEngine.ExecuteCommand("Look", player, project, room, "Look");
|
||||
string lookValue = "";
|
||||
|
||||
if (cmd.Result.Length != 0)
|
||||
lookValue = cmd.Result[0].ToString();
|
||||
|
||||
return new CommandResults(new object[] { lookValue, room });
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return new CommandResults("Unable to travel in that direction.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace MudDesigner.MudEngine.GameObjects
|
|||
{
|
||||
string displayName = Enum.GetName(typeof(AvailableTravelDirections), value);
|
||||
|
||||
if (displayName == Direction)
|
||||
if (displayName.ToLower() == Direction.ToLower())
|
||||
return (AvailableTravelDirections)Enum.Parse(typeof(AvailableTravelDirections), displayName);
|
||||
}
|
||||
|
||||
|
|
22
Mud Designer/MudEngine/Interfaces/IGameCommand.cs
Normal file
22
Mud Designer/MudEngine/Interfaces/IGameCommand.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using MudDesigner.MudEngine.GameCommands;
|
||||
using MudDesigner.MudEngine.Characters;
|
||||
using MudDesigner.MudEngine.GameManagement;
|
||||
using MudDesigner.MudEngine.GameObjects.Environment;
|
||||
|
||||
namespace MudDesigner.MudEngine.Interfaces
|
||||
{
|
||||
public interface IGameCommand
|
||||
{
|
||||
//Name of the command
|
||||
string Name { get; set; }
|
||||
//Used to override commands with the same name
|
||||
bool Override { get; set; }
|
||||
//Executes the command.
|
||||
CommandResults Execute(BaseCharacter player, ProjectInformation project, Room room, string command);
|
||||
}
|
||||
}
|
2
Mud Designer/Runtime.Designer.cs
generated
2
Mud Designer/Runtime.Designer.cs
generated
|
@ -71,7 +71,7 @@
|
|||
this.txtCommand.Name = "txtCommand";
|
||||
this.txtCommand.Size = new System.Drawing.Size(693, 20);
|
||||
this.txtCommand.TabIndex = 1;
|
||||
this.txtCommand.TextChanged += new System.EventHandler(this.txtCommand_TextChanged);
|
||||
this.txtCommand.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtCommand_KeyDown);
|
||||
//
|
||||
// Runtime
|
||||
//
|
||||
|
|
|
@ -36,16 +36,23 @@ namespace MudDesigner
|
|||
_Room = new Room();
|
||||
}
|
||||
|
||||
public void Print(string message)
|
||||
{
|
||||
txtConsole.Text += message + "\n";
|
||||
txtConsole.Select(txtConsole.Text.Length, 0);
|
||||
}
|
||||
|
||||
private void Runtime_Load(object sender, EventArgs e)
|
||||
{
|
||||
Print("Loading project information...");
|
||||
_Project = (ProjectInformation)_Project.Load(FileManager.GetDataPath(SaveDataTypes.Root));
|
||||
if (_Project.InitialLocation.Zone == "")
|
||||
{
|
||||
MessageBox.Show("No Initial Zone was defined within the Project Information. Please associated a Zone to the Projects Initial Zone setting in order to launch the game.",
|
||||
"Mud Designer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
Print("No Initial Zone was defined within the Project Information. Please associated a Zone to the Projects Initial Zone setting in order to launch the game.");
|
||||
Application.Exit();
|
||||
}
|
||||
|
||||
Print("Loading environment...");
|
||||
string filename = FileManager.GetDataPath(SaveDataTypes.Root);
|
||||
if (_Project.InitialLocation.Realm != "No Realm Associated.")
|
||||
{
|
||||
|
@ -59,13 +66,94 @@ namespace MudDesigner
|
|||
filename = Path.Combine(filename, _Project.InitialLocation.Room);
|
||||
filename += ".room";
|
||||
_Room = (Room)_Room.Load(filename);
|
||||
|
||||
Print("Prepping test player...");
|
||||
_Player.CurrentRoom = _Room;
|
||||
_Player.OnTravel(AvailableTravelDirections.North);
|
||||
|
||||
Print("Loading Game Commands...");
|
||||
CommandEngine.LoadAllCommands();
|
||||
|
||||
Print("Startup Complete.");
|
||||
Print(""); //blank line
|
||||
txtCommand.Select();
|
||||
|
||||
if (string.IsNullOrEmpty(_Project.CompanyName))
|
||||
Print("No company name defined for the project!");
|
||||
else
|
||||
Print("Created by " + _Project.CompanyName);
|
||||
|
||||
if (string.IsNullOrEmpty(_Project.Website))
|
||||
Print("No website defined for the project!");
|
||||
else
|
||||
Print("Visit us at " + _Project.Website);
|
||||
|
||||
if (string.IsNullOrEmpty(_Project.GameTitle))
|
||||
Print("No Game Title defiend for the project!");
|
||||
else
|
||||
Print(_Project.GameTitle);
|
||||
|
||||
if (string.IsNullOrEmpty(_Project.Version))
|
||||
Print("Game Version was not specified.");
|
||||
else
|
||||
Print(_Project.Version);
|
||||
|
||||
if (string.IsNullOrEmpty(_Project.Story))
|
||||
Print("The games startup story has not been created yet!");
|
||||
else
|
||||
Print(_Project.Story);
|
||||
|
||||
Print("");//blank line
|
||||
CommandResults result = CommandEngine.ExecuteCommand("Look", _Player, _Project, _Room, "Look");
|
||||
if (result.Result.Length != 0)
|
||||
Print(result.Result[0].ToString());
|
||||
}
|
||||
|
||||
private void txtCommand_TextChanged(object sender, EventArgs e)
|
||||
private void txtCommand_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter)
|
||||
{
|
||||
string[] words = txtCommand.Text.Split(' ');
|
||||
string firstWord = "";
|
||||
if (words.Length == 0)
|
||||
return;
|
||||
firstWord = words[0];
|
||||
List<object> arguments = new List<object>();
|
||||
foreach (string word in words)
|
||||
{
|
||||
if (word == firstWord)
|
||||
continue;
|
||||
arguments.Add(word);
|
||||
}
|
||||
arguments.Add(_Room);
|
||||
arguments.Add(_Player);
|
||||
arguments.Add(_Project);
|
||||
CommandResults result = CommandEngine.ExecuteCommand(txtCommand.Text, _Player, _Project, _Room, txtCommand.Text);
|
||||
|
||||
if (result.Result == null)
|
||||
return;
|
||||
|
||||
foreach (object obj in result.Result)
|
||||
{
|
||||
switch (obj.GetType().Name.ToLower())
|
||||
{
|
||||
case "string":
|
||||
Print(obj.ToString());
|
||||
break;
|
||||
case "room":
|
||||
_Room = (Room)obj;
|
||||
break;
|
||||
case "projectinformation":
|
||||
_Project = (ProjectInformation)obj;
|
||||
break;
|
||||
case "playerbasic":
|
||||
_Player = (PlayerBasic)obj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
txtCommand.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue