Beispiel #1
0
        /// <summary>
        /// Loads the components to this playable scene, and loads a new player instance.
        /// </summary>
        /// <param name="game">Current game instance.</param>
        /// <param name="mapPath">String path of the map being read from directory.</param>
        private void LoadMap(Game game, string mapPath)
        {
            //Create new player object and add to scene components
            Player = new Player(game);
            GameObjects.Add(Player);

            //Default position is (1,1) --> (64, 64)
            var yCount = 1;
            var xCount = 1;

            MapBuilder builder = new MapBuilder(game, _texture, TextureDictionary);

            //Reads text map file line by line
            using (StreamReader rdr = new StreamReader(File.OpenRead(mapPath)))
            {
                while (rdr.Peek() > 0)
                {
                    var row = rdr.ReadLine();

                    //Foreach character in the read line, build the game object it represents (See 'MapBuilder.cs')
                    foreach (var c in row.ToCharArray())
                    {
                        var gameObject = builder.BuildGameObject(c);
                        if (gameObject != null)
                        {
                            //Each character represents it's location times 64 (Since tiles are assumed 64x64)
                            gameObject.X = 64 * xCount;
                            gameObject.Y = 64 * yCount;
                            GameObjects.Add(gameObject);
                        }
                        xCount++;
                    }
                    xCount = 1;
                    yCount++;
                }
            }
        }