Invaders Digital Art javaScript example
Continuing with a digital art collection of javaScript examples I have made yet another quick project following the same general thought process with the others when it comes to sticking with a fairly simple idea, and getting the core of that idea done within the span of just few days. This time I wanted to make a digital art project that involves display objects that repentants fixed structures that spawn at the center of the canvas, and additional display objects that spawn from outside the canvas and move in to where the buildings are to attack and destroy these structures. The structures themselves also fight back, and both kinds of display units fire yet another kind of display object that is a shot object at each other. So then this digital art project is then something that resembles a kind of game, but because it is a digital art project that means I do not have to worry about UI design, save states, menus, and all kinds of additional features that I would need to work out of it where a game. This allows me to focus more so on just game logic, and also how the project looks as this is a digital art project.
1 - The utilities library
With just about all of these javaScript examples I often have a general utilities library where I park various methods that I will end up using in one or more additional files in an over all project such as this one. These are then typical usual suspect type methods that have to do with things like getting the distance between two points, bounding box collision detection, working with angles, the Document Object Model and so forth. The collection of methods in a module such as this will change from one project to the next so it is a kind of custom cut, application specific utilizes library.
One method that I have in here is a method similar to that of the lodash get method where I can pass an object, then a string that is a path to a value in that object, and then an optional default value to return in the vent that there is not a value at that path location. Another method that I have here is a kind of get a value within a range by a number between 0 and 1 method. This is another kind of method that helps with something that seems to happen fairly often when making this kind of a project. That is that I give the method a value between 0 and 1, which can also be the result of a Math random call, and then the next values are a range of values, a kind of min and max so that they return value is the per value that is between these values.
I then have my create canvas method that I started when working out my collection if vanilla javaScript canvas examples. When it comes to those examples I started each project from the ground up, as I do with these examples, rather than using some kind of canvas framework for better or worse that is one of rules when it came to making that series of examples. However I made one little exception from one project to the next, and that exception was this method.
I then have a number of typical methods that I use in almost every project like this such as a mathematical module method, and distance and bounding box method. I also have a few methods that have to do with working with angles.
|
|
1 - The improved object pool normalized library
In revision 1 of this example I made an improved object pool library that I will likely use for future projects moving forward as a like it a lot more compared to the library that I based this off of. With my canvas example on object pools I started a library for this kind of thing that I kept copying over to other projects, each time I did so I might not change much of anything to it, other times I made improvements and added features. However with this object pool library I decided to give it a new name to help really set this one apart from the others by calling in pool normalized.
By normalized I mean thinking in terms of having a point in 2d space first and for most rather than a 2d box object with a width and height that is to be place somewhere relative to the upper left corner of the object, which was the nature of the older object pool library.
|
|
2 - The units module and plug ins
Yet another major component that I have started with this example and will likely take with me to additional projects is the unit module that I started in this example. I have made a few modules such as this in past projects, but often then end up being coded together with the game module of a project, as such it is something that I found myself writing over and over again with each project. To help put an end to this I made a stand alone units module, complete with a plug in system that will allow for me to septate logic that has to do with specific kinds of units such as units that move, units that just stay in a fixed location, and various other kinds of units such as shots.
|
|
2.1 - The attackers plug in
The attackers plug in for my units module contains the logic for the units that will spawn from the outside of the canvas and then select a target that is one of the buildings and move it to attack.
|
|
2.2 - The Buildings plug in
I then have the fixed buildings in the center of the canvas so of course I am going to want a units plug in for that as well.
|
|
2.3 - The shots plug in
Both the attacker units and the building units will fire shots at each other, so then there will need to be another plug in for the units module that will contain the logic used for a shared object pool of shots.
|
|
3 - The game module
So then sense most of the logic that has to do with the game is pulled out of the main game module and into units module plug ins that results in a cleaner main game module. This is still the module where I will be creating the main game state object as well as updating such an object.
|
|
4 - The draw module
Sense this is a digital art project of course there is a library that contains a collection of methods that can be used to draw the various aspects of the main game object to the canvas.
|
|
5 - The main javaScript file
I am then just going to need a little more additional logic that will provide a main application loop, and a main state object.
6 - Conclusion
So then this example turned out pretty great, and I was able to get the core idea of what I had in mind up and running pretty fast. As of this writing I have a lot planed out for the rest of the example when it comes to the additional features phase of this example, in time I might complete that phase and have something that is even more interesting than what I have all ready, but I do also have a lot of competing ideas also so we will see if I get to the maintenance phase with this one.
Even if I do not complete the additional features planed out for this example, a whole lot has been created and improved that I will be taking with me to additional examples.