Design Question

Started by
17 comments, last by superpig 15 years, 11 months ago
In my current game project I have a bullet object. In some cases I would like to be able to split the object into multiple bullets, sort of like a delayed tri-shot sort of thing. This brings up two design questions. 1. Is this bullet type worthy of a new class, or should I simply add the code to the existing bullet class? 2. How should I implement keeping track of these bullets? My game class has vectors contain the various object types. Tanks, bullets, and explosions. I can't think of a graceful way to add more bullets to the bullet-vector in the game class. Any help would be very appreciated! Thanks. You can also talk to my on AIM or GTalk AIM:ZackGomez GTalk:starruler@gmail.com -edited to add IM handles-
Advertisement
You don't want to actually split the object (bullet) to create 3 separate bullets. Instead, you want to create 3 separate and independent bullets. Wherever the code is that creates and fires the bullets, you want to edit that to create 3 bullets instead of only 1. Then, calculate the trajectories of each bullet to be displaced from the previous bullet by a small amount.

Quote:
1. Is this bullet type worthy of a new class, or should I simply add the code to the existing bullet class?


No and no. You should have a Bullet class that does only 1 thing: simulates a Bullet. Bullets don't split into more bullets, so neither should your Bullet class.

Quote:
2. How should I implement keeping track of these bullets? My game class has vectors contain the various object types. Tanks, bullets, and explosions.


By simply creating 3 Bullets instead of 1 when you fire the weapon you don't need to change your data structures; all you're doing is adding more Bullets to the data structures.

Quote:
I can't think of a graceful way to add more bullets to the bullet-vector in the game class.



void GameClass::addBullet(Bullet b){
bullet_vector.push_back(b);
}


EDIT:
Quote:
...sort of like a delayed tri-shot...


Do you actually mean delayed? As in you fire a single shot from your weapon that travels for 30 yards and then splits into multiple shots that then continue to travel away?
Yeah I do mean delayed. Like when the projectile gets to its maximum height it splits into three different projectiles with different x-velocity components.
As for part 1 I'll subclass the projectile into a SplittingBullet or something similar when I figure out how to implement it.
It sounds like maybe you do want a subclass of your Bullet class called SplittingBullet or something. SplittingBullet would only override one of Bullet's functions, the "update" function responsible for doing the bullet's logic every frame.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by starruler
>two design questions.
>1. Is this bullet type worthy of a new class, or should I simply add the code to the existing bullet class?
>2. How should I implement keeping track of these bullets?

This must be some new meaning of the word "design" of which I'd previously been unaware.

-- Tom Sloper -- sloperama.com

I understand the subclassing, however the new class's update function would be horrid if it had to keep track of the projectiles after the split. It would have to be responsible for the update and exploding of all three projectiles, logic that I think should be in the main game loop.

Quote:Original post by Tom Sloper
Quote:Original post by starruler
>two design questions.
>1. Is this bullet type worthy of a new class, or should I simply add the code to the existing bullet class?
>2. How should I implement keeping track of these bullets?

This must be some new meaning of the word "design" of which I'd previously been unaware.


What is the correct term for the questions I'm asking?
Well you could have something like:

class Bullet
{
...
Bullet(init Position, init trajectory .... );
void Update BulletPosition();

};

and then

class BigBullet : public Bullet
{
...
CheckDelaySinceShot()
{
if(TimeElapsedSinceShot > WantedDelay)
CreateThreeNewBullets(CurrentPosition,NewTrajectory...)
}
};

My Reasoning: A bullet is just a bullet and should only worry about itself and what it does: travel at some trajectory and speed.
So.... should a regular bullet worry about splitting? No! A regular bullet does not split. It is more than just a bullet... Dun da dun!!! derived class!!!

A class should fulfill its purpose and nothing more. Derived classes may add functionality to fulfill their own purposes. So if you want a bullet that splits, make a new class that spawns three base class bullets after the delay.
Hope this helps you,

Sab
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
The Splitbullet class wouldn't be responsible for tracking the three bullets, it would simply spawn the additional bullets at it's current location.

If the 3 bullets "fan out" eg: one breaks left, one right, and one stays on course, then you might just want to spawn the left and right as regular bullets and just let the SplitBullet continue it's course. Alternatively, you can adjust the course of the SplitBullet to a new trajectory, or simply remove it and replace it with a standard Bullet, depending on what you're most comfortable with.

throw table_exception("(? ???)? ? ???");

Quote:Original post by Ravyne
The Splitbullet class wouldn't be responsible for tracking the three bullets, it would simply spawn the additional bullets at it's current location.

If the 3 bullets "fan out" eg: one breaks left, one right, and one stays on course, then you might just want to spawn the left and right as regular bullets and just let the SplitBullet continue it's course. Alternatively, you can adjust the course of the SplitBullet to a new trajectory, or simply remove it and replace it with a standard Bullet, depending on what you're most comfortable with.


Right, but how do the spawned bullets get added to the game class's bullet vector so they can be updated? I could pass the vector to the update function of the bullet, or maybe have the game object be global. Actually the game object being global doesn't seem like a terrible idea.

-EDIT-
Quote:CreateThreeNewBullets(CurrentPosition,NewTrajectory...)

I'm asking the best way to accomplish this task.
Some game engines are built on messaging systems that would pass a message to the system to create the bullets and add them to the 'system' array.

If this is something really simple, I would suggest that you make a System class that encapsulates all the global variables. So if you wanted to spawn more bullets, you would just go System.CreateBullet() three times
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...

This topic is closed to new replies.

Advertisement