Jump to content
  • Advertisement
Sign in to follow this  
EmrldDrgn

Multiple Tile Types and Pointers - how to update?

This topic is 3803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Wow, that's a totally unhelpful title... wish I could think of a better one. So here's the deal. In my tile-based game, I want to have some tiles which are static (grass, maybe trees) and some tiles which are dynamic (water with waves, lava that... lavas?), but I want to store them in the same map structure (probably a std::vector< std::vector < > >). I can't really figure out how to do this, however. If I had a vector of vectors of StaticTile(s), I could only have static tiles. If I had a vector of vectors of DynamicTile(s), I could only have dynamic ones. Now, my idea was to have a vector of vectors of Tile*(s), which would point to either a StaticTile or DynamicTile. However, I'd want to call an Update() function on DynamicTiles to change the frame of the animation. Since StaticTiles do not contain this function, I'd crash and burn (right?). I suppose I could do something like this:
void Update()
{
    return;
}
for StaticTile, but wouldn't that just be a waste of time? Anyone with a better solution, please, let me know! This shouldn't be this hard...

Share this post


Link to post
Share on other sites
Advertisement
Here's the stuff off the top of my head, which may or may not be better solutions

1) If you have to manually render the tiles(i.e. like OpenGL in C++), then you could have the render() function handle updating too. However, since I don't know what kind of environment you're programming in, this may not be an option (like in Java 3D, all the rendering is done automatically, which means you don't have a render() function).

2) If each tile has synchronized animation (like the water in Warcraft 3, each water tile plays the same frame), then you could have some static variables that some update() function could edit, and since they are static you wouldn't have to iterate through your tiles and update each one. You could just update each type of tile instead of each tile itself.

3) Like you said, you could have two separate data structures, one for static and one for dynamic tiles. This would work, but I personally don't like having to maintain two lists, so I agree that there is a better solution.

4) Well, that's about it. There are a couple more that I can think of but they would be ugly implementations and require more memory, so I'm not even going to bother with listing them.

Share this post


Link to post
Share on other sites
Don't make it too hard. I'm assuming that you're using a sub-class for each tile? Just make your update() virtual, and have it empty in the base class. Only "dynamic" tiles need override it. Empty functions are no problem, and static tiles never touch update().

Share this post


Link to post
Share on other sites
It doesn't matter that a StaticTile does nothing.

class TileBase
{
public:
virtual void Update (double time_delta) = 0;
virtual void Render (void) = 0;
};

// Unanimated/static tile implementation
class StaticTitle : public TileBase
{
public:
void Update (double time_delta) { }
void Render (void) { draw (m_image); }
};

// Animated/dynamic tile class implementation
class AnimatedTitle : public TileBase
{
public:
void Update (double time_delta) {
if (time_delta > m_frametime)
++m_frame;
}
void Render (void) { draw (m_image[m_frame]); }
};



int main ()
{
std::vector <TileBase*> tiles;
tiles.push_back (new StaticTile ("road", 1, 1));
tiles.push_back (new AnimatedTile ("water", 2, 1));

for (size_t i = 0; i < tiles.size (); i++)
tiles->Update (0.2);
}



Remember to delete your tiles in the vector.

Share this post


Link to post
Share on other sites
Aargh. Never mind all that. Simplify, and generalize.

A "dynamic" tile animates in a loop, yes? So, represent "static" tiles with a "loop" of animation, which just contains the one frame of animation. It will loop over and over, and thus always look the same. There you go. Now you only have one tile type, and no code design problems.

To avoid drawing the image repeatedly for static tiles, we could add code to the drawing routine which checks if the sprite ID is the same this render as the last. It doesn't need to think about why that might be the case. (After all, a "dynamic" tile might want to pause for a few frames at some point in the animation.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!