public static bool TryParse(string text, out GameObjectInstance instance) { // TODO: find an existing GameObject with the same name before creating one; // parsing the output of GameObjectInstance.ToString() will require converting pluralized names back to singular; // We might only be able to parse well-structured plurals, as in "N singular-name" or "singular-name (xN)". // ToString() could emit well-structured plurals and leave conversational formatting up to the client, // or it could have a pluralization format parameter, knowing that reverse-parsing conversational plurals will not work. char[] blank = { ' ' }; string[] tokens = text.Trim().Split(blank, StringSplitOptions.RemoveEmptyEntries); int count; if (tokens.Length > 1 && int.TryParse(tokens[0], out count)) { string name = text.Trim().Substring(tokens[0].Length).Trim(); GameObject obj = new GameObject(name); instance = new GameObjectInstance(obj, count); return(true); } else { instance = null; return(false); } }
private void GameObjectInstanceWriteXml(GameObjectInstance obj) { writer.WriteStartElement("Instance"); Utility.WriteAttribute(writer, "Count", obj.Count, 1); GameObjectWriteXml(obj.Item); SaveObject(obj.Contents, "Contains"); writer.WriteEndElement(); }
private GameObjectInstance GameObjectInstanceFromXml(XmlNode node) { GameObjectInstance obj = new GameObjectInstance(); obj.Count = Utility.ParseAttribute(node, "Count", 1); obj.Item = GameObjectFromXml(node.SelectSingleNode("GameObject")); obj.Contents = ItemListFromXml(node.SelectSingleNode("Contains")); return(obj); }
/// <summary> /// Performs a die roll to generate one or more IResolvers from Item /// </summary> /// <returns></returns> public IResolver Roll() { //GameObjectInstance result = new GameObjectInstance(Item, Dice.Roll() * Multiplier); if (Item is GameObject) { GameObjectInstance item = new GameObjectInstance((GameObject)Item, Dice.Roll() * Multiplier); Logger.Write("... rolled " + item.ToString()); return(item); } else if (Item is TableRoll && IsGrouped) { // Number of dice in Dice determines number of TableRolls performed. // Dice rolled with a single die determines number of occurrences of each TableRoll results. // e.g. if Dice = 4d10+2, 4 TableRolls are performed, and d10+2 is rolled to determine number of occurrences of each result. TableRoll t = (TableRoll)Item; Logger.Write("... performing " + Dice.Dice.ToString() + " rolls on " + t.Table.TableName); ItemList list = new ItemList(); DieRoll singleRoll = new DieRoll(1, Dice.Sides, Dice.Modifier); for (int i = 0; i < Dice.Dice; i++) { IResolver item = Item.Resolve(); if (item != null) { int count = singleRoll.Roll(); Logger.Write("... occurring " + count + " times"); for (int j = 0; j < count; j++) { list.Add(item); } } } return(list); } else { ItemList list = new ItemList(); int count = Dice.Roll() * Multiplier; Logger.Write("... rolled " + Dice.ToString() + ": " + count.ToString()); for (int i = 0; i < count; i++) { list.Add(Item.Resolve()); } return(list); } }
public IResolver Resolve() { Logger.Write("Resolve GameObject: " + Name); GameObjectInstance instance = new GameObjectInstance(this, 1); if (this.Contents == null) { instance.Contents = null; } else { instance.Contents = new ItemList(); instance.Contents = (ItemList)(this.Contents.Resolve()); } return(instance); }
//public int CompareTo(object other) //{ // if (other == null) // { // return 1; // } // else // { // return String.Compare(this.ToString(), other.ToString()); // } //} public int CompareTo(object other) { if (other == null) { return(1); } else { if (other is GameObjectInstance) { GameObjectInstance otherInstance = (GameObjectInstance)other; if (this.Item.Name == otherInstance.Item.Name) { return(this.Count.CompareTo(otherInstance.Count)); } else { return(this.Item.Name.CompareTo(otherInstance.Item.Name)); } } else if (other is GameObject) { string otherName = other.ToString(); if (this.Item.Name == otherName) { return(this.Count.CompareTo(1)); } else { return(this.Item.Name.CompareTo(otherName)); } } else { return(String.Compare(this.ToString(), other.ToString())); } } }
/// <summary> /// Convert a text string to an IResolver /// </summary> /// <param name="itemText"></param> /// <returns></returns> public static IResolver ParseItem(string itemText) { char[] comma = { ',' }; char[] blank = { ' ' }; // if the item is a comma-separated list, parse it as an ItemList string[] items = itemText.Split(comma, StringSplitOptions.RemoveEmptyEntries); if (items.Length > 1) { ItemList itemList = new ItemList(); foreach (string item in items) { IResolver obj = ParseItem(item); if (obj != null) { itemList.Add(obj); } } return(itemList); } // parse an individual item else if (items.Length == 1) { DieRoll dieRoll; GameObject gameObject; string item = items[0]; // ItemRoll string[] tokens = item.Split(blank, StringSplitOptions.RemoveEmptyEntries); if (DieRoll.TryParse(tokens[0], out dieRoll)) { string theRest = item.Substring(tokens[0].Length).Trim(); if (GameObject.TryParse(theRest, out gameObject)) { return(new ItemRoll(gameObject, dieRoll)); } } // GameObjectInstance GameObjectInstance gameObjectInstance; if (GameObjectInstance.TryParse(item, out gameObjectInstance)) { return(gameObjectInstance); } // TableRoll if (item.ToLower().StartsWith("roll")) { TableRoll tableRoll; if (TableRoll.TryParse(item.Remove(0, 5), out tableRoll)) { return(tableRoll); } } // GameObject if (GameObject.TryParse(item, out gameObject)) { return(gameObject); } } return(null); }