How should I structure my projectiles?

Started by
2 comments, last by MisterFuzzy 11 years, 5 months ago
I am working on a small project in which the player controls a 2-Dimensional Tank - the problem is, I'm not entirely sure on how I should structure my projectiles. I plan on having three different projectiles - bullets, missiles and lasers (not relevant, but whatevs).

My game's WORLD object holds a TILEMAP, and the TILEMAP holds TILE objects: That's no problem. But my WORLD also holds PLAYER and ENEMY objects, which in turn should hold PROJECTILE objects - but if I assign the PLAYER or ENEMY objects PROJECTILE objects, won't the PROJECTILE objects be destroyed when the PLAYER or ENEMY object that they belong to gets destroyed?

"Only idiots quote themselves" - MisterFuzzy

Advertisement
You've already pointed out the error of objects owning their own projectiles. What you need is an object that manages the lifetimes of said projectiles. Think "projectile manager".
Just because a game character provides the source position of a projectile doesn't mean the character needs to own the projectile. Once the projectile exists, it lives in the world just like all other entities. Assuming the the World is what manages characters interacting with the environment and with each other, the World should also manage the same interactions for projectiles.

What you need is an object that manages the lifetimes of said projectiles.
[/quote]
Yeah, I started thinking about that after I posted my initial question (derp). But on that subject, I would need a seperate class(?) to handle each individual projectile, updating each one as they occur in a list(?) of projectile objects. The tutorials I ran through never actually discussed destroying an object (even though there was a section on making a hundred DrawableGameComponents run around on the screen), so I have no idea how to make an object "self destruct" if they hit an obstacle or non-owner (and I use the term owner loosely: I am referring to the value I mentioned earlier that tells other objects what object launched the projectile, so it doesn't hurt friendlies or the player's own tank)... I imagine I could figure something out, but for the sake of education I'd rather know the correct way to go about this. This is my first foray into the world of OOP, so my past procedural code knowledge is sorta useless...wacko.png The whole idea of "objects" is pretty new to me, and still difficult for me to fully grasp...

"Only idiots quote themselves" - MisterFuzzy

This topic is closed to new replies.

Advertisement