New Object Pool library javaScript example
Looks like I will be continuing to expand on my collection of javaScript examples posts with a new examples as it just seems like the thing to do once I write about the basics of javaScript. This week the example that I made is a new object pool library that I made for a recent digital art example earlier this month. After doing so I thought that this new object pool library deserves a for post folder of its own in my test vjs repository, and I should also maybe mack at least one if not more examples that make use of this library when it comes to making additional javaScript projects.
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.
1 - The pool normalized lib
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.
2 - Utilities of this example
Just like with my many other javaScript examples I typical have a utilities library that contains a lot of methods that I will use in my various files that compose an over all project. As you may have noticed a few of the methods in my object pool library use methods in this utilities library, namely a distance formula method, wrap number method, and bounding box.
|
|
3 - Draw module
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.
|
|
4 - Main javaScript file
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.
|
|
5 - Conclusion
I will want to make at least a few revisions of this object pool library at one point or another, but only as needed. I think much of the additional features and functionality of this should be added on as additional libraries that build on top of this rather than making this project even more complex actually. I will be creating those projects in the not so distant future as I continue to work out even more javaScript project examples to write about in posts such as this.
As I make additional javaScript example prototypes I will like to continue with the same general tone that I started with a few weeks back with my digital art reduce pool javaScript example. That is to start out with a very basic, minimal idea when it comes to what the end result is, at least at first. I often end up going nuts with features when making something, and while doing so I do not finish the most important core set of features first. I often seem to think in terms of continuing to work on a project indefinitely, and when it comes to that there is always the thought of “Oh I can just get to that later”. That is a really bad habit, and I think the key to breaking it is to start out with an outline of what a project should be first.