Expanding players model, combining multiple rects/clips

Started by
1 comment, last by jdean300 10 years, 2 months ago

My current project is a pixel themed shoot-em-up and a feature I have been thinking of for a while is an upgrade system for the players ship running like so:

-Players wins a level, earning income along the way

-Player transitions to an upgrade menu/shop

-Player can buy additions to his ship, extending ships dimensions, but bringing in new abilities

So lets say the players initial model is a 20x20 square, he can buy another square, lets say it boosts ship speed, and choose where on his model to place the ship (top, bottom, left, right), in a tile format

How would I implement something like this?

The relevant structure of my program:

I have a PlayerShip class extending the Ship class. It has a SDL_Rect defining its dimensions, and a pointer to a clip used for rendering. I have a SDL_Texture wrapper class that provides a Render function that takes x, y, clip, and rotation values, then renders with SDL_RenderCopyEx.

I tried to create an add_clip function which took a clip to add and a coordinate relative to the main clip location. I created a struct that held a clip and an x and y value for each clip, then created a vector of these, adding new clips to this vector as needed, and updating the PlayerShip dimensions as a rectangle enclosing all added clips. However, once I started expanding my other ship functions ( move, render, and collision detection ) this was quickly becoming cumbersome because of having to handle each added piece almost like an added ship, and updating each of their positions and attributes seperately.

Is this the right direction to take? Is there some way to set a rect to render that is comprised of the other rects, without having to iterate through and render them seperately?

Advertisement

Use a tree structure. The main part of the ship would be the tree root node. Adding a bit on makes a child of the root (or if it is attached to a child piece it becomes a child of the child node it is attached to). Store the relative offset of the child node to the parent.

When the ship moves update the root node and then the root node recursively updates the positions of the child nodes.

Some advantages of that approach:

1) You can make the child nodes do more exciting things than just having a fixed offset from the parent (e.g. they could orbit around the parent node if they liked)

2) You can do collision on the tree of nodes instead of building a bounding box for the whole ship, so if you build an L shaped ship the collision would be better than enclosing the L shape in a rectangular collision object

3) Expanding the above you could have bits knocked off the ship by collisions

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Thanks! I've never made a tree structure before, but after googling some information it definitely sounds like what I'm looking for. I found some decent information on them, but is there some resource/tutorial that stands out as something I should check out?

This topic is closed to new replies.

Advertisement