Basic Realm->Zone->Room combination is now created during World.Initialize(). This will be replaced with loading XML instead of hard-coding.

Newly created characters are assigned to the new World.StartLocation.
Rooms can now be connected.
Realms and Zones can create Zones and Rooms accordingly
Force moving of a character is now supported.  Walking has yet to be implemented.
This commit is contained in:
Scionwest_cp 2012-03-04 16:56:04 -08:00
parent a3eb1b5fad
commit c40d32e7ae
12 changed files with 302 additions and 21 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Reflection; using System.Reflection;
using MudEngine.Core;
using MudEngine.Core.Interfaces; using MudEngine.Core.Interfaces;
using MudEngine.Game.Characters; using MudEngine.Game.Characters;

View file

@ -53,14 +53,6 @@ namespace MudEngine.Core
} }
public static void WriteLine(String message, Importance importance) public static void WriteLine(String message, Importance importance)
{
}
/// <summary>
/// Writes a single line to the engine log file.
/// </summary>
/// <param name="message"></param>
public static void WriteLine(String message)
{ {
//Only write to log if enabled. //Only write to log if enabled.
if (!Enabled) if (!Enabled)
@ -98,6 +90,16 @@ namespace MudEngine.Core
} }
} }
/// <summary>
/// Writes a single line to the engine log file.
/// </summary>
/// <param name="message"></param>
public static void WriteLine(String message)
{
//Just output as typical informational stuff.
Logger.WriteLine(message, Importance.Information);
}
/// <summary> /// <summary>
/// Returns an array of messages that have been queued in the log cache. /// Returns an array of messages that have been queued in the log cache.
/// </summary> /// </summary>

View file

