Bullet Firing Methods? Shmups Etc?

BlitzMax Forums/BlitzMax Programming/Bullet Firing Methods? Shmups Etc?

Amon(Posted 2012) [#1]
I know how to do just wondering how everybody else does it. I track the player and fire the bullet from its position using KeyDown() and various bits n Bobs!

My thinking is that, definite for me, I am doing things a clumsy way so before I commit to the current method I have implemented I'd like to see an example of how any one else does it. If that's ok?

Ta!


ima747(Posted 2012) [#2]
Depends greatly on your overall structure, are you using objects or doing it all in an old school fashion? how/where/when are updates handled for other things in your system etc.

Personally I like to subclass:
Create entity class (super for all things basically, usually just an X/Y position, maybe an orientation, maybe a draw method and a graphic holder depending on your structure, how much the simplest thing can do etc. you can subclass this for stuff like platforms or trees)
Subclass the entity to a mobile entity class (adds things like momentum and update handler, this is allows easy grouping of things that need more logic)
Subclass the mobile entity to player/monster/bullet/etc. (add logic for creation, AI, and actions such as a player firing a gun)

now when the user presses a key, you go to the player object (which is a mobile entity, which is in turn an entity) and tell it to fire. The player class's fire method creates a bullet and adds it to a bullets list (either in the player if you track per object, or just a master bullet list if all bullets are handled the same...). Since the new bullet is a subclass of the mobile object class as well you can apply the player's position and momentum to it, then add an offset for the actual gun position on the player graphic, and adjust the momentum according to where it's pointing, etc.

With a shmup specifically the hard part is that you have to be REALLY efficient with managing your lists since they're going to be long... looping through 1k bullets, updating and drawing each one takes a while. But again this is pretty specific to the structure you're using for the game, for the objects, what kinds of lists are you using (linked lists, arrays, etc.), where they're stored, what kind of logic they perform, etc.

And all of this is assuming you're going with objects, totally different if you're going with global management, or other master arrays, etc...

Example layout of some object types

Entity
- Picture
- X/Y
- Draw() - (loop through ALL entities in the game and call this, then flip)

Mobile Entity (subclass of Entity)
- Momentum X/Y
- Update() - (loop through ALL mobile entities and call this to have them update before drawing)

Bullet (subclass of Mobile Entity)
- Update() - (overrides the generic mobile entity update and adds a colision check to see if it should damage something and then be removed...)

Player (subclass of Mobile Entity)
- Shoot() - (creates a bullet)
- Update() - (over rides the generic mobile entity update and adds stuff like keyboard checks to change momentum, check if the player is colliding with the world, etc.)

Last edited 2012