private void LoadGameLevel(int level) { XmlDocument xml = new XmlDocument(); xml.Load("D:\\VS 2010 Projects\\Shooter\\Shooter\\ShooterContent\\Levels\\" + level.ToString() + ".xml"); XmlNode root = xml.FirstChild; XmlNode waveNode; XmlNode formationNode; XmlNode enemyNode; XmlAttributeCollection attributes; Level = new Level(); //level contains list of waves Wave nextWave; //wave contains list of formations Formation nextFormation; //formation contains list of enemies Enemy nextEnemy; //enemies will die horrible deaths AttackPattern nextAttackPattern; //attackPattern defines enemy movement between start and end positions ShotPattern nextShotPattern; //defines enemy bullets per shot and how those bullets move Texture2D shotImage; //duh float waveDuration = 0; float formationTrigger = 0; string attackPatternID = ""; //name identifier for AttackPattern string shotPatternID = ""; //name identifier for ShotPattern string enemyType = ""; //tells which texture will be drawn for enemy string enemyShot = ""; //tells which texture will be drawn for enemy shot float enemyHealth = 0; //duh float startX = 0; //Start and End positions the enemy ship will use float startY = 0; float endX = 0; float endY = 0; int fireCount = 0; //number of times the enemy will fire it's shotPattern float fireRate = 0; //how frequently the enemy will fire another shotPattern int shotSpeed = 0; //how fast each shot will move List<ShotPattern> shotPatterns; //list of shotPatterns that will be passed to enemy int count = 0; //counter for adding ShotPatterns if (root.HasChildNodes) { for (int i = 0; i < root.ChildNodes.Count; i++) //waves { waveNode = root.ChildNodes[i]; attributes = waveNode.Attributes; waveDuration = Convert.ToSingle(attributes.GetNamedItem("d").Value); nextWave = new Wave(waveDuration); //Create new wave if (waveNode.HasChildNodes) { for (int j = 0; j < waveNode.ChildNodes.Count; j++) //formations { formationNode = waveNode.ChildNodes[j]; attributes = formationNode.Attributes; formationTrigger = Convert.ToSingle(attributes.GetNamedItem("d").Value); nextFormation = new Formation(formationTrigger); if (formationNode.HasChildNodes) { for (int k = 0; k < formationNode.ChildNodes.Count; k++) //enemies { enemyNode = formationNode.ChildNodes[k]; attributes = enemyNode.Attributes; enemyType = attributes.GetNamedItem("type").Value; enemyShot = attributes.GetNamedItem("shot").Value; try { enemyHealth = Convert.ToInt32(attributes.GetNamedItem("health").Value); } catch (FormatException e) { Console.WriteLine("Enemy Health: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Health: The number cannot fit in an Int32."); } try { startX = Convert.ToInt32(attributes.GetNamedItem("startX").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting X: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting X: The number cannot fit in an Int32."); } try { startY = Convert.ToInt32(attributes.GetNamedItem("startY").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting Y: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting Y: The number cannot fit in an Int32."); } try { endX = Convert.ToInt32(attributes.GetNamedItem("endX").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting X: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting X: The number cannot fit in an Int32."); } try { endY = Convert.ToInt32(attributes.GetNamedItem("endY").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting Y: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting Y: The number cannot fit in an Int32."); } try { shotSpeed = Convert.ToInt32(attributes.GetNamedItem("shotSpeed").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Speed: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Speed: The number cannot fit in an Int32."); } try { fireCount = Convert.ToInt32(attributes.GetNamedItem("fireCount").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Freqency: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Freqency: The number cannot fit in an Int32."); } try { fireRate = Convert.ToSingle(attributes.GetNamedItem("fireRate").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Freqency: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Freqency: The number cannot fit in a float."); } //enemy attack pattern nextAttackPattern = new AttackPattern(); attackPatternID = attributes.GetNamedItem("ap").Value; nextAttackPattern.SetID(attackPatternID); nextAttackPattern.SetEndPosition(endX, endY); nextAttackPattern.SetStartPosition(startX, startY); if ((nextAttackPattern = PopulateAttackPattern(nextAttackPattern)) == null) //Load AttackPattern moveList { Console.WriteLine("Failed to load attack pattern " + attackPatternID.ToString()); } //enemy shot pattern shotPatternID = attributes.GetNamedItem("sp").Value; shotPatterns = new List<ShotPattern>(); //only one shotPattern List per enemy, so new outside while loop count = 0; while (count < fireCount) { nextShotPattern = new ShotPattern(shotPatternID, shotSpeed); //we potentially want multiple ShotPattern objects per enemy, so new in while loop shotImage = this.Content.Load<Texture2D>(enemyShot); //Create the appropriate number of shotPatterns for the enemy if ((nextShotPattern = PopulateShotPattern(nextShotPattern, shotImage)) == null) //populate ShotPattern's shotList { Console.WriteLine("Failed to load shot pattern " + shotPatternID.ToString()); } shotPatterns.Add(nextShotPattern); count++; } //ContentManager should only load textures if they aren't already cached nextEnemy = new Enemy(this.Content.Load<Texture2D>(enemyType), Convert.ToInt32(enemyHealth), nextAttackPattern, shotPatterns, fireCount, fireRate); LevelEnemies.Add(nextEnemy); nextFormation.AddEnemy(nextEnemy); } nextWave.AddFormation(nextFormation); } else { Console.WriteLine("Empty formation found"); } } Level.AddWave(nextWave); } else { Console.WriteLine("Empty wave found"); } } } else { Console.WriteLine("root node: no children found"); } }
public void AddWave(Wave w) { m_waves.Add(w); }
private void LoadGameLevel(int level) { XmlDocument xml = new XmlDocument(); xml.Load("D:\\VS 2010 Projects\\Shooter\\Shooter\\ShooterContent\\Levels\\" + level.ToString() + ".xml"); XmlNode root = xml.FirstChild; XmlNode waveNode; XmlNode formationNode; XmlNode enemyNode; XmlAttributeCollection attributes; Level = new Level(); //level contains list of waves Wave nextWave; //wave contains list of formations Formation nextFormation; //formation contains list of enemies Enemy nextEnemy; //enemies will die horrible deaths AttackPattern nextAttackPattern; //attackPattern defines enemy movement between start and end positions ShotPattern nextShotPattern; //defines enemy bullets per shot and how those bullets move Texture2D shotImage; //duh float waveDuration = 0; float formationTrigger = 0; string attackPatternID = ""; //name identifier for AttackPattern string shotPatternID = ""; //name identifier for ShotPattern string enemyType = ""; //tells which texture will be drawn for enemy string enemyShot = ""; //tells which texture will be drawn for enemy shot float enemyHealth = 0; //duh float startX = 0; //Start and End positions the enemy ship will use float startY = 0; float endX = 0; float endY = 0; int fireCount = 0; //number of times the enemy will fire it's shotPattern float fireRate = 0; //how frequently the enemy will fire another shotPattern int shotSpeed = 0; //how fast each shot will move List <ShotPattern> shotPatterns; //list of shotPatterns that will be passed to enemy int count = 0; //counter for adding ShotPatterns if (root.HasChildNodes) { for (int i = 0; i < root.ChildNodes.Count; i++) //waves { waveNode = root.ChildNodes[i]; attributes = waveNode.Attributes; waveDuration = Convert.ToSingle(attributes.GetNamedItem("d").Value); nextWave = new Wave(waveDuration); //Create new wave if (waveNode.HasChildNodes) { for (int j = 0; j < waveNode.ChildNodes.Count; j++) //formations { formationNode = waveNode.ChildNodes[j]; attributes = formationNode.Attributes; formationTrigger = Convert.ToSingle(attributes.GetNamedItem("d").Value); nextFormation = new Formation(formationTrigger); if (formationNode.HasChildNodes) { for (int k = 0; k < formationNode.ChildNodes.Count; k++) //enemies { enemyNode = formationNode.ChildNodes[k]; attributes = enemyNode.Attributes; enemyType = attributes.GetNamedItem("type").Value; enemyShot = attributes.GetNamedItem("shot").Value; try { enemyHealth = Convert.ToInt32(attributes.GetNamedItem("health").Value); } catch (FormatException e) { Console.WriteLine("Enemy Health: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Health: The number cannot fit in an Int32."); } try { startX = Convert.ToInt32(attributes.GetNamedItem("startX").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting X: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting X: The number cannot fit in an Int32."); } try { startY = Convert.ToInt32(attributes.GetNamedItem("startY").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting Y: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting Y: The number cannot fit in an Int32."); } try { endX = Convert.ToInt32(attributes.GetNamedItem("endX").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting X: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting X: The number cannot fit in an Int32."); } try { endY = Convert.ToInt32(attributes.GetNamedItem("endY").Value); } catch (FormatException e) { Console.WriteLine("Enemy Starting Y: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Starting Y: The number cannot fit in an Int32."); } try { shotSpeed = Convert.ToInt32(attributes.GetNamedItem("shotSpeed").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Speed: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Speed: The number cannot fit in an Int32."); } try { fireCount = Convert.ToInt32(attributes.GetNamedItem("fireCount").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Freqency: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Freqency: The number cannot fit in an Int32."); } try { fireRate = Convert.ToSingle(attributes.GetNamedItem("fireRate").Value); } catch (FormatException e) { Console.WriteLine("Enemy Shot Freqency: Input string is not a sequence of digits."); } catch (OverflowException e) { Console.WriteLine("Enemy Shot Freqency: The number cannot fit in a float."); } //enemy attack pattern nextAttackPattern = new AttackPattern(); attackPatternID = attributes.GetNamedItem("ap").Value; nextAttackPattern.SetID(attackPatternID); nextAttackPattern.SetEndPosition(endX, endY); nextAttackPattern.SetStartPosition(startX, startY); if ((nextAttackPattern = PopulateAttackPattern(nextAttackPattern)) == null) //Load AttackPattern moveList { Console.WriteLine("Failed to load attack pattern " + attackPatternID.ToString()); } //enemy shot pattern shotPatternID = attributes.GetNamedItem("sp").Value; shotPatterns = new List <ShotPattern>(); //only one shotPattern List per enemy, so new outside while loop count = 0; while (count < fireCount) { nextShotPattern = new ShotPattern(shotPatternID, shotSpeed); //we potentially want multiple ShotPattern objects per enemy, so new in while loop shotImage = this.Content.Load <Texture2D>(enemyShot); //Create the appropriate number of shotPatterns for the enemy if ((nextShotPattern = PopulateShotPattern(nextShotPattern, shotImage)) == null) //populate ShotPattern's shotList { Console.WriteLine("Failed to load shot pattern " + shotPatternID.ToString()); } shotPatterns.Add(nextShotPattern); count++; } //ContentManager should only load textures if they aren't already cached nextEnemy = new Enemy(this.Content.Load <Texture2D>(enemyType), Convert.ToInt32(enemyHealth), nextAttackPattern, shotPatterns, fireCount, fireRate); LevelEnemies.Add(nextEnemy); nextFormation.AddEnemy(nextEnemy); } nextWave.AddFormation(nextFormation); } else { Console.WriteLine("Empty formation found"); } } Level.AddWave(nextWave); } else { Console.WriteLine("Empty wave found"); } } } else { Console.WriteLine("root node: no children found"); } }