Doodle Post
3 Comments
---
bob:
name: "Bob"
health: 10
money: 5
drop: [null, null, basicattack]
color: ff0000
combat:
- Attack 1
- Attack 1
- [Attack 1, Attack 1, Charge]
- Attack 1
- [Flee, Defend]
- Restart
...
// An enemy class to match the structure of the YAML
public class Enemy : DataObject {
public static string DataPath = Assets.Data.Enemies;
public int Health { get; set; }
public int Money { get; set; }
public List<string> Drop { get; set; }
public List<object> Combat { get; set; }
public string Color { get; set; }
}
...
// A method to load the data from the YAML
enemies = Load<Parsed.Enemy>();
...
// The method to load the data from YAML
static Dictionary<string, T> Load<T>() where T : Parsed.DataObject {
string dataPath = "";
try {
dataPath = (string)Util.GetFieldValue(typeof(T), "DataPath");
}
catch {
throw new Exception(string.Format("No data path found on type {0}", typeof(T)));
}
var dict = DD.Yaml.Deserialize<Dictionary<string, T>>(File.ReadAllText(dataPath));
dict.Each((k, v) => {
v.Id = k;
});
return dict;
}