Exemplo n.º 1
0
        public void ChangeTile(PointF location) {
            //new value is used to find source rect for textures only
            int yTile = (int)location.Y / Game.TILE_SIZE;
            int xTile = (int)location.X / Game.TILE_SIZE;
            int xPos = ((int)location.X / Game.TILE_SIZE) * Game.TILE_SIZE;
            int yPos = ((int)location.Y / Game.TILE_SIZE) * Game.TILE_SIZE;
            //new value holds the tile value of what it turns into
            int oldValue = tileMap[yTile][xTile].TileValue;
#if TILEDEBUG
            Console.WriteLine("Tile old value: " + oldValue);
#endif
            tileMap[yTile][xTile].Destroy();

            tileMap[yTile][xTile] = new Tile(tileSheet, spriteSources[breakableTiles[oldValue]]);
#if TILEDEBUG
            Console.WriteLine("Source rect: " + spriteSources[breakableTiles[oldValue]]);
#endif
            tileMap[yTile][xTile].TileValue = breakableTiles[oldValue];
            mapFormat[yTile][xTile] = tileMap[yTile][xTile].TileValue;
#if TILEDEBUG
            Console.WriteLine("Tile new value: " + tileMap[yTile][xTile].TileValue);
#endif
            tileMap[yTile][xTile].Walkable = true;
            foreach (int w in unwalkableTiles) {
                if (mapFormat[yTile][xTile] == w) {
                    tileMap[yTile][xTile].Walkable = false;
                }
            }
            for (int k = 0; k < breakableTiles.Count; k++) {
                int b = tileMap[yTile][xTile].TileValue;
                tileMap[yTile][xTile].Breakable = breakableTiles.ContainsKey(b);

            }
#if TILEDEBUG
            Console.WriteLine("Tile Location PreAdjustment, X: " + tileMap[yTile][xTile].WorldPosition.X + " , Y: " + tileMap[yTile][xTile].WorldPosition.Y);
#endif
            tileMap[yTile][xTile].WorldPosition = new Point(xPos, yPos);
#if TILEDEBUG
            Console.WriteLine("Tile Location PostAdjustment, X: " + tileMap[yTile][xTile].WorldPosition.X + " , Y: " + tileMap[yTile][xTile].WorldPosition.Y);
#endif
            tileMap[yTile][xTile].Scale = 1.0f;
            //add score
            Score += 50;
        }
