public enum ConditionType{
default,
itemLevelReq,
itemEquip,
playerLevel,
playerAttribute
}
abstract class Condition{
abstract ConditionType type;
string failMessage = "Condition not met";
public abstract bool IsThisConditionMet();
}
//example concrete condition
class ItemLevelCondition : Condition{
Condition type = itemLevelReq;
string itemType;
int levelReq;
public ItemLevelCondition(String item, int level, string fail){
this.itemType = item;
this.levelReq = level;
this.failMessage = fail;
}
public bool IsThisConditionMet(){
if(player.items.contains(this.itemType)){
return player.items[itemType].level >= this.levelReq;
} else {
return false;
}
}
}
class Scene{
string description;
Condition[] requirements;
string responseMessage;
public Scene(string description,Condition[] requirements,string responseMessage)
{
this.description = description;
this.requirements = requirements;
this.responseMessage = responseMessage;
}
public bool AreAllConditionsMet(){
bool result = true;
string responseMessage;
foreach(Condition condition in requirements){
if(!condition.IsThisConditionMet()){
this.responseMessage += condition.failMessage + "\n";
}
result &= condition.IsThisConditionMet();
}
Scene.OutputFailMessage();
return result;
}
public void OutputFailMessage(){
Console.Write(this.responseMessage);
}
}
<scene index="5">
<description>You enter a dark room and feel a door stuck in front of you</description>
<condition type="itemLevelReq" itemType="light" levelReq="5">
<failMessage>and do not have enough light to see it well.</text>
</condition>
<condition type="playerAttribute" attrType="strength" levelReq="15">
<failMessage>You are not strong enough.</text>
</condition>
<condition type="itemEquip" itemType="crowbar" equipped="true">
<failMessage>You do not have a crowbar equipped.</text>
</condition>
</scene>
Here is a snippet of how to do the XML parsing itself:
using System.Collections.Generic;
using System.Xml;
public static List<Scene> InitialiseScenes()
{
List<Scene> scenes = new List<Scene>();
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Scenes.xml");
//This if block verifies the file was loaded into the stream and throws a FileNotFound if it isn't
if (stream != null)
{
StreamReader reader = new StreamReader(stream);
XmlDocument sceneDoc = new XmlDocument();
sceneDoc.LoadXml(reader.ReadToEnd());
string xpathQuery = "/scenes/scene";
foreach (XmlNode sceneNode in sceneDoc.SelectNodes(xpathQuery))
{
//Set up new Champion details
Scene currentScene;
List<Condition> conditions = InitialiseConditions(sceneNode.SelectNodes("conditions"));
currentScene = new Scene(sceneNode.SelectNode("description").InnerText, conditions)
scenes.Add(currentScene);
}
}
else
{
throw new FileNotFoundException("The resource file for the champions data is missing.", "Champions.xml");
}
return champions;
}
public List<Condition> InitialiseConditions(XmlNodeList nodeList) {
//Get conditions from nodes
List<Condition> result;
foreach (XmlNode conditionNode in nodeList){
Condition tmpCondition;
//TODO: Create list of nodes and use switch statement to load the
// node into a concrete implementation of the abstract Condition class
//eg. tmpCondition = new ItemLevelCondition(item, level, fail);
result.Add(tmpCondition);
}
}