@ -22,6 +22,7 @@ namespace MudEngine.Game.Characters
/// </summary> /// </summary>
public class StandardCharacter : BaseScript, INetworked, ISavable, IGameComponent public class StandardCharacter : BaseScript, INetworked, ISavable, IGameComponent
{ {
#region Properties
/// <summary> /// <summary>
/// Gets a reference to the currently active game. /// Gets a reference to the currently active game.
/// </summary> /// </summary>
@ -89,6 +90,9 @@ namespace MudEngine.Game.Characters
public Room CurrentRoom { get; private set; } public Room CurrentRoom { get; private set; }
#endregion
#region Constructors
public StandardCharacter(StandardGame game, String name, String description) public StandardCharacter(StandardGame game, String name, String description)
: base(game, name, description) : base(game, name, description)
{ {
@ -101,6 +105,9 @@ namespace MudEngine.Game.Characters
this.OnConnectEvent += new OnConnectHandler(OnConnect); this.OnConnectEvent += new OnConnectHandler(OnConnect);
this.OnDisconnectEvent += new OnDisconnectHandler(OnDisconnect); this.OnDisconnectEvent += new OnDisconnectHandler(OnDisconnect);
this.OnLoginEvent += new OnLoginHandler(OnLogin); this.OnLoginEvent += new OnLoginHandler(OnLogin);
this.OnWalkEvent += new OnWalkHandler(OnWalk);
this.OnEnterEvent += new OnEnterHandler(OnEnter);
this.OnLeaveEvent += new OnLeaveHandler(OnLeave);
} }
public StandardCharacter(StandardGame game, String name, String description, Socket connection) public StandardCharacter(StandardGame game, String name, String description, Socket connection)
@ -113,7 +120,9 @@ namespace MudEngine.Game.Characters
this._Writer.AutoFlush = true; //Flushes the stream automatically. this._Writer.AutoFlush = true; //Flushes the stream automatically.
} }
#endregion
#region Inherited Methods
public virtual void Initialize() public virtual void Initialize()
{ {
//throw new NotImplementedException(); //throw new NotImplementedException();
@ -162,7 +171,9 @@ namespace MudEngine.Game.Characters
this.Role = GetRole(role); this.Role = GetRole(role);
} }
#endregion
#region Networking Methods
/// <summary> /// <summary>
/// Executes the specified command if it exists in the Command library. /// Executes the specified command if it exists in the Command library.
/// </summary> /// </summary>
@ -211,7 +222,6 @@ namespace MudEngine.Game.Characters
public void Disconnect() public void Disconnect()
{ {
Console.WriteLine("Disconnecting...");
this.Save(this.Filename); this.Save(this.Filename);
//Purge all of this characters commands. //Purge all of this characters commands.
@ -290,7 +300,9 @@ namespace MudEngine.Game.Characters
} }
} }
} }
#endregion
#region Event Methods
public delegate void OnConnectHandler(); public delegate void OnConnectHandler();
public event OnConnectHandler OnConnectEvent; public event OnConnectHandler OnConnectEvent;
public void OnConnect() public void OnConnect()
@ -326,7 +338,6 @@ namespace MudEngine.Game.Characters
public event OnDisconnectHandler OnDisconnectEvent; public event OnDisconnectHandler OnDisconnectEvent;
public void OnDisconnect() public void OnDisconnect()
{ {
Console.WriteLine("Disconnect Complete.");
} }
public delegate void OnLoginHandler(); public delegate void OnLoginHandler();
@ -343,6 +354,44 @@ namespace MudEngine.Game.Characters
} }
} }
public delegate void OnWalkHandler(AvailableTravelDirections direction);
public event OnWalkHandler OnWalkEvent;
public void OnWalk(AvailableTravelDirections direction)
{
if (this.CurrentRoom.DoorwayExists(direction))
{
this.CurrentRoom = this.CurrentRoom.GetDoorway(direction).ArrivalRoom;
}
else
return;
}
public delegate void OnLeaveHandler(Room departingRoom, Room targetRoom, AvailableTravelDirections direction);
public event OnLeaveHandler OnLeaveEvent;
public void OnLeave(Room departingRoom, Room targetRoom, AvailableTravelDirections direction)
{
//This character was forced moved so we have no direction
if (direction == AvailableTravelDirections.None)
departingRoom.SendMessageToOccupants(this.Name + " has left the room.", this);
else
departingRoom.SendMessageToOccupants(this.Name + " has left the room heading " + direction.ToString(), this);
}
public delegate void OnEnterHandler(Room departingRoom, Room targetRoom, AvailableTravelDirections direction);
public event OnEnterHandler OnEnterEvent;
public void OnEnter(Room departingRoom, Room targetRoom, AvailableTravelDirections direction)
{
//This character was forced moved so we have no direction
if (direction == AvailableTravelDirections.None)
targetRoom.SendMessageToOccupants(this.Name + " has entered the room.");
else
targetRoom.SendMessageToOccupants(this.Name + " has entered the room from the " + direction.ToString(), this);
this.ExecuteCommand("Look");
}
#endregion
#region Character Methods
public void SetRole(StandardCharacter adminCharacter, StandardCharacter subordinateCharacter, CharacterRoles role) public void SetRole(StandardCharacter adminCharacter, StandardCharacter subordinateCharacter, CharacterRoles role)
{ {
//Check to make sure the admin character is truly a admin //Check to make sure the admin character is truly a admin
@ -392,6 +441,33 @@ namespace MudEngine.Game.Characters
} }
return CharacterRoles.Player; return CharacterRoles.Player;
} }
#endregion
#region World Methods
public virtual void Walk(AvailableTravelDirections direction)
{
Room r = this.CurrentRoom;
this.OnWalkEvent(direction);
//If we didn't go anywhere, do nothing.
if (this.CurrentRoom == r)
return;
OnLeaveEvent(r, this.CurrentRoom, direction);
OnEnterEvent(r, this.CurrentRoom, direction);
}
public virtual void Move(Room room)
{
if (this.CurrentRoom != null)
this.CurrentRoom.SendMessageToOccupants(this.Name + " has left the room.", this);
this.CurrentRoom = room;
this.CurrentRoom.AddOccupant(this);
this.CurrentRoom.SendMessageToOccupants(this.Name + " has entered the room.", this);
}
#endregion
private Socket _Connection; private Socket _Connection;
private StreamReader _Reader; private StreamReader _Reader;

View file

@ -21,9 +21,18 @@ namespace MudEngine.Game.Environment
public Room DepartureRoom { get; private set; } public Room DepartureRoom { get; private set; }
public Doorway(StandardGame game) public Doorway(Room arrival, Room departure, AvailableTravelDirections direction)
{ {
this.TravelDirection = direction;
this.ArrivalRoom = arrival;
this.DepartureRoom = departure;
this.LevelRequirement = 0; this.LevelRequirement = 0;
} }
public override string ToString()
{
return "{" + this.GetType().Name + "}: " + this.DepartureRoom.Name + "->" + this.TravelDirection.ToString() + "->" + this.ArrivalRoom.Name;
}
} }
} }

View file

