Beispiel #1
0
        /*************************************************
         * Load tiles from file and populate levelTiles
         *************************************************/
        public void LoadTiles(string fileStream)
        {
            // Load the level and ensure all of the lines are the same length.
            int width = 0;

            List<string> lines = new List<string>();
            using (StreamReader reader = new StreamReader(fileStream))
            {
                string line = reader.ReadLine();
                width = line.Length;

                while (line != null)
                {
                    lines.Add(line);
                    if (line.Length != width)
                        throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count));
                    line = reader.ReadLine();
                }
            }

            LEVEL_WIDTH = width;
            LEVEL_HEIGHT = lines.Count();
            levelTiles = new Tile[LEVEL_WIDTH, LEVEL_HEIGHT];
            camera.setGameScale();
            currentLevelName = allLevels.ElementAt(currentLevelNr - 1);
            int y = 0;
            foreach (string row in lines)
            {
                int x = 0;
                foreach (char aChar in row)
                {
                    //Block tile
                    if(aChar.ToString().Equals("#"))
                        levelTiles[x, y] = Tile.createBlocked();

                    //Empty tile
                    if (aChar.ToString().Equals("."))
                        levelTiles[x, y] = Tile.createEmpty();

                    //Trap I.E Disco tile
                    if (aChar.ToString().Equals("T"))
                        levelTiles[x, y] = Tile.createTrap();

                    //Player start position
                    if (aChar.ToString().Equals("1"))
                    {
                        levelTiles[x, y] = Tile.createEmpty();
                        player.setPlayerPosition(new Vector2(x, y));
                    }

                    //Exit tile
                    if (aChar.ToString().Equals("X"))
                        levelTiles[x, y] = Tile.createExit();

                    //Heart Tile/gem
                    if (aChar.ToString().Equals("L"))
                    {
                        levelTiles[x, y] = Tile.createEmpty();
                        Vector2 gemPos = new Vector2(x, y);
                        Gem aGem = new Gem(gemPos);
                        aGem.setGemType(Gem.GemType.Life);
                        gemList.Add(aGem);
                    }

                    // Enemy - Rasta
                    if (aChar.ToString().Equals("R"))
                    {
                        levelTiles[x, y] = Tile.createEmpty();
                        Vector2 enemyPos = new Vector2(x, y);
                        Enemy theEnemy = new Enemy(enemyPos, camera.getScaleX());
                        theEnemy.setEnemyType(Enemy.EnemyType.Rasta);
                        enemyList.Add(theEnemy);
                    }

                    // Enemy - HipHopper
                    if (aChar.ToString().Equals("H"))
                    {
                        levelTiles[x, y] = Tile.createEmpty();
                        Vector2 enemyPos = new Vector2(x, y);
                        Enemy theEnemy = new Enemy(enemyPos, camera.getScaleX());
                        theEnemy.setEnemyType(Enemy.EnemyType.HipHopper);
                        enemyList.Add(theEnemy);
                    }

                    // Enemy - Ghost
                    if (aChar.ToString().Equals("G"))
                    {

                        levelTiles[x, y] = Tile.createEmpty();
                        Vector2 enemyPos = new Vector2(x, y);
                        Enemy theEnemy = new Enemy(enemyPos, camera.getScaleX());
                        theEnemy.setEnemySpeed(new Vector2(2.0f * ((float)randMovement.NextDouble() / (float)1f) - 1.0f, (float)randMovement.NextDouble()));
                        theEnemy.setEnemyType(Enemy.EnemyType.Ghost);
                        enemyList.Add(theEnemy);
                    }

                    levelTiles[x, y].setTilePosition(new Vector2(x, y));

                    x++;
                }
                y++;
            }
        }