Attack Wave Control System javaScript example
For todays javaScript example I worked out an attack wave control system that I might use in one or move canvas projects that will be games that might make use of such a system. What I am taking about here is a system where there is an object pool that is used for buttons that from a bar or sorts. As time goes by the buttons move along to a given point such as the top of the canvas element. When a button reaches the top of the canvas it will become inactive, and that wave will then become the current wave, and as such will add however many enemies the wave will add to a queue.
There are a number of canvas examples that I would like to improve with a system such as this, and a whole bunch of additional ides for canvas element powered games that might make use of this feature in one way or another. So for this post for starters I would like to get a solid starting point for this kind of system up and running. Once I have that it is just a matter of shoehorning it into a project.
1 - The wave module
I am going to start off this post with the main event of the over all system rather than starting with support modules. I will be getting to them later in this post, but many of them are just more of the same when it comes to the various other topics that I write about when it comes to the various resources that I make for a javaScript project.
At the top of the module I have some constant values that have to do with the size, as well as the min and max speed at which the wave buttons move. When I start using this in some games I might want to make some of these settings that I can change in the create method of the module. However for now I think I will just leave them as hard coded settings, I just wanted to get the basic idea that I had in mind up and running fast and did not get to doing things like that.
I then have the spawn and update methods that I will be using when creating an instance of the object pool that will be used for this module. I will nt be getting into detail with the object pool module here of course, but I will be getting to the file that I am using for this example in a later section.
I then have my create, update and on click public methods.
|
|
There might be a few more things that I might want to add here and there, but I do not want to go to nuts with this for now. When it comes to all the other little ideas that come to mind many of them might change a little form one project to another. So maybe I should hold off for now at least, and get to that kind of stuff when I start to use this in a real game of some kind.
2 - A utils.js unilty library
This is a javaScript example, and as such the main feature is something that can be used in just about any kind of javaScript environment. However the typical use of the wave module will be as a canvas project in front end javaScript. As with just about any other canvas example I have a utility library that contains a bunch of copy and past methods that I use from one canvas example to another. This is s module that will change up a little from one example to another thigh, so for this example I am using bound box collision detection, as well as some additional methods that have to do with creating a canvas and getting a point relative to a canvas element.
|
|
3 - Object pool module
In this example I am also making use of an object pool module, this is something that I first worked out for my canvas example on objects pools, and take with me from one canvas example to another. I am using it to create wave bar buttons, and I am also using it to create enemy units for this example in the game module that I will be getting to a little later in this post.
|
|
4 - A game module
I made a quick place holder game module for this example, for now this is just a very simple game where units spawn at the top of the canvas and move to the bottom of the canvas. I did not put much time into this because the main thing I wanted to work out for this post is of course the wave control module. However In a real game I might only built so much more on top of this.
Just like with the wave module I am using the object pool module, however here I am using it to create a collection of units that will spawn when a wave button reaches the top of the canvas.
|
|
5 - A draw module
I made the full example of this a canvas example, so I have a draw module that I put together real fast. Most of what i have here I copied over from one of my canvas examples, called pop the lock when it comes to the draw pool methods. For this example I am just going to want to have draw pool methods for the wave buttons, and the units that will spawn into the canvas when a wave button reaches the top of the canvas. In the event That I start to make some kind of real example with this I might have a few more things that i use the object pool library for that have to do with navigating menus and displaying all kinds of info and so forth.
|
|
6 - Main javaScript file
I now just have a little more javaScript that will make use of everything that I put together here, and also a place to pack the main application loop. With many complex examples that I am starting to turn into a finished product of one kind or another often I might get into adding a state machine, and animated button system, and all kinds of additional little features. However for this example I have just a very simple main state object that contains an instance of a game module, which in turn holds an instance of my wave buttons module of course.
|
|
When this javaScript example is up and running things seem to work more or less as I expect. I get a collection of wave buttons on the side of the canvas that start moving up to the top of the canvas. Once a wave button gets to the top of the canvas the unit count of the wave is added to a total count stored in my main game module object. In the main game module object units are then spawn from the top of the canvas and they then move to the bottom of the canvas. If a unit reach the bottom of the canvas it will become inactive, and a unit will also become inactive if I click a unit.
7 - Conclusion
Well that is it for now when it comes to this javaScript example of an attack wave control system if I can call it that. I am not sure what else to call it, but so far it seems to work just the way that I wanted it to. I was able to put this together real fast in just a single day, and there is not really all that much more I can thing of that I might want to add to it. I am sure that some things will come up when I use this is an actual example of two. There are a few of my canvas examples that I would like to rework with something like this, and of course all kids of additional ideas that would work great with a system such as this that I might get around to starting at some point.
I just wanted to have a nice starting point for this kind of system, something that I can copy and past to a folder and start to hack over to make something else that will prove to be more interesting. As I start to make and update some of my canvas examples with this I will be sure to link to those examples in this post when I get around to editing.