@ -50,10 +50,12 @@ namespace MudEngine.Game.Environment
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void CreateZone(String name, String description) public Zone CreateZone(String name, String description)
{ {
Zone zone = new Zone(this.Game, name, description); Zone zone = new Zone(this.Game, name, description);
this._ZoneCollection.Add(zone); this._ZoneCollection.Add(zone);
zone.Realm = this;
return zone;
} }
public Zone GetZone(String name) public Zone GetZone(String name)

View file

@ -12,10 +12,18 @@ namespace MudEngine.Game.Environment
{ {
public class Room : BaseScript, IUpdatable public class Room : BaseScript, IUpdatable
{ {
public Room(StandardGame game, String name, String description) public Zone Zone { get; private set; }
public Boolean Safe { get; set; }
public List<StandardCharacter> Occupants { get; private set; }
public Room(StandardGame game, String name, String description, Zone zone)
: base(game, name, description) : base(game, name, description)
{ {
this._Doors = new List<Doorway>(); this._Doors = new List<Doorway>();
this.Occupants = new List<StandardCharacter>();
this.Zone = zone;
} }
public void Update() public void Update()
@ -23,11 +31,128 @@ namespace MudEngine.Game.Environment
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <summary>
///
/// </summary>
/// <param name="departureDirection"></param>
/// <param name="connectedRoom"></param>
/// <returns></returns>
public Boolean LinkRooms(AvailableTravelDirections departureDirection, Room connectedRoom)
{
foreach (Doorway door in this._Doors)
{
if (door.TravelDirection == departureDirection)
{
return false;
}
}
//Create a new doorway and link it to this room
Doorway d = new Doorway(connectedRoom, this, departureDirection);
this._Doors.Add(d);
//Link the connected room
Boolean successful = connectedRoom.FinishLink(d);
if (successful)
return true;
else
{
this._Doors.Remove(d);
return false;
}
}
protected Boolean FinishLink(Doorway door)
{
foreach (Doorway d in this._Doors)
{
if (d.TravelDirection == door.TravelDirection)
{
return false;
}
}
Doorway newDoor = new Doorway(door.DepartureRoom, this, TravelDirections.GetReverseDirection(door.TravelDirection));
this._Doors.Add(newDoor);
return true;
}
public String[] GetDescription() public String[] GetDescription()
{ {
return new List<String>().ToArray(); return new List<String>().ToArray();
} }
/// <summary>
/// Returns a array of Doorways that are currently associated with this Room.
/// </summary>
/// <returns></returns>
public Doorway[] GetDoorways()
{
return this._Doors.ToArray();
}
public Doorway GetDoorway(AvailableTravelDirections direction)
{
foreach (Doorway door in this._Doors)
{
if (door.TravelDirection == direction)
return door;
}
//No direction that matched was found
return null;
}
/// <summary>
/// Checks if the specified travel direction has a doorway within this Room.
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public Boolean DoorwayExists(AvailableTravelDirections direction)
{
foreach (Doorway door in this._Doors)
{
if (door.TravelDirection == direction)
return true;
}
//No direction that matched was found
return false;
}
public void SendMessageToOccupants(String message)
{
foreach (StandardCharacter character in this.Occupants)
{
character.SendMessage(message);
}
}
public void SendMessageToOccupants(String message, StandardCharacter exemptCharacter)
{
foreach (StandardCharacter character in this.Occupants)
{
if (character.Name != exemptCharacter.Name)
character.SendMessage(message);
}
}
public void AddOccupant(StandardCharacter character)
{
foreach (StandardCharacter c in this.Occupants)
{
if (character.Name == character.Name)
return;
}
this.Occupants.Add(character);
}
public override string ToString()
{
return "{" + this.GetType().Name + "}:" + this.Zone.Realm + "." + this.Zone.Name + "." + this.Name;
}
private List<Doorway> _Doors; private List<Doorway> _Doors;
} }
} }

View file

