Creating a Simple Particle Generator for Barrage to generate awesome bullet patterns

Posted by Tony Nov 11, 2014

Bosses in video games should present a unique challenge while invoking a sense of excitement within players. One way to make a boss (or any enemy) unique in a shooter type game is to give them cool weapons.  Some of the fondest video game moments I can remember involve crushing a boss and then immediatley telling my friends about it.  The goal of using Barrage is to create some really fun bullet patterns that would simultaneously make players say "ooh pretty" and "oh crap" at the same time.


The documentation on Barrage is a bit thin, but most of the important details are present.  Barrage itself is a particle govenor, which basically means a bunch of particles will be forced to do it's bidding.  The way this works is very similar to other particle systems.  The particles themselves have position, velocity and acceleration and based on time work together to create pretty patterns.  

This article covers the following:

  • Creating a simple particle class for Barrage
  • Creating a simple particle system class for Barrage
  • Updating Barrage and a single particle system to create some cool particle patterns

Creating a simple particle class for Barrage

The first thing a particle needs to do is implement the IBullet interface shown below:

The IBullet interface describes the properties a particle needs in order to be controlled. The first thing we need to do is create a Particle class to facilitate the IBullet interface, this is done with the MiniParticle class shown below:

The MiniParticle class doesn't do a whole lot.  It contains the properties that a particle needs in order to move such as position, velocity and acceleration.  The initial value for these properties are passed in through the constructor and stored off for later usage.  The particle in this case is nothing more than a data structure.  This makes sense because the particle is dumb (no logic), the particle system manages the particles (create, update, delete) and Barrage is the brains that controls the particle system. 

Creating a simple particle system class for Barrage

The next interface to implement is IBulletEmitter.  This is the portion of the particle system that will manage the particles.  It's nothing special, the only tricky part is figuring out how to properly update the particles.

Before the IBulletEmitter interface can be properly implemented we also should take a look at the Barrage implementation instructions from github to ensure we know how to properly intialize and update Barrage and our particle system.

This is the part where Barrage is weak in documentation.  For the IBulletEmitter (the particle system) we focus on the emitter variable.  Looking at the above code snippet shows that we need to perform the following:

  1. Create an emitter of type IBulletEmitter
  2. Pass the emitter as an argument to Barrage
  3. Periodically update the emitter with a delta timestamp

To create the emitter we add the MiniEmitter class and implement IBulletEmitter as shown below:

The MiniEmitter class does two things:

  1. Uses drawTiles() to display the particles
  2. Updates the particles per the IBulletEmitter interface

The IBulletEmitter interface allows Barrage to directly control the particle system to create the pretty patterns we so desperately seek.  

emit() The emit() function allows us to create new particles.  This is a simple function, we simply create a new instance of our IBullet particle and then store it for later usage in an array.
getAngleToEmitter() The getAngleToEmitter() function supports finding the angle between a particle and the source emitter.  Because position is a Vector2D object there is already a written angleTo() function that will do the math for us, we simply glue the APIs together.  It's good to think of the emitter as the source of the bullets.
 getAngleToPlayer() The getAngleToPlayer() function is identical to the getAngleToEmitter() function except the test point is different.  The player is the target of the bullets, and in this case we aim all particles at the current mouse pointer on screen.  This function is used by Barrage to determine what the bullets should move towards.
 kill()  The kill() function is self explanatory, when a particle is no longer used Barrage will eliminate it.
The meat and potatoes of a particle system is updating the particles to move around.  This implementation may not be 100% accurate, but because we are dealing with position, velocity and acceleration we chose to model the particles using linear motion.
All this really means is that a particles position can always be calculated based on velocity, acceleration, start position and time. Each update() iteration causes the particle system to:
  1. Update velocity of each particle depending on its acceleration
  2. Update position of each particle depending on its velocity
  3. Add the necessary particle graphic data to the drawing buffer for drawTiles()

By modeling the system using linear motion we allow time and the Barrage IBullet properties to determine a particles coordinates at any moment.  

Updating Barrage and a single particle system to create some cool particle patterns


Last but not least is the update code from the github Barrage implementation instructions that ties everything together.  First a barrage pattern is parsed then the particle system is initialized and finally we setup our periodic updates, and thats it!  

A FlashDevelop project and full source is available on github, check out the results!


(The particle system's target position is your mouse pointer)

Category: Xenizo Games


Xenizo Games Lead Developer

Add Pingback
blog comments powered by Disqus