muddesigner/MudEngine/GameManagement/GameWorld.cs
Scionwest_cp ce910a5fd0 MudEngine:
- GameWorld.AddRealm() method replaced with GameWorld.AddObject(). It accepts any Type passed to it. You can now supply a Zone to it (provided the Zone.Realm property is set first) and the method will add the Zone into the appropriate Realm for you.
 - Began converting certain enumerated items to LINQ

MudGame:
 - Updated scripts to reflect the GameWorld changes.
2010-08-21 10:49:12 -07:00

255 lines
8.6 KiB
C#

//Microsoft .NET Framework
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
//MudEngine
using MudEngine.FileSystem;
using MudEngine.GameManagement;
using MudEngine.GameObjects;
using MudEngine.GameObjects.Characters;
using MudEngine.GameObjects.Environment;
using MudEngine.GameObjects.Items;
using MudEngine.Scripting;
namespace MudEngine.GameManagement
{
public enum ObjectTypes
{
Realm,
Zone,
Room,
Character,
Item,
Standard,
//Any
}
public class GameWorld
{
/// <summary>
/// Gets the collection of Objects that do not belong to any other Type of categories.
/// </summary>
public List<BaseObject> Objects { get; private set; }
/// <summary>
/// Gets the collection of Items that are available in the game world.
/// </summary>
public List<BaseItem> Items { get; private set; }
/// <summary>
/// Gets the collection of Characters (NPC/Monster) that are available in the Game world
/// </summary>
//TODO: This should be BaseAI
public List<BaseCharacter> Characters { get; private set; }
/// <summary>
/// Gets the collection of Realms currently available in the game world
/// </summary>
public List<Realm> RealmCollection { get; private set; }
private Game _Game;
public GameWorld(Game game)
{
_Game = game;
Objects = new List<BaseObject>();
Items = new List<BaseItem>();
Characters = new List<BaseCharacter>();
RealmCollection = new List<Realm>();
}
public void Start()
{
//See if we have an Initial Realm set
//TODO: Check for saved Realm files and load
Log.Write("Initializing World...");
foreach (Realm r in RealmCollection)
{
if (r.IsInitialRealm)
{
_Game.InitialRealm = r;
break;
}
}
//Check if any the initial room exists or not.
if ((_Game.InitialRealm == null) || (_Game.InitialRealm.InitialZone == null) || (_Game.InitialRealm.InitialZone.InitialRoom == null))
{
Log.Write("ERROR: No initial location defined. Game startup failed!");
Log.Write("Players will start in the Abyss. Each player will contain their own instance of this room.");
//return false;
}
else
Log.Write("Initial Location loaded-> " + _Game.InitialRealm.Name + "." + _Game.InitialRealm.InitialZone.Name + "." + _Game.InitialRealm.InitialZone.InitialRoom.Name);
}
public void Save()
{
//Save all of the Environments
for (Int32 x = 0; x <= RealmCollection.Count - 1; x++)
{
RealmCollection[x].Save(_Game.DataPaths.Environment);
}
}
public void Load()
{
String filename = _Game.GameTitle + ".ini";
//Restore initial realm
String realmFile = FileManager.GetData(filename, "InitialRealm");
String realmFolder = Path.GetFileNameWithoutExtension(realmFile);
String realmPath = Path.Combine(_Game.DataPaths.Environment, realmFolder, realmFile);
foreach (String realm in FileManager.GetCollectionData(filename, "RealmCollection"))
{
Realm r = new Realm(_Game);
r.Load(Path.Combine(realmPath, realm));
}
foreach (Realm r in RealmCollection)
{
if (r.IsInitialRealm)
{
_Game.InitialRealm = r;
break;
}
}
}
/// <summary>
/// Adds a object to the game world. Any Game Object can be supplied as a parameter.
/// </summary>
/// <param name="worldObject"></param>
public Boolean AddObject(dynamic worldObject)
{
//Check if this object is a Realm
if (worldObject is Realm)
{
///Add it to the Worlds Realm collection
this.RealmCollection.Add(worldObject);
}
//Check if this object is a Zone
else if (worldObject is Zone)
{
//Query the Realm collection to find the Realm this Zone belongs to.
var realmQuery =
from r in RealmCollection
where r.Filename == worldObject.Realm
select r;
//Add the zone to the Realm we found
if (realmQuery.FirstOrDefault() != null)
realmQuery.FirstOrDefault().AddZone(worldObject);
else
{
Log.Write("Error: Attempted to add Zone " + worldObject.Filename + " to a unspecified Realm.");
return false;
}
}
else if (worldObject is Room)
{
var realmQuery =
from r in RealmCollection
where r.Filename == worldObject.Realm
select r;
var zoneQuery =
from z in realmQuery.FirstOrDefault().ZoneCollection
where z.Filename == worldObject.Zone
select z;
if (zoneQuery.FirstOrDefault() != null)
zoneQuery.FirstOrDefault().AddRoom(worldObject);
else
{
Log.Write("Error: Attempted to add Room " + worldObject.Filename + " to a unspecified Realm and/or Zone");
return false;
}
}
return true;
}
/// <summary>
/// Adds a Realm to the Games current list of Realms.
/// </summary>
/// <param name="realm"></param>
private void AddRealm(Realm realm)
{
//If this Realm is set as Initial then we need to disable any previously
//set Realms to avoid conflict.
if (realm.IsInitialRealm)
{
foreach (Realm r in RealmCollection)
{
if (r.IsInitialRealm)
{
r.IsInitialRealm = false;
break;
}
}
}
//Set this Realm as the Games initial Realm
if (realm.IsInitialRealm)
_Game.InitialRealm = realm;
//TODO: Check for duplicate Realms.
RealmCollection.Add(realm);
}
/// <summary>
/// Returs a reference to an object that matches the supplied Type and filename.
/// Object MUST inherit from BaseObject or one of its child classes in order to be found.
/// </summary>
/// <param name="objectType">Determins the Type of object to perform the search for. Using Standard will search for objects that inherit from BaseObject, but none of BaseObjects child Types.</param>
/// <param name="filename"></param>
/// <returns></returns>
public BaseObject GetObject(ObjectTypes objectType, String filename)
{
BaseObject obj = new BaseObject(_Game);
switch (objectType)
{
case ObjectTypes.Standard:
break;
case ObjectTypes.Character:
break;
case ObjectTypes.Item:
break;
case ObjectTypes.Realm:
obj = GetRealm(filename);
break;
case ObjectTypes.Zone:
break;
case ObjectTypes.Room:
break;
}
return obj;
}
/// <summary>
/// Returns a reference to the Realm contained within the Realms collection if it exists.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private Realm GetRealm(String filename)
{
foreach (Realm r in RealmCollection)
{
if (r.Filename == filename)
return r;
}
return null;
}
}
}