@ -6,6 +6,7 @@ using System.Text;
using MudEngine.Core.Interfaces; using MudEngine.Core.Interfaces;
using MudEngine.GameScripts; using MudEngine.GameScripts;
using MudEngine.Game.Characters; using MudEngine.Game.Characters;
using MudEngine.Core;
namespace MudEngine.Game.Environment namespace MudEngine.Game.Environment
{ {
@ -18,7 +19,7 @@ namespace MudEngine.Game.Environment
public Boolean Safe { get; set; } public Boolean Safe { get; set; }
public String Realm { get; private set; } public Realm Realm { get; set; }
public Zone(StandardGame game, String name, String description) : base(game, name, description) public Zone(StandardGame game, String name, String description) : base(game, name, description)
{ {
@ -45,9 +46,46 @@ namespace MudEngine.Game.Environment
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void CreateRoom(String name, String description) public Room CreateRoom(String name, String description)
{ {
Room room = new Room(this.Game, name, description, this);
foreach(Room r in this._RoomCollection)
{
if (r.Name == name)
{
Logger.WriteLine("An attempt to create a duplicate Room was stopped. Room '" + name + "' was not created within the Zone '" + this.Name + "'");
return null;
}
}
this._RoomCollection.Add(room);
return room;
}
public Boolean LinkRooms(String departingRoom, String arrivalRoom, AvailableTravelDirections direction)
{
Room depart, arrival;
depart = this.GetRoom(departingRoom);
arrival = this.GetRoom(arrivalRoom);
if (depart == null || arrivalRoom == null)
return false;
Boolean result = depart.LinkRooms(direction, arrival);
return result;
}
public Room GetRoom(String room)
{
foreach (Room r in this._RoomCollection)
{
if (r.Name == room)
return r;
}
return null;
} }
private List<Room> _RoomCollection; private List<Room> _RoomCollection;

View file

@ -9,6 +9,7 @@ using MudEngine.Networking;
using MudEngine.Core; using MudEngine.Core;
using MudEngine.Game.Characters; using MudEngine.Game.Characters;
using MudEngine.DAL; using MudEngine.DAL;
using MudEngine.Game.Environment;
namespace MudEngine.Game namespace MudEngine.Game
{ {
@ -141,7 +142,7 @@ namespace MudEngine.Game
CommandSystem.LoadCommands(); CommandSystem.LoadCommands();
//Load World //Load World
this.World.CreateRealm("Azeroth", "The realm of Azeroth is full of jungles and small villages"); this.World.Initialize();
//Start our server. //Start our server.
this.Server.Start(maxPlayers, maxQueueSize); this.Server.Start(maxPlayers, maxQueueSize);

View file

@ -4,19 +4,46 @@ using System.Linq;
using System.Text; using System.Text;
using MudEngine.Game.Environment; using MudEngine.Game.Environment;
using MudEngine.Core.Interfaces;
namespace MudEngine.Game namespace MudEngine.Game
{ {
public class World public class World : IGameComponent
{ {
/// <summary>
/// Gets a reference to the currently running game.
/// </summary>
public StandardGame Game { get; private set; } public StandardGame Game { get; private set; }
/// <summary>
/// Gets or Sets the starting location for new characters.
/// </summary>
public Room StartLocation { get; set; }
public World(StandardGame game) public World(StandardGame game)
{ {
this.Game = game; this.Game = game;
this._RealmCollection = new List<Realm>(); this._RealmCollection = new List<Realm>();
} }
public void Initialize()
{
Realm realm = new Realm(this.Game, "Azeroth", "");
Zone zone = realm.CreateZone("Bablo", "");
zone.CreateRoom("Bedroom", "");
zone.CreateRoom("Hallway", "");
zone.LinkRooms("Bedroom", "Hallway", AvailableTravelDirections.East);
this.StartLocation = zone.GetRoom("Bedroom");
}
public void Destroy()
{
throw new NotImplementedException();
}
public void CreateRealm(String name, String description) public void CreateRealm(String name, String description)
{ {
Realm r = new Realm(this.Game, name, description); Realm r = new Realm(this.Game, name, description);

View file

@ -87,9 +87,9 @@ namespace MudEngine.GameScripts
public override string ToString() public override string ToString()
{ {
if (String.IsNullOrEmpty(this.Name)) if (String.IsNullOrEmpty(this.Name))
return this.GetType().Name + " without Name"; return "{" + this.GetType().Name + "}:" + "Without Name";
else else
return this.Name; return "{" + this.GetType().Name + "}:" + this.Name;
} }
} }
} }

View file

@ -80,6 +80,8 @@ namespace MudEngine.GameScripts.Commands
} }
} }
character.Move(game.World.StartLocation);
//TODO: Create a class and setup Stats. //TODO: Create a class and setup Stats.
character.Save(character.Filename, false); character.Save(character.Filename, false);

View file

@ -25,8 +25,6 @@ namespace MudEngine.GameScripts.Commands
public bool Execute(string command, StandardCharacter character) public bool Execute(string command, StandardCharacter character)
{ {
return false; return false;
} }
} }