Jump to content

  • Log In with Google      Sign In   
  • Create Account





The tiled beginning of my journey

Posted by Truerror, 16 April 2014 · 402 views

tile beginner xna
The tiled beginning of my journey It's been a while since I first registered at Gamedev.net, and I've taken advantage of some of its features, notably the forums and the hobbyist projects listing. The journey to become a full-fledged game developer is arduous, and full of obstacles. That said, it's also enjoyable. I've had lots of a-ha moments that is simply too fond to let go, and therefore had been thinking about starting my own blog. However, I never get around to actually doing it.

Then I came across this feature, and thought "Why not use it in lieu of a blog? It's not like I need the things that a full-featured blog have, I just want to have a personal record of what I've done and want to do next." Thus, this journal.

My first entry would be an experiment on tiles, in XNA. Its' nothing fancy, just a simple parser so that I can better understand the innner workings of a tile-base game. If I can manage this, then perhaps I can make something like the Super Robot Wars. It's good to keep those dreams alive, right? So, on to the technical stuff.

The first thing that popped up in my mind when I thought about 'tiles' was a 2D array, so I based my approach on that, and proceeded to make a Tile class to populate the array. The class has the position and the texture of the tile. I also made an enum for the tile types and some very simple images (20x20 at the moment, I'm not that good at drawing things) to represent each type. For now, I have three types, plains, mountains, and water. Then, while building my Tile class, I realized that I'd need some sort of test map to test it with. Most probably loaded from a text-based file. That means, I'd need some sort of parser for the text file. Now there's a problem.
        public void BuildMap()
        {
            for (int x = 0; x < this._dimension.X; x++)
            {
                for (int y = 0; y < this._dimension.Y; y++)
                {
                    switch (_mapArray[y, x])
                    {
                        case "A":
                            tileArray[y, x] = new Tile(_contentManager, TileType.Plains);
                            break;
                        case "B":
                            tileArray[y, x] = new Tile(_contentManager, TileType.Mountain);
                            break;
                        case "C":
                            tileArray[y, x] = new Tile(_contentManager, TileType.Water);
                            break;
                    }
                    tileArray[y, x].position = new Vector2(x * 20, y * 20);
                }
            }
        }

I must admit that I'm not a very proficient programmer, and thus, I have never parsed text from a file before, though I knew how to read from a file. Through trial and error, I finally managed to hack a parser class, which responsibility is to read the map file, actually map its content (which is string) to a 2D array, and finally convert the 2D array of strings into a 2D array of Tiles. As you can see from the code snippet above, it wasn't the best code ever Posted Image But it gets the job done. The texture for each tiles is decided at the time of its creation, along with their positions. Then, after I'm done populating the 2D array with tiles, I finally drew them. The end result can be seen in that picture above, with the game screen on the right, showing the tiles, and the very simple map file on the left. Granted, it's not pretty. But at least it's a start.

I'll write another update when I get more things done. Next would be adding a game object in there. Hope I can do it. Posted Image




Nice work. Code looks perfectly suited to task to me.

Thanks. I'm trying to implement movement right now. Maybe I'll write about it tomorrow. That is, if I can get it to work by tomorrow.

That's good, I had similar code for a tile map I did, I even had an enum called TileType. One bit of refactoring you could do is to put the char to type conversion into it's own static method in the Tile class, then the BuildMap method becomes shorter and more focused on map building - also you then only have one place where the Tile constructor is called so if in future you need to add a new parameter to that constructor you only have to change it in one place.

 
TileType tileType = Tile.CharToType(_mapArray[y, x]);
tileArray[y, x] = new Tile(_contentManager, tileType);

 

That's good, I had similar code for a tile map I did, I even had an enum called TileType. One bit of refactoring you could do is to put the char to type conversion into it's own static method in the Tile class, then the BuildMap method becomes shorter and more focused on map building - also you then only have one place where the Tile constructor is called so if in future you need to add a new parameter to that constructor you only have to change it in one place.

 
TileType tileType = Tile.CharToType(_mapArray[y, x]);
tileArray[y, x] = new Tile(_contentManager, tileType);

 

 

Indeed :)

 

I realized it almost as soon as I finished writing that code (I had been browsing the net while writing code) that the string class has a method to convert it to a char array. If I had done that, not only would it shorten the code, it would also simplify map making (right now, I'm doing it by hand, haven't build a tool for that yet). I've put it in my To-Do list, and I'll do it when the game is feature-complete. Then would be the time of major overhaul. Thanks for your suggestion, appreciate it. :)

Twitter

December 2014 »

S M T W T F S
 123456
78910111213
1415161718 19 20
21222324252627
28293031   

Recent Entries

Recent Comments

PARTNERS