Exemplo n.º 2
0
        public Map(string mapPath, PlayerCharacter hero) {
            if (System.IO.File.Exists(mapPath)) {
                unwalkableTiles = new List<int>();
                List<int> doorIndex = new List<int>();
                spriteSources = new Dictionary<int, Rectangle>();
                mapFormat = new List<List<int>>();
                Dictionary<int, string> nextMap = new Dictionary<int, string>();
                nextRoom = new Dictionary<string, Point>();
                breakableTiles = new Dictionary<int, int>();
                enemies = new List<EnemyCharacter>();
                Items = new List<Item>();
                breakFX = new List<BreakEffect>();
                //load map
                using (TextReader reader = File.OpenText(mapPath)) {
                    string contents = reader.ReadLine();
                    while (contents != null) {
                        string[] content = contents.Split(',');
                        if (content.Length == 1) {
                            content = contents.Split(' ');
                        }

                        //Console.WriteLine(content[0]);

                        //load texture
                        if (content[0] == "T") {
                            string path = content[1];
                            tileSheet = path;
#if LOADMAPDEBUG
                            Console.WriteLine("Texture Path: " + tileSheet);
#endif
                        }
                        //load source rects
                        else if (content[0] == "R") {
                            //source rect
                            Rectangle r = new Rectangle(System.Convert.ToInt32(content[2]), System.Convert.ToInt32(content[3]), System.Convert.ToInt32(content[4]), System.Convert.ToInt32(content[5]));
                            //adds rect index and source rect to dictionary
                            spriteSources.Add(System.Convert.ToInt32(content[1]), r);
#if LOADMAPDEBUG
                            Console.WriteLine("Rectangle Added: " + r);
#endif
                        }
                        //figure out walkable tiles
                        else if (content[0] == "U") {
                            for (int i = 1; i < content.Length; i++) {
                                unwalkableTiles.Add(System.Convert.ToInt32(content[i]));
#if LOADMAPDEBUG
                                Console.WriteLine("Unwalkable Tiles: " + content[i]);
#endif
                            }
                        }
                        //door Tiles
                        else if (content[0] == "D") {
                            //identifies which tile is a door
                            doorIndex.Add(System.Convert.ToInt32(content[1]));
                            //door destination
                            nextRoom.Add(content[2], new Point(System.Convert.ToInt32(content[3]), System.Convert.ToInt32(content[4])));
                            //door spawn destination
                            nextMap.Add(System.Convert.ToInt32(content[1]), content[2]);
#if LOADMAPDEBUG
                            Console.WriteLine("Door tile index: " + content[1]);
                            Console.WriteLine("Next room path: " + content[2]);
                            Console.WriteLine("Door spaw location: " + content[3] + "," + content[4]);
#endif
                        }
                        //starting tile
                        else if (content[0] == "S") {
                            SpawnTile = new Point(System.Convert.ToInt32(content[1]), System.Convert.ToInt32(content[2]));
#if LOADMAPDEBUG
                            Console.WriteLine("Starting tile: " + content[1] + ", " + content[2]);
#endif
                        }
                        //add enemies
                        else if (content[0] == "E") {
                            if (enemies == null) {
                                enemies = new List<EnemyCharacter>();
                            }
                            bool upDownMove = content[3] == "X" ? false : true;
                            if (content[1] == "G") {
                                enemies.Add(new Goomba(content[2], upDownMove));
                            }
                            else if (content[1] == "K") {
                                enemies.Add(new Koopa(content[2], upDownMove));
                            }
                            enemies[enemies.Count - 1].Position.X = System.Convert.ToInt32(content[4]) * Game.TILE_SIZE;
                            enemies[enemies.Count - 1].Position.Y = System.Convert.ToInt32(content[5]) * Game.TILE_SIZE;
                            if (content[6] == "R") {
                                enemies[enemies.Count - 1].Direction = 1;
                            }
                            else if (content[6] == "L") {
                                enemies[enemies.Count - 1].Direction = -1;
                            }
#if LOADMAPDEBUG
                            Console.WriteLine("Enemy added, Y Axis Movement: " + upDownMove);
                            Console.WriteLine("Enemy type: " + content[1]);
                            Console.WriteLine("Enemy sprite path: " + content[2]);
                            Console.WriteLine("Enemy starting location: " + content[4] + ", " + content[5]);
#endif

                        }
                        else if (content[0] == "//" || content[0] == " ") {
                            //used to make comments in txt file!
                        }
                        //which tiles are breakable
                        else if (content[0] == "B") {
#if LOADMAPDEBUG
                            Console.WriteLine("Breakable dict length: " + (content.Length - 1));
#endif
                            for (int i = 1; i < (content.Length - 1) / 2 + 1; i++) {
                                breakableTiles.Add(System.Convert.ToInt32(content[i]), System.Convert.ToInt32(content[content.Length - i]));
                                //Console.WriteLine("Breakable tile: " + System.Convert.ToInt32(content[i]) + " turns into: " + System.Convert.ToInt32(content[content.Length - i]));
                                //Console.WriteLine("i: " + i + ", content length-i: " + (content[content.Length - i]));
                            }
                        }
                        //add items to map
                        else if (content[0] == "I") {
                            Item.ItemSheet = content[1];
#if LOADMAPDEBUG
                            Console.WriteLine("Item sheet: " + content[1]);
#endif
                        }
                        //load rows
                        else if (System.Convert.ToInt32(content[0]) >= 0) {
                            //create new row
                            mapFormat.Add(new List<int>());
                            for (int i = 0; i < content.Length; i++) {
                                //add numbers to row
                                if (string.IsNullOrEmpty(content[i])) {
                                    continue;
                                }
                                mapFormat[mapFormat.Count - 1].Add(System.Convert.ToInt32(content[i]));
                            }
#if LOADMAPDEBUG
                            Console.WriteLine("Row created");
#endif
                        }
                        contents = reader.ReadLine();
                    }
                }
                //create map
                int rows = mapFormat.Count;
                int cols = mapFormat[0].Count;
                tileMap = new Tile[rows][];
                for (int i = 0; i < rows; i++) {
                    tileMap[i] = new Tile[cols];
                    //create individual tile
                    for (int j = 0; j < cols; j++) {
                        //mapFormat[i][j] = individual tile
                        Rectangle source = spriteSources[mapFormat[i][j]];
                        Point worldPosition = new Point();
                        worldPosition.X = (j * source.Width);
                        worldPosition.Y = (i * source.Height);

                        tileMap[i][j] = new Tile(tileSheet, source);
                        tileMap[i][j].Walkable = true;
                        //assign tile values
                        tileMap[i][j].TileValue = mapFormat[i][j];
                        //check if it's a door
                        for (int k = 0; k < doorIndex.Count; k++) {
                            tileMap[i][j].IsDoor = mapFormat[i][j] == doorIndex[k] ? true : false;
                            if (tileMap[i][j].IsDoor) {
                                break;
                            }
                        }
                        //assign doorpath if it's a door
                        if (tileMap[i][j].IsDoor) {
                            tileMap[i][j].DoorPath = nextMap[mapFormat[i][j]];
                        }
                        //breakable?
                        for (int k = 0; k < breakableTiles.Count; k++) {
                            int b = tileMap[i][j].TileValue;
                            //check to see if the tile value is in the list of breakable values
                            tileMap[i][j].Breakable = breakableTiles.ContainsKey(b);

                        }
                        tileMap[i][j].WorldPosition = worldPosition;
                        tileMap[i][j].Scale = 1.0f;
                        //check if tile is walkable
                        foreach (int w in unwalkableTiles) {
                            if (mapFormat[i][j] == w) {
                                //txt file indexed unwalkable tiles
                                tileMap[i][j].Walkable = false;
                            }
                        }
                        //assign items to tile
                        if (tileMap[i][j].TileValue == 33) {
                            tileMap[i][j].Item = "GrowMushroom";
                        }
                        else if (tileMap[i][j].TileValue == 34) {
                            tileMap[i][j].Item = "Star";
                        }
                        else if (tileMap[i][j].TileValue == 35) {
                            tileMap[i][j].Item = "OneUp";
                        }
                        else if (tileMap[i][j].TileValue == 36) {
                            tileMap[i][j].Item = "Coin";
                        }
                        else if (tileMap[i][j].TileValue == 37) {
                            tileMap[i][j].Item = "FireFlower";
                        }
                        else if (tileMap[i][j].TileValue == 3) {
                            tileMap[i][j].Item = "Coin";
                        }/*
                        
                        */
                    }
                }
                //set hero position
                hero.Position.X = SpawnTile.X * Game.TILE_SIZE;
                hero.Position.Y = SpawnTile.Y * Game.TILE_SIZE;                

#if LOADMAPDEBUG
                Console.WriteLine("Map has been loaded!");
#endif
            }
            else {
                Console.WriteLine("Map not found!");
            }
        }