private void GenerateEnemy(LevelEnemy enemy, ContentManager content, GameWindow Window) { int currentBehaviour = -1; switch (enemy.Type) { //Case Zombie enemy case 1: ZombieEnemy z = new ZombieEnemy(); currentBehaviour = enemy.EntryBehaviours[0]; if (enemy.EntryBehaviours.Length == 2) { if (random.NextDouble() > 0.5) { currentBehaviour = enemy.EntryBehaviours[1]; } } if (currentBehaviour == 0) { float x = (float)random.NextDouble() * (Window.ClientBounds.Width - z.boundingRectangle.Width); z.position = new Vector2(x, 0); z.isFalling = true; enemies.Add(z); } else if (currentBehaviour == 1) { //TODO: make it depend on the level size float y = (float)random.NextDouble() * (Window.ClientBounds.Height - z.boundingRectangle.Height); z.position = new Vector2(Window.ClientBounds.Width - y / 2, 350); z.isFalling = false; enemies.Add(z); } z.load(content); break; //Case Bomber enemy case 2: Bomberia b = new Bomberia(); currentBehaviour = enemy.EntryBehaviours[0]; if (enemy.EntryBehaviours.Length == 2) { if (random.NextDouble() > 0.5) { currentBehaviour = enemy.EntryBehaviours[1]; } } if (currentBehaviour == 0) { float x = (float)random.NextDouble() * (Window.ClientBounds.Width - b.boundingRectangle.Width); b.position = new Vector2(x, 0); b.isFalling = true; enemies.Add(b); } else if (currentBehaviour == 1) { //TODO: make it depend on the level size float y = (float)random.NextDouble() * (Window.ClientBounds.Height - b.boundingRectangle.Height); b.position = new Vector2(Window.ClientBounds.Width - y / 2, 350); b.isFalling = false; enemies.Add(b); } b.load(content); break; //TODO: Add the rest of enemies types default: break; } maxEnemiesToSpawn--; }
public Level(string xmlFile) { //Initialize enemiesCounterMap = new Dictionary<int, EnemyCounter>(); onDeathGeneratedEnemyIds = new List<GenerationConstraint>(); onInterarrivalTimeGeneratedEnemyIds = new List<GenerationConstraint>(); random = new Random(); instance = this; //Read the XML File and load it's information into the Level Class StreamReader sr = new StreamReader(xmlFile); string xmlContent = sr.ReadToEnd(); XDocument obj = XDocument.Parse(xmlContent); int.TryParse(obj.Descendants("number").First().Value, out this.levelNumber); int.TryParse(obj.Descendants("maxTime").First().Value, out this.levelDuration); float.TryParse(obj.Descendants("size").First().FirstAttribute.Value, out mapSize.X); float.TryParse(obj.Descendants("size").First().LastAttribute.Value, out mapSize.Y); float.TryParse(obj.Descendants("playerPosition").First().Value, out playerPos.X); //TODO: Load the player Y position based on the level number //Load the obstacles var obstacles = obj.Descendants("obstacle"); mapObstacles = new List<Obstacle>(); foreach (var o in obstacles) { Obstacle ob = new Obstacle(); Vector2 pos = new Vector2(); float.TryParse(o.FirstAttribute.Value, out pos.X); float.TryParse(o.Attributes().ElementAt(1).Value, out pos.Y); int t; int.TryParse(o.LastAttribute.Value, out t); ob.Position = pos; ob.Type = t; mapObstacles.Add(ob); } //Load the HotSpots var hotspots = obj.Descendants("hotspot"); mapHotSpots = new List<HotSpot>(); foreach (var h in hotspots) { Vector2 pos = new Vector2(); float.TryParse(h.FirstAttribute.Value, out pos.X); float.TryParse(h.Attributes().ElementAt(1).Value, out pos.Y); int t; int.TryParse(h.LastAttribute.Value, out t); //Create a hot spot depending on the type switch (t) { case 1: Shredder hs = new Shredder(); hs.position = pos; mapHotSpots.Add(hs); break; default: break; } } //Load the allowed objects char[] sep = { ',' }; string allowedObjsString = obj.Descendants("objects").First().Value; string[] allowedObjStringList = allowedObjsString.Split(sep, StringSplitOptions.RemoveEmptyEntries); AllowedObjects = new int[allowedObjStringList.Length]; for (int i = 0; i < allowedObjStringList.Length; i++) { AllowedObjects[i] = int.Parse(allowedObjStringList[i]); } //Load the dangerous allowed objects string allowedDObjsString = obj.Descendants("dangerousObjects").First().Value; string[] allowedDObjStringList = allowedDObjsString.Split(sep, StringSplitOptions.RemoveEmptyEntries); AllowedDObjects = new int[allowedDObjStringList.Length]; for (int i = 0; i < allowedDObjStringList.Length; i++) { AllowedDObjects[i] = int.Parse(allowedDObjStringList[i]); } int.TryParse(obj.Descendants("maxNumber").First().Value, out maxEnemiesToSpawn); //Load the initial enemies list var initialEn = obj.Descendants("initialEnemy"); initialEnemies = new List<InitialEnemy>(); foreach (var i in initialEn) { int type = int.Parse(i.FirstAttribute.Value); int number = int.Parse(i.Attributes().ElementAt(1).Value); string behaviours = i.LastAttribute.Value; InitialEnemy en = new InitialEnemy(type, number, behaviours); this.initialEnemies.Add(en); //Update enemy counter map if (!enemiesCounterMap.ContainsKey(en.Type)) { EnemyCounter enemyCounter = new EnemyCounter(); enemyCounter.CurrentCount += en.Number; enemiesCounterMap.Add(en.Type, enemyCounter); } else { EnemyCounter enemyCounter = enemiesCounterMap[en.Type]; enemyCounter.CurrentCount += en.Number; } } //Load the enemies types and constraints var enList = obj.Descendants("enemy"); levelEnemies = new List<LevelEnemy>(); foreach (var e in enList) { int type = int.Parse(e.FirstAttribute.Value); int maxNum = int.Parse(e.Attributes().ElementAt(1).Value); int dir = int.Parse(e.Attributes().ElementAt(2).Value); string behaviours = e.LastAttribute.Value; LevelEnemy en = new LevelEnemy(type, maxNum, behaviours, dir); var enConstraints = e.Descendants("constraint"); foreach (var c in enConstraints) { int cType = int.Parse(c.FirstAttribute.Value); double prob = double.Parse(c.LastAttribute.Value); GenerationConstraint constraint = new GenerationConstraint(en.Type, prob); //If constraint is generate on death if (cType == 0) { //Add to on death generation event onDeathGeneratedEnemyIds.Add(constraint); } else { //Add to on interarrival time generation event onInterarrivalTimeGeneratedEnemyIds.Add(constraint); } } levelEnemies.Add(en); //Update enemy counter map if (!enemiesCounterMap.ContainsKey(en.Type)) { EnemyCounter enemyCounter = new EnemyCounter(); enemyCounter.MaximumCount += en.MaxAlive; enemiesCounterMap.Add(en.Type, enemyCounter); } else { EnemyCounter enemyCounter = enemiesCounterMap[en.Type]; enemyCounter.MaximumCount += en.MaxAlive; } } if (player == null) player = new Player((int)playerPos.X); else player.position = new Vector2((int)playerPos.X, player.position.Y); ground = new Ground(); enemies = new List<Enemy>(); }