Example #1
0
File: Eggo.cs Project: mozarty/Eggo
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            if (world == null)
            {
                world = new World(Vector2.Zero);
            }
            else
            {
                world.Clear();
            }
            world.Gravity = new Vector2(0f, 20f);

            // TODO: Add your initialization logic here
            // Calculate safe bounds based on current resolution
            Viewport viewport = graphics.GraphicsDevice.Viewport;
            safeBounds = new Rectangle(
                (int)(viewport.Width * SafeAreaPortion),
                (int)(viewport.Height * SafeAreaPortion),
                (int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
                (int)(viewport.Height * (1 - 2 * SafeAreaPortion)));

            //player = new Player();
            //ground = new Ground();
            level = new Level("Levels/TheInsider_XML");
            assetCreator = new AssetCreator(GraphicsDevice);
            base.Initialize();
        }
Example #2
0
        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>();
        }