This is not the first time I made this sort of library that I then end up using in one or more additional projects, in fact this is still very much based off of what I started in my canvas examples series post on this subject of object pools. If you are not sure what an object pool is the general idea is more or less as the term descries, it is just a system of creating a pool of objects that are then used over and over again, rather than creating and destroying objects as needed.
One major improvement that I wanted to make with this object pool library is how the object state is stored as well as rendered. The older object pool module that I based this on centered around the idea of having the x and y values of an object refer to the upper left corner of a display object. In this pool I wanted to change things with the various methods so that the x and y values refer to the center point of a display object actually, and then things are figured out from there. While I was at it I started to make some additional improvements on top of that of course though. As I keep copying the source code of the original library over from project to project I have made little additions here and there with this, one of which is a wrap object method which is a typical methods that is needed for a library such as thins, however for this version of the library I also added a clamp method.
So then in this section I will want to start out with the state of the source code of the object pool library itself before continuing with additional dependencies and files to make use of this library for the sake of having at least one or more demos of it. The public methods of this library can be divided into three categorizes on create methods, pool methods, and disp object methods. That is methods that are used to create a single display object, or a pool of such objects when it comes to create methods. Then there are methods that will do something with a whole pool of objects such as get a count of how many in the pool are currently active, while display object methods are for figuring out something with just one display object.
The main method of interest for getting started with using this object pool library would be to use the main create method. When doing so I can pass an options object that will contain all the logic that is used to update the pool for each active display object, as well as what to do when an object is spawned, and purged, or rather set active and inactive as objects are never created or destroyed once that main object pool object is created.
Conditions are not set when creating the object pool as to go about spawning one or more objects from the pool, this is something that is to be determined when creating the over all larger app that will make use of this. Inside the body of a main app loop, the update method of a state object or whatever the case may be is where I will want to call the main spawn method of the module, when doing so I pass the object pool, along with a state object that I want to use and an additional spawn options object.
I could go nuts adding additional methods to this module, but for now I am thinking that I should stick with just a very small core set of usual suspect methods. Many of the ideas that come to mind are specific to a certain kind of game, or just an idea of some kind of digital art type thing, such as having a method that will give me an average heading for a pool of objects or a subset of a pool of objects. That is an idea that I would want to keep in a specific application rather than writing it into the core of what this is.
I then have a draw module that will contain methods that are used to draw the state of various things to one or more canvas elements. For this project I then of course have a draw method that will render the current state of a display object to a canvas element. On top of that draw method I then also have a few other typical draw methods that are used for just about any project or example such as ones that are used to just draw a simple solid background, and also the current version of the example as I get around to editing these posts now and then, and when I do so I typically improve the quality of the code also while I am at it.
I think that I will be making at least a few examples that make use of this library, in fact the source code of pool was very much pulled from my recent digital art project on an invaders game concept type thing that came to mind. There are a few good things that came out of the project actually, this object pool library was just one of them that I polished up a little when working on this. However for this javaScrript example post I will want to make at least one additional demo that makes use of the object pool library just for the sake of testing out this revision of the object pool library.