/// <summary> /// Initializes a new instance of the <see cref="BulletMLLib.BulletMLTask"/> class. /// </summary> /// <param name="node">Node.</param> /// <param name="owner">Owner.</param> public BulletMLTask(BulletMLNode node, BulletMLTask owner) { if (null == node) { throw new NullReferenceException("node argument cannot be null"); } ChildTasks = new List<BulletMLTask>(); ParamList = new List<float>(); TaskFinished = false; this.Owner = owner; this.Node = node; }
/// <summary> /// Initialize this bullet with a top level node /// </summary> /// <param name="rootNode">This is a top level node... find the first "top" node and use it to define this bullet</param> public void InitTopNode(BulletMLNode rootNode) { System.Diagnostics.Debug.Assert(null != rootNode); //okay find the item labelled 'top' bool bValidBullet = false; BulletMLNode topNode = rootNode.FindLabelNode("top", ENodeName.action); if (topNode != null) { //initialize with the top node we found! InitNode(topNode); bValidBullet = true; } else { //ok there is no 'top' node, so that means we have a list of 'top#' nodes for (int i = 1; i < 10; i++) { topNode = rootNode.FindLabelNode("top" + i, ENodeName.action); if (topNode != null) { if (!bValidBullet) { //Use this bullet! InitNode(topNode); bValidBullet = true; } else { //Create a new top bullet Bullet newDude = _bulletManager.CreateBullet(this, true); //set the position to this dude's position newDude.X = this.X; newDude.Y = this.Y; //initialize with the node we found newDude.InitNode(topNode); } } } } if (!bValidBullet) { //We didnt find a "top" node for this dude, remove him from the game. _bulletManager.RemoveBullet(this); } }
/// <summary> /// This bullet is fired from another bullet, initialize it from the node that fired it /// </summary> /// <param name="subNode">Sub node that defines this bullet</param> public void InitNode(BulletMLNode subNode) { System.Diagnostics.Debug.Assert(null != subNode); //clear everything out Tasks.Clear(); //Grab that top level node MyNode = subNode; //found a top num node, add a task for it BulletMLTask task = new BulletMLTask(subNode, null); //parse the nodes into the task list task.ParseTasks(this); //initialize all the tasks task.InitTask(this); Tasks.Add(task); }
/// <summary> /// Parse a specified node and bullet into this task /// </summary> /// <param name="myNode">the node for this dude</param> /// <param name="bullet">the bullet this dude is controlling</param> public virtual void ParseChildNode(BulletMLNode childNode, Bullet bullet) { Debug.Assert(null != childNode); Debug.Assert(null != bullet); //construct the correct type of node switch (childNode.Name) { case ENodeName.repeat: { //convert the node to an repeatnode RepeatNode myRepeatNode = childNode as RepeatNode; //create a placeholder bulletmltask for the repeat node RepeatTask repeatTask = new RepeatTask(myRepeatNode, this); //parse the child nodes into the repeat task repeatTask.ParseTasks(bullet); //store the task ChildTasks.Add(repeatTask); } break; case ENodeName.action: { //convert the node to an ActionNode ActionNode myActionNode = childNode as ActionNode; //create the action task ActionTask actionTask = new ActionTask(myActionNode, this); //parse the children of the action node into the task actionTask.ParseTasks(bullet); //store the task ChildTasks.Add(actionTask); } break; case ENodeName.actionRef: { //convert the node to an ActionNode ActionRefNode myActionNode = childNode as ActionRefNode; //create the action task ActionTask actionTask = new ActionTask(myActionNode, this); //add the params to the action task for (int i = 0; i < childNode.ChildNodes.Count; i++) { actionTask.ParamList.Add(childNode.ChildNodes[i].GetValue(this, bullet)); } //parse the children of the action node into the task actionTask.ParseTasks(bullet); //store the task ChildTasks.Add(actionTask); } break; case ENodeName.changeSpeed: { ChildTasks.Add(new ChangeSpeedTask(childNode as ChangeSpeedNode, this)); } break; case ENodeName.changeDirection: { ChildTasks.Add(new ChangeDirectionTask(childNode as ChangeDirectionNode, this)); } break; case ENodeName.fire: { //convert the node to a fire node FireNode myFireNode = childNode as FireNode; //create the fire task FireTask fireTask = new FireTask(myFireNode, this); //parse the children of the fire node into the task fireTask.ParseTasks(bullet); //store the task ChildTasks.Add(fireTask); } break; case ENodeName.fireRef: { //convert the node to a fireref node FireRefNode myFireNode = childNode as FireRefNode; //create the fire task FireTask fireTask = new FireTask(myFireNode.ReferencedFireNode, this); //add the params to the fire task for (int i = 0; i < childNode.ChildNodes.Count; i++) { fireTask.ParamList.Add(childNode.ChildNodes[i].GetValue(this, bullet)); } //parse the children of the action node into the task fireTask.ParseTasks(bullet); //store the task ChildTasks.Add(fireTask); } break; case ENodeName.wait: { ChildTasks.Add(new WaitTask(childNode as WaitNode, this)); } break; case ENodeName.vanish: { ChildTasks.Add(new VanishTask(childNode as VanishNode, this)); } break; case ENodeName.accel: { ChildTasks.Add(new AccelTask(childNode as AccelNode, this)); } break; } }
/// <summary> /// Parses a bulletml document into this bullet pattern /// </summary> /// <param name="xmlFileName">Xml file name.</param> public void ParseXML(string xmlFileName, XmlReader reader) { try { //Open the file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode.NodeType == XmlNodeType.Element) { //eat up the name of that xml node string strElementName = rootXmlNode.Name; if ("bulletml" != strElementName) { //The first node HAS to be bulletml throw new Exception("Error reading \"" + xmlFileName + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead"); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(ENodeName.bulletml); //Read in the whole bulletml tree RootNode.Parse(rootXmlNode, null); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be veritcal or horizontal Orientation = StringToPatternType(strValue); } } } } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } //grab that filename Filename = xmlFileName; //validate that the bullet nodes are all valid try { RootNode.ValidateNode(); } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } }
/// <summary> /// Parse a specified node and bullet into this task /// </summary> /// <param name="myNode">the node for this dude</param> /// <param name="bullet">the bullet this dude is controlling</param> public override void ParseChildNode(BulletMLNode childNode, Bullet bullet) { Debug.Assert(null != childNode); Debug.Assert(null != bullet); switch (childNode.Name) { case ENodeName.bulletRef: { //Create a task for the bullet ref BulletRefNode refNode = childNode as BulletRefNode; BulletRefTask = new BulletMLTask(refNode.ReferencedBulletNode, this); //populate the params of the bullet ref for (int i = 0; i < childNode.ChildNodes.Count; i++) { BulletRefTask.ParamList.Add(childNode.ChildNodes[i].GetValue(this, bullet)); } BulletRefTask.ParseTasks(bullet); ChildTasks.Add(BulletRefTask); } break; case ENodeName.bullet: { //Create a task for the bullet ref BulletRefTask = new BulletMLTask(childNode, this); BulletRefTask.ParseTasks(bullet); ChildTasks.Add(BulletRefTask); } break; default: { //run the node through the base class if we don't want it base.ParseChildNode(childNode, bullet); } break; } }
/// <summary> /// Parse the specified bulletNodeElement. /// Read all the data from the xml node into this dude. /// </summary> /// <param name="bulletNodeElement">Bullet node element.</param> public void Parse(XmlNode bulletNodeElement, BulletMLNode parentNode) { // Handle null argument. if (null == bulletNodeElement) { throw new ArgumentNullException("bulletNodeElement"); } //grab the parent node Parent = parentNode; //Parse all our attributes XmlNamedNodeMap mapAttributes = bulletNodeElement.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //skip the type attribute in top level nodes if (ENodeName.bulletml == Name) { continue; } //get the bullet node type NodeType = BulletMLNode.StringToType(strValue); } else if ("label" == strName) { //label is just a text value Label = strValue; } } //parse all the child nodes if (bulletNodeElement.HasChildNodes) { for (XmlNode childNode = bulletNodeElement.FirstChild; null != childNode; childNode = childNode.NextSibling) { //if the child node is a text node, parse it into this dude if (XmlNodeType.Text == childNode.NodeType) { //Get the text of the child xml node, but store it in THIS bullet node NodeEquation.Parse(childNode.Value); continue; } else if (XmlNodeType.Comment == childNode.NodeType) { //skip any comments in the bulletml script continue; } //create a new node BulletMLNode childBulletNode = NodeFactory.CreateNode(BulletMLNode.StringToName(childNode.Name)); //read in the node and store it childBulletNode.Parse(childNode, this); ChildNodes.Add(childBulletNode); } } }
/// <summary> /// Parse a specified node and bullet into this task /// </summary> /// <param name="myNode">the node for this dude</param> /// <param name="bullet">the bullet this dude is controlling</param> public virtual void ParseChildNode(BulletMLNode childNode, Bullet bullet) { Debug.Assert(null != childNode); Debug.Assert(null != bullet); //construct the correct type of node switch (childNode.Name) { case ENodeName.repeat: { //convert the node to an repeatnode RepeatNode myRepeatNode = childNode as RepeatNode; //create a placeholder bulletmltask for the repeat node RepeatTask repeatTask = new RepeatTask(myRepeatNode, this); //parse the child nodes into the repeat task repeatTask.ParseTasks(bullet); //store the task ChildTasks.Add(repeatTask); } break; case ENodeName.action: { //convert the node to an ActionNode ActionNode myActionNode = childNode as ActionNode; //create the action task ActionTask actionTask = new ActionTask(myActionNode, this); //parse the children of the action node into the task actionTask.ParseTasks(bullet); //store the task ChildTasks.Add(actionTask); } break; case ENodeName.actionRef: { //convert the node to an ActionNode ActionRefNode myActionNode = childNode as ActionRefNode; //create the action task ActionTask actionTask = new ActionTask(myActionNode, this); //add the params to the action task for (int i = 0; i < childNode.ChildNodes.Count; i++) { actionTask.ParamList.Add(childNode.ChildNodes[i].GetValue(this, bullet.MyBulletManager)); } //parse the children of the action node into the task actionTask.ParseTasks(bullet); //store the task ChildTasks.Add(actionTask); } break; case ENodeName.changeSpeed: { ChildTasks.Add(new ChangeSpeedTask(childNode as ChangeSpeedNode, this)); } break; case ENodeName.changeDirection: { ChildTasks.Add(new ChangeDirectionTask(childNode as ChangeDirectionNode, this)); } break; case ENodeName.fire: { //convert the node to a fire node FireNode myFireNode = childNode as FireNode; //create the fire task FireTask fireTask = new FireTask(myFireNode, this); //parse the children of the fire node into the task fireTask.ParseTasks(bullet); //store the task ChildTasks.Add(fireTask); } break; case ENodeName.fireRef: { //convert the node to a fireref node FireRefNode myFireNode = childNode as FireRefNode; //create the fire task FireTask fireTask = new FireTask(myFireNode.ReferencedFireNode, this); //add the params to the fire task for (int i = 0; i < childNode.ChildNodes.Count; i++) { fireTask.ParamList.Add(childNode.ChildNodes[i].GetValue(this, bullet.MyBulletManager)); } //parse the children of the action node into the task fireTask.ParseTasks(bullet); //store the task ChildTasks.Add(fireTask); } break; case ENodeName.wait: { ChildTasks.Add(new WaitTask(childNode as WaitNode, this)); } break; case ENodeName.vanish: { ChildTasks.Add(new VanishTask(childNode as VanishNode, this)); } break; case ENodeName.accel: { ChildTasks.Add(new AccelTask(childNode as AccelNode, this)); } break; } }
/// <summary> /// Parse the specified bulletNodeElement. /// Read all the data from the xml node into this dude. /// </summary> /// <param name="bulletNodeElement">Bullet node element.</param> public void Parse(XmlNode bulletNodeElement, BulletMLNode parentNode) { // Handle null argument. if (null == bulletNodeElement) { throw new ArgumentNullException("bulletNodeElement"); } //grab the parent node Parent = parentNode; //Parse all our attributes XmlNamedNodeMap mapAttributes = bulletNodeElement.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //skip the type attribute in top level nodes if (ENodeName.bulletml == Name) { continue; } //get the bullet node type NodeType = BulletMLNode.StringToType(strValue); } else if ("label" == strName) { //label is just a text value Label = strValue; } } //parse all the child nodes if (bulletNodeElement.HasChildNodes) { for (XmlNode childNode = bulletNodeElement.FirstChild; null != childNode; childNode = childNode.NextSibling) { //if the child node is a text node, parse it into this dude if (XmlNodeType.Text == childNode.NodeType) { // Store the original text Text = childNode.Value; // Skip the value for triggers if (ENodeName.trigger != Name) { //Get the text of the child xml node, but store it in THIS bullet node NodeEquation.Parse(Text); } continue; } else if (XmlNodeType.Comment == childNode.NodeType) { //skip any comments in the bulletml script continue; } //create a new node BulletMLNode childBulletNode = NodeFactory.CreateNode(BulletMLNode.StringToName(childNode.Name)); //read in the node and store it childBulletNode.Parse(childNode, this); ChildNodes.Add(childBulletNode); } } }
/// <summary> /// Parses a bulletml document into this bullet pattern /// </summary> /// <param name="xmlFileName">Xml file name.</param> public void ParseXML(string xmlFileName) { #if NETFX_CORE XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; #else XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.None; settings.DtdProcessing = DtdProcessing.Parse; settings.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler); #endif try { using (XmlReader reader = XmlReader.Create(xmlFileName, settings)) { //Open the file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); XmlNode rootXmlNode = xmlDoc.DocumentElement; //make sure it is actually an xml node if (rootXmlNode.NodeType == XmlNodeType.Element) { //eat up the name of that xml node string strElementName = rootXmlNode.Name; if ("bulletml" != strElementName) { //The first node HAS to be bulletml throw new Exception("Error reading \"" + xmlFileName + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead"); } //Create the root node of the bulletml tree RootNode = new BulletMLNode(ENodeName.bulletml); //Read in the whole bulletml tree RootNode.Parse(rootXmlNode, null); //Find what kind of pattern this is: horizontal or vertical XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { //will only have the name attribute string strName = mapAttributes.Item(i).Name; string strValue = mapAttributes.Item(i).Value; if ("type" == strName) { //if this is a top level node, "type" will be veritcal or horizontal Orientation = StringToPatternType(strValue); } } } } } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } //grab that filename Filename = xmlFileName; //validate that the bullet nodes are all valid try { RootNode.ValidateNode(); } catch (Exception ex) { //an error ocurred reading in the tree throw new Exception("Error reading \"" + xmlFileName + "\"", ex); } }