public TileMap Generate(MapInformation mapInformation, Camera camera)
        {
            Random random = new Random();

            //Create map
            List<MapLayer> mapLayers = new List<MapLayer>();

            //Ground
            MapLayer ground = new MapLayer(mapInformation.TileWidth, mapInformation.TileHeight);
            for (int y = 0; y < ground.Height; y++)
                for (int x = 0; x < ground.Width; x++)
                {
                    int grassTile = random.Next(0, 2);
                    ground[x, y] = new Tile(grassTile, 0);
                }

            //Decoration
            MapLayer decoration = new MapLayer(40, 40);
            for (int i = 0; i < mapInformation.NumberOfDecorativeObjects; i++)
            {
                int x = random.Next(0, 40);
                int y = random.Next(0, 40);
                //TODO ADD CHECK SO NO OVERWRITE
                int tileIndex = random.Next(3, 15);

                if(tileIndex == 14) //Add tree
                {
                    Tile treeTile = new Tile(tileIndex, 0); // tileSet: 0
                    if (y > 0)
                        decoration[x, y-1] = treeTile;
                    tileIndex = 15; //to get the foot below
                }

                Tile tile = new Tile(tileIndex, 0); // tileSet: 0
                decoration[x, y] = tile;
            }

            mapLayers.Insert(0, ground);
            mapLayers.Insert(1, decoration);

            //Add player position
            //TODO make sure it is created along water at the sides
            int playerX = random.Next(1, (mapInformation.TileWidth * 32)-32); //TODO have better calc
            int playerY = random.Next(1, (mapInformation.TileHeight * 32)-32); //TODO have better calc
            camera.Position = new Vector2((float)150, (float)150);
            //camera.Position = new Vector2((float)playerX, (float)playerY);

            //Create enemies
            foreach (EnemyInformation ei in mapInformation.Enemies)
            {
                Enemy enemy = new Enemy(game);                          //TODO dont add enemies to close to the player
                int enemyX = random.Next(1, (mapInformation.TileWidth * 32) - 32); //TODO have better calc
                int enemyY = random.Next(1, (mapInformation.TileWidth * 32) - 32); //TODO have better calc
                enemy.Create(ei, new Vector2((float)enemyX, (float)enemyY));
                enemies.Add(enemy);
            }

            return new TileMap(camera.Game, mapInformation.TileSets, mapLayers);
        }
Beispiel #2
0
        public GameScene(Game game)
            : base(game)
        {
            // Initialize map settings
            camera = new Camera(game, new Rectangle(0, 0, 800, 480)); // TODO: Fix this so it is not hardcoded -> GraphicsDevice is not initialized at this point, need to wrap it somehow (perhaps add it as a service) so the camera will access it later when it's initialized

            MapInformation mapInformation = new MapInformation(Game);
            mapInformation.LoadMapFromXML("test map");
            map = MapGenerator.Generate(mapInformation, camera);

            Font gameTitle = new Font(game, "Generated map", new Vector2(0, 0), Color.Black);

            crossControl = new CrossControl(game);

            // Player
            player = new Player(game, camera);
            SceneComponents.Add(map);
            SceneComponents.Add(gameTitle);
            SceneComponents.Add(player);
            SceneComponents.Add(crossControl);
        }