Archived

This topic is now archived and is closed to further replies.

DevLiquidKnight

How could I make an object shoot more then one bullet while..

Recommended Posts

I don''t quite understand what you mean. But anyway (i''m asuming you''re programming some kind of shooter, but it probably won''t make much difference anyway ).

I think that using an array that holds every bullet is generally a bad idea. Say that you have some kind of machinegun (I don''t get how the "surface" you have is relevant to this issue) and it holds 30 bullets. Then you could in theory use an array of 30 and each item in the array represents one bullet in some way, but that would result in that if you managed to fire all 30 bullets, reload and then shoot again before the first bullet have made an impact, you would either have to remove one bullet that''s still in the air and thus won''t be able to hit anything. Or, you won''t be able to shoot again before the first bullet hits something and is removed. Now, maybe it won''t be possible to shoot 30 bullets, reload and shoot again before the first one hits, but I consider it "bad programming ethic"

Some OOP solution with a linked list with instances of a bullet class and using the new/delete operators would probably be the way I would handle it (If you''re using c++, you hopefully understand what I meant)

Enough rambling now.

-Luctus

Share this post


Link to post
Share on other sites
What you need is a well structured entity managment system. When the gun is fired a function like SpawnEntity should be called which adds a bullet to a list of entities. The bullet it then handled by an entity system, so your firing code can forget about it. All it needs to do then is decrement an int which says how many bullets are left in the magazine.

The entity system has a linked list of entity structs (or classes if you like that stuff). The entity struct would be something like this:


struct entity_t {
model_t *model; //model drawn by your graphics code
body_t *body; //physics info like pos, velocity etc.
float ttl; //time to live (to limit the entitys lifespan)
//blabla
};


SpawnEntity creates a new entity_t, fills it with the info passed to it, and adds it to the entity list. Then every frame, each code section handles the apropriate part; the graphics section will handle drawing the model, the physics section will handle updating the physical body and collisions etc.

hmm... well this has turned into me imposing my exact way of doing things I''m pretty sure it''s the standard way of doing things though.

hope that helps,
Frank

Share this post


Link to post
Share on other sites
I kinda get what you are saying. You have a sprite loaded onto a surface and you want to reuse that surface multiple times in one frame right in order to get multiple bullets right?

I guess if you are just experimenting you can use an array. But I would go with the linked lists described the above 2 guys. Thats a pretty good way to do it.

Share this post


Link to post
Share on other sites
CoderX:
yeah, absolutely. You don''t want to duplicate resources like the bullet''s surface for each instance of a bullet. There should be 1 copy of every sprite, and as many entities (which are instances of a sprite) as you want in a linked list, which only contain referance the sprite to draw and stuff like where to draw it.

Share this post


Link to post
Share on other sites
I a highly suggest you look into my 4e3 entry, it contains source code, and it all basicly just this, it is programmed in D3D but has a v nice wrapper over all that so you can easly see what is happening, its not so much on the DX side of things u seemed to be worryed about as how do do the bullit shooting and holding system, so look at my source, its not very large or complicated. (note: its 2D)

CEO Platoon Studios

Share this post


Link to post
Share on other sites