Edit:
Spent more time on this and with Blue's answer I was able to come up with a viable solution.
Sample XML
<scene>
<description>You enter a dark room </description>
<condition type="itemEquip" itemType="Flashlight">
<message>and do you do not have a light source.</message>
</condition>
<button text="Use flashlight" sceneIndex="2">
<condition type="itemHave" itemType="Flashlight">
<message> and you forgot your flashlight at home.</message>
</condition>
</button>
<button text="Go back" sceneIndex="0"/>
</scene>
Made it so that you can have a condition for a button and for the scene as separate checks, that way buttons can be also dynamic based on the condition class.
The condition class:
public enum ConditionType
{
itemLevelReq,
itemEquip,
itemHas,
playerLevel,
playerAttribute,
baseDefault
}
abstract class Condition
{
public ConditionType type;
public string failMessage = "Condition not met";
public abstract bool IsThisConditionMet();
}
class BaseCondition : Condition
{
ConditionType type = ConditionType.baseDefault;
string failMessage = "Condition not met";
override public bool IsThisConditionMet()
{
return false;
}
}
//example concrete condition
class ItemHasCondition : Condition
{
ConditionType type = ConditionType.itemHas;
string itemType;
int levelReq;
public ItemHasCondition(String item, string fail)
{
this.itemType = item;
this.failMessage = fail;
}
override public bool IsThisConditionMet()
{
if(Program.testPlayer.items.Contains(this.itemType)){
return true;
} else {
return false;
}
}
}
Then here is the parser for the XML(Simplified)
private static void readThisXML()
{
XDocument doc = XDocument.Load("Scenes.xml");
foreach (XElement root in doc.Root.Elements("scene"))
{
SceneStruct scene = new SceneStruct();
List<ButtonStruct> buttons = new List<ButtonStruct>();
List<Condition> conditions = new List<Condition>();
foreach (XElement element in root.Elements("button"))
{
ButtonStruct btn = new ButtonStruct();
List<int> btnIndex = new List<int>();
List<Condition> btnConditions = new List<Condition>();
btn.text = (string)element.Attribute("text");
//Maybe different indexes based on conditions?
btnIndex.Add((int)element.Attribute("sceneIndex"));
btn.btnIndex = btnIndex;
foreach (XElement elementC in element.Elements("condition"))
{
if (elementC.Attribute("type").Value.Equals("itemEquip"))
{
Condition con = new ItemHasCondition((string)elementC.Attribute("itemType"), (string)elementC.Element("message").Value);
btnConditions.Add(con);
}
}
btn.conditions = btnConditions;
buttons.Add(btn);
}
dwStruct.buttons = buttons;
foreach (XElement element in root.Elements("description"))
{
scene.description = (string)element.Value;
}
foreach (XElement element in root.Elements("condition"))
{
if (element.Attribute("type").Value.Equals("itemEquip"))
{
Condition con = new ItemHasCondition((string)element.Attribute("itemType"), (string)element.Element("message").Value);
conditions.Add(con);
}
}
scene.conditions = conditions;
sceneList.Add(scene);
}
}
Hopefully this helps anyone who happens to stumble over this problem like I did.