Page 1 of 4
Tile Engine question
Posted: Mon Mar 22, 2010 10:02 pm
by mattheweston
I'm building a tile Engine for a game I'm working on and had a question.
I'm looking at having a Tile class and a map class.
The map class would be a 2D array of Tiles and the tile class would have information about a "Tile" including an array that would hold the information for each layer for that tile.
In a game I would have an array of "map"s to build a level.
Currently, I'm looking for feedback on this design. What do you guys think?
Re: Tile Engine question
Posted: Mon Mar 22, 2010 10:13 pm
by GroundUpEngine
Great design imo, I have used the exact same thing for 2 of my first games
Re: Tile Engine question
Posted: Mon Mar 22, 2010 10:19 pm
by XianForce
so like:
Code: Select all
class Map
{
Tile** tiles;
};
class Tile
{
//Other tile stuff...
Layer* layer
};
?
If that's along the lines of what you were thinking, I think that's a tad bit flawed.
Typically when you have different layers, you'll probably have like a ground layer, a couple object layers, maybe an item layer, etc.
So if any of these share the same (x, y) position, yet different layers, they will probably all need to hold different information to define itself.
So if a tile has an array that defines all the spaces in that (x, y) space through a number of layers, things will become a bit more complicated when two of those spaces in the layer aren't the same (and if they are the same, layers become useless...).
In my level editor / tile engine. I have maps defined as a dynamically allocated 3D array. So, I have width, height, and layers, and each one is a different tile. This way I can store information about each individual piece.
Sorry if my post seems a little confusing, but I hope it helps.
EDIT: After thinking about what I posted, perhaps I interpreted what you said a little wrong, did you perhaps mean something more like this:
Code: Select all
class Map
{
Tile** tiles;
Tile* tileLayers;
};
class Tile
{
//Tile info
};
? Because that seems like a great way to do it =D
Re: Tile Engine question
Posted: Mon Mar 22, 2010 10:41 pm
by mattheweston
I see what you are saying. I started with the 3D array concept but didn't want to get things too complex. =)
The gist would be to use a Tile to have a ground layer(grass, road, caves, etc) then have a layer for objects(treasure chests, etc)
I would have say an overworld "level" that would be similar to a "screen" in a game like zelda this would be a 2D array.
then the coordinate x,y in that array would be a "map" which in and of itself would be a 2D array of TIles.
That was the initial thought; however, I think once difference would be that in this design you would almost have to do it like The Legend of Zelda in order to only have one "Map" on the screen at once. If you used a 3D array you would be able to do more of a smooth scrolling around the world.
My inital problem was trying to think of a way to work on a "level" that goes beyond the bounds of a screen in a level editor.
Then again I really haven't put TOO much tought into this. =)
Re: Tile Engine question
Posted: Mon Mar 22, 2010 10:49 pm
by XianForce
mattheweston wrote:
My inital problem was trying to think of a way to work on a "level" that goes beyond the bounds of a screen in a level editor.
Well if you still haven't found a solution to this, here's my two cents:
Create a camera class, and only load the tiles that would be on screen. So with a few calculations, you can find where the top left corner of the game window should be, and from there, it's easy to find the rest of the rectangle. So you'd start with the tiles that would be at that (x, y) coordinate in the map, and tile until you reach the screen width and height.
Re: Tile Engine question
Posted: Mon Mar 22, 2010 10:58 pm
by mattheweston
yeah I was going to have a variable for storing the top left corner of the tiles on screen. This is probably really easy stuff. I just have to think it through and not make it out to be harder than it is. =)
Re: Tile Engine question
Posted: Mon Mar 22, 2010 11:09 pm
by XianForce
mattheweston wrote:yeah I was going to have a variable for storing the top left corner of the tiles on screen. This is probably really easy stuff. I just have to think it through and not make it out to be harder than it is. =)
Well, I'd say you should at least have a rectangle to hold the top left corner, width, and height, instead of only the top left corner
Re: Tile Engine question
Posted: Mon Mar 22, 2010 11:14 pm
by mattheweston
Yes, I'll have a height and width as well. Not sure if I'll have two vector2D vars or one Rectangle yet.
Rectangle would make more sense.
Re: Tile Engine question
Posted: Tue Mar 23, 2010 11:21 am
by RyanPridgeon
XianForce wrote:so like:
Code: Select all
class Map
{
Tile** tiles;
};
class Tile
{
//Other tile stuff...
Layer* layer
};
This looks absolutely fine.
This is the way I would do it, although maybe not have a seperate Layer object for every layer, as some layers (like collision) might only need something as simple as a char value, and others might need much more complex information. Unless Layer is an abstract base type which would work fine, as long as you're not going too overboard on OOP.
XianForce wrote:
Code: Select all
class Map
{
Tile** tiles;
Tile* tileLayers;
};
class Tile
{
//Tile info
};
? Because that seems like a great way to do it =D
This makes little sense.
Re: Tile Engine question
Posted: Tue Mar 23, 2010 9:06 pm
by XianForce
RyanPridgeon wrote:XianForce wrote:so like:
Code: Select all
class Map
{
Tile** tiles;
};
class Tile
{
//Other tile stuff...
Layer* layer
};
This looks absolutely fine.
This is the way I would do it, although maybe not have a seperate Layer object for every layer, as some layers (like collision) might only need something as simple as a char value, and others might need much more complex information. Unless Layer is an abstract base type which would work fine, as long as you're not going too overboard on OOP.
XianForce wrote:
Code: Select all
class Map
{
Tile** tiles;
Tile* tileLayers;
};
class Tile
{
//Tile info
};
? Because that seems like a great way to do it =D
This makes little sense.
Yeah I realized I effed that up haha...
The second thing I posted is most definitely NOT the way to do it. I had a little brain malfunction there haha.
But the first way... What happens when you need information for a specific tile? If you have a 2D array of tiles, that accounts for the (x, y), but if the tiles hold something to represent each tile in that layer, that becomes a problem because they likely need to have different properties, and have all the properties any tile would. So the only way I could think that it would have the same properties, is if you made it another array of tiles within the tile class... which wouldn't really work, as it would be unending, right?
And just to fix the second one, what I probably meant to put was something like:
Code: Select all
class Map
{
Tile** tiles1;
Tile** tiles2;
//continue for amount of layers
};
But that's probably not great style.
But here's another suggestion:
Code: Select all
class Layer
{
Tile** tiles
};
class Map
{
std::vector<Layer> layers;
};
That would probably work well, but I imagine there would be some nasty overhead with the vector.
But then again, what do I know? I'm a nub
Re: Tile Engine question
Posted: Tue Mar 23, 2010 9:31 pm
by avansc
since you are so pointer happy,
why not just go, Tile ***tiles; ?
Re: Tile Engine question
Posted: Tue Mar 23, 2010 9:41 pm
by mattheweston
All the C++ examples are great but, pointers won't be a problem as I'll be using C# and XNA. =)
Re: Tile Engine question
Posted: Tue Mar 23, 2010 10:01 pm
by XianForce
mattheweston wrote:All the C++ examples are great but, pointers won't be a problem as I'll be using C# and XNA. =)
Oh, oops >.>
Re: Tile Engine question
Posted: Wed Mar 24, 2010 12:08 am
by mattheweston
It's all good. I think I got things figured out now.
Re: Tile Engine question
Posted: Wed Mar 24, 2010 3:48 am
by LeonBlade
avansc wrote:since you are so pointer happy,
why not just go, Tile ***tiles; ?
Better yet...
...seems to be the best route to take