• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MrPlosion1243

How to Structure a Tile Based Game?

7 posts in this topic

I would consider myself a beginner programmer so I need help on how I should structure my tile based game. The game is a 2D Minecraft or Terraria type clone I'm developing for learning purposes.

I'm more concerned about how I'm going to make each tile. Should I have base class called "Tile" then have tiles like dirt, stone, wood derive from it? Then I could have a 2D array of "Tile" to be the world. A problem I see with that is there would be a lot of instances of the same type, I'm talking thousands because the worlds are going to be huge. Or should I just have one class called "World" and inside that have a 2D byte array where each byte represents a different tile. A problem with this however is I'll need to loop through each byte every time I wan't to do something like say check if the tile is collidable or not.

Could I receive some help on this?

Thanks.
0

Share this post


Link to post
Share on other sites
Don't derive tile types from a base class. You have a class/struct "Tile", and that class has some attributes.

At a basic level, it might have:[list]
[*]Pointer to its image (shared among other tiles)
[*]A flag for collidable or not
[*]...and a few others
[/list]
Your "world" is broken up into chunks. You only load each area when the player is within range of it. You [i]don't[/i] need to have the entire world loaded at one time.
Yes, each 'chunk' is a array of tiles. Maybe even an array of pointers to tiles, since you could probably share most the tile instances even - it depends on the game.

You don't need to loop through every tile each time you want to check for collision... you just find out where the player is and check the tiles immediately surrounding the player.

Have you made (completed, I should say) any game before this one? It might be a daunting task if you haven't ever completed something before. Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites
Thank you I will consider what you've said. I think I was just over thinking the whole thing and this seems a lot simpler than what I was doing.

Quick question though for really complex tiles like water or lava should they be their own class/struct maybe called "Liquid" or whatever?
0

Share this post


Link to post
Share on other sites
It depends on what kind of tile-based game you're making. Is it side-scrolling or overhead?

If overhead, your "Tile" class could look like this:
[list]
[*]Pointer to a vector of images (shared among other tiles)
[*]The current frame
[*]How much time left until the next frame
[*]A flag for collidable or not
[*]A flag for the type of noise that's made when walked on. ("Liquid", "Wood", "Stone", "Metal", etc...).
[*]A script to run every update tick when stood on (Like, "LavaDoDamage.script")
[*]...and a few others
[/list]

That allows for basic animated tiles (Nothing fancy, no splash effects or whatever, but sufficient for some games).
In general, don't go crazy with inheritance. Inheritance is a useful tool, but is not the solution to everything. Use it where needed, but prefer have things as member variables rather than inherited (but at the same time, don't be afraid to inherit if and when it's the better solution - just don't make it your default solution).

I suppose this also depends on what programming language you're using also... my advice comes from a C++ viewpoint, and other languages may differ.

How it's done really depends on the needs of the game. My WIP 2D turn-based tile-based RPG has multiple layers of tiles layered over each other, and I plan to have other special layers that aren't for images, but for things like what noise to make while walked on, or what script to trigger, separating the triggering of scripts and the playing sounds into a struct different from the ones dealing with the appearance of the tiles (because each tile in each layer doesn't need a sound, just one layer).
0

Share this post


Link to post
Share on other sites
I structured my own 2D tile map from tile types, which define all the attributes a tile has (image index to the image array, collision data, special flags, etc.).
The map itself is then a 2D array of indices to the tile types array.

This saves a whole lot of memory both while running the game, and on the disk, though the real intention was to make the map easily editable (I see editing a few tile types a whole lot easier than editing every single tile manually, but maybe that's just me).

To make empty tiles I just start the tile type indices at 1, so that every tile with value of 0 is to say "no tile type".

About collisions - you need only check the tiles around the player. You can get the exact tiles by converting your player coordinates to tile coordinates.

Suppose your player's coordinates are some point (x, y), and suppose every tile has the dimensions (w, h).
To instantly get what tile the player is on, simply divide the player's position by the tile size (x/w, y/h). This will result in the x/y indices of the tile.
For example, if the player's position is (200, 100) and the dimensions of each tile are (50, 50) then the player is on tile (4, 2).
That's converting your player's world space coordinates to tile space coordinates.

You should of course also take care of the player's dimensions. For example, if the above player's dimensions are also (50, 50), it is most likely that the player is intersecting with more than one tile, so take that into consideration.

I would say to just go with a physics engine like Box2D, but I really have no idea how optimal general physics engines are for uniform tile maps, which might have thousands to millions of tiles, yet are very, very efficient in terms of collision if done specifically for them.
You might want to check this out, as I've never yet used a physics engine.
While they are not meant specifically for uniform tile maps, they might have internal buckets (e.g. the well known quad tree) to filter out unneeded collisions, which are not as efficient as writing your own code, but are probably fast enough for you not to care. Edited by wolfscaptain
1

Share this post


Link to post
Share on other sites
If your game would be side scrolling then sometimes there would be empty tiles, like air...
To save memory and time of calculating you can use resizable array of pointers in current chunk which contains only solid tiles. To delete any tile from this array, for example
[CODE]
remove(i);
[/CODE]
Copy last pointer in your array to i and then resize array. Pretty simple and working :)
0

Share this post


Link to post
Share on other sites
[quote name='dEgle' timestamp='1338807084' post='4946064']
If your game would be side scrolling then sometimes there would be empty tiles, like air...
To save memory and time of calculating you can use resizable array of pointers in current chunk which contains only solid tiles. To delete any tile from this array, for example
[CODE]
remove(i);
[/CODE]
Copy last pointer in your array to i and then resize array. Pretty simple and working [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

That is good for the final representation, but for editing (if you support real time editing anyway), saving the full array is better, since otherwise you will keep deleting and allocating stuff with every change.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0