Ejemplo n.º 1
0
        public void CreateLevel()
        {
            State = GameState.New;

            foreach (var e in Entities.ToList())
            {
                if (e.GetType() != typeof(Player))
                {
                    e.Destroy();
                }
            }

            var width  = 31;
            var height = 21; //width * 9 / 16;

            SectorCountX = width * 6;
            SectorCountY = height * 6;
            sectors      = new int[SectorCountX * SectorCountY];

            Pathfinder = new Pathfinder(SectorCountX, SectorCountY);

            Player.Body.Position = new Vector2(
                SectorCountX * SectorSize / 2 + (SectorSize / 2),
                SectorCountY * SectorSize / 2 + (SectorSize / 2));

            Player.Health = 100;

            var maze  = new MazeGenerator(width, height);
            var walls = maze.GetWalls();

            foreach (var wall in walls.OfType <Wall>().ToList())
            {
                for (int i = 0; i < wall.BoundingBox.Width / 64; i++)
                {
                    for (int j = 0; j < wall.BoundingBox.Height / 64; j++)
                    {
                        var sector = PositionHelper.GetSectorAsVector(
                            new Vector2(wall.BoundingBox.X + i * 64 + 32, wall.BoundingBox.Y + j * 64 + 32));
                        Pathfinder.SetObstacle((int)Math.Floor(sector.X), (int)Math.Floor(sector.Y), true);
                    }
                }
            }
            Entities.AddRange(walls);

            var positions    = new List <Vector2>();
            var spanwerCount = 0;

            while (spanwerCount < 30)
            {
                var vector = new Vector2(
                    Random.Next(width) * SectorSize * 6 + (SectorSize / 2) + (SectorSize * 2) + 64,
                    Random.Next(height) * SectorSize * 6 + (SectorSize / 2) + (SectorSize * 2) + 64
                    );

                if (!positions.Contains(vector) && Vector2.Distance(vector, Player.Body.Position) > 640)
                {
                    spanwerCount++;
                    var spawner = new Spawner(vector);
                    Entities.Add(spawner);

                    for (int j = 0; j < 10; j++)
                    {
                        SpawnCrawlerAt(spawner);
                    }
                }
            }

            var healthCount = 0;

            while (healthCount < 30)
            {
                var x = Random.Next(0, SectorCountX);
                var y = Random.Next(0, SectorCountY);
                if (!Pathfinder.IsObstacle(x, y))
                {
                    var v = new Vector2(x * SectorSize - (SectorSize / 2), y * SectorSize - (SectorSize / 2));
                    if (!positions.Contains(v))
                    {
                        healthCount++;
                        var health = new Health(v);
                        Entities.Add(health);
                    }
                }
            }

            Assets.MiniMap = maze.CreateMazeTexture();

            State = GameState.Running;
        }