我正在开发一款游戏,在一个名为WorldState
的类中维护其信息。世界上的每个游戏对象(树木,僵尸,玩家,健康包等)由三个对象组成:GameObjectController
,GameObjectModel
和GameObjectView
。 (如果游戏对象需要附加功能,这些类可以扩展。)这是一个持久状态的好接口吗?
WorldState
提供了用于加载和保存GameObjects的接口IPersistentState
的实现。目前,IPersistentState
的实现是ReadWriteXML
,它使用XML。我担心IPersistentState
接口过于耦合使用XML - 如果我要切换到使用数据库或csv或其他类型,可能需要更改它。我认为我没有创造出足够的抽象。
的IPersistentState
接口:
/// <summary>
/// Used to read and write from persistent memory.
///
/// When dictionaries are returned, they are key-value pairs:
/// dict["key"] == value
///
/// Reading is done from whatever the current URI is.
/// </summary>
// This is too hard coded to XML.
public interface IPersistentState
{
/// <summary>
/// Gets the KV for every object of element `elementName`.
/// Really this is just a convenience/wrapper method for `GetAllOfTypeIn()`
/// where the ancestor is the root.
/// </summary>
/// <param name="elementName">the element of object to get, like "zombie" or "point"</param>
/// <returns>(See interface summary)
/// For Zombie, it could look like this:
///
/// dict["positionX"] == "23"
/// dict["positionY"] == "4"
///
/// etc
/// </returns>
ICollection<IDictionary<string, string>> GetAllOfType(string elementName);
/// <summary>
/// Creates or overwrites the file `fileName`, filling it with the data in `dicts`.
/// The root of the data is `rootName`.
/// </summary>
/// <param name="dicts"></param>
/// <param name="rootName"></param>
/// <param name="fileName"></param>
void Write(ICollection<IDictionary<string, string>> dicts, string rootName, string fileName);
/// <summary>
/// Appends the information in `dict` to the end of the current file, under the element `elementName`.
/// </summary>
/// <param name="dict">The key-value pairs to append to the file identified by the current URI</param>
/// <param name="elementName">The root of the key-value pairs to append</param>
void Append(IDictionary<string, string> dict, string elementName);
/// <summary>
/// Sets the current URI from which to read.
/// </summary>
/// <param name="uri">Presumed to be in the Data/ directory.</param>
// not generic?
void SetURI(string uri);
/// <summary>
/// Gets the KV for every object of element `elementName`.
/// The search is constrainted to within `ancestor`.
/// </summary>
/// <param name="elementName">The element to look for</param>
/// <param name="ancestor">The element in which to search</param>
/// <returns>(See GetAllOfType)</returns>
ICollection<IDictionary<string, string>> GetAllTypeIn(string elementName, string ancestor);
}
这里是一个WorldState
装载方法使用例子:
persistentState.SetURI(configURI);
ICollection<IDictionary<string, string>> levelVariantURIDicts = persistentState.GetAllOfType("levelVariant");
// OH YEAH LINQ!!
levelVariantURIs = levelVariantURIDicts.Select(dict => dict["filePath"]).Distinct().ToList();
configURI点这里:
<config>
<levelVariant>
<filePath>SampleVariant</filePath>
</levelVariant>
<levelVariant>
<filePath>LegendaryMode</filePath>
</levelVariant>
<levelVariant>
<filePath>AmazingMode</filePath>
</levelVariant>
</config>
你觉得这个怎么样?也许这种抽象不适用于数据库,但它可能适用于不同的文件类型?也许我应该只希望项目总是使用XML?我如何改变这个使它更通用?