In tutorial 5 (which can be found here). We looked at how to calculate a circular collision field. But this isn’t that practical when we’ve got lots of stuff to check through. We need to have a manager to control all of the objects.
The manager keeps hold of an array of objects (called aryCollisionObjects) and is based on the singleton pattern. The singleton pattern is a software engineering pattern and means that we need to call the function GetCollisionManager() which returns the one and only (or single if you will) so that we can be sure that we’ve got the right one with only one list of collision items.
We add items to the manager by calling the code in the following way:
var colMgr = GetCollisionManager();
colMgr.add( ourCollisionItem );
The best place to do this would probably be the constructor but it doesn’t have to be it could happen anywhere.
When we are done with the object (i.e. when it’s destroyed) we need to call remove, as in:
var colMgr = GetCollisionManger();
colMgr.remove ( ourCollisionItem );
We need to remember to remove it when it’s done or bad things could happen.
To spice things up a bit we’ve also added a health bar to show how much health is left. This is an extension of FiltoidDrawable() in FiltoidUtils.js. The extension checks to see if the this pointer has a health count. It then calculates the proportion of health that’s left and draws the green bar proportional to the red bar.