Rotate and Fire javaScript example
Lately I have been giving my turret defense canvas example a much needed overhaul as I do not like the state that I have it in. I added a whole bunch of code that brings things to the example that start to make it look like an actual game to some extent for once. However there is much more work to do when it comes to making a quality game that people might actually want to play. For today though I wanted to work out a simple javaScript example where I am just focusing one one little aspect of the game, and that is just having the turret move and fire.
This fire control system is just working out a standard way to control what will cause the turret to move, in what direction, and at at what rate of movement. This example then makes use of a bunch of methods that have to do with working with angles, because I will want to know the shortest direction between two angles, and also the angular distance from one angle to another, and so forth. Also it is true that what I am working out here will ally to all kind of games that will make use of this kind of control system, or something like it.
1 - utils library for this javaScript example
For this javaScript example I put together a simple general utility library that will contain all kinds of methods that will be used for this javaScript example. In just about any kind of project example like this I always have a general utility library to park methods that I might use in more than one place, or are methods that I will often copy over to other modules like this.
Anyway in this module I have a number of methods that have to do with things like getting the distance between tow points as well as a number of methods that have to do with working with angles. I am going to want to get an angular distance, as well as the shortest distance to rotate the turret so of course I have methods to help with that sort of thing here.
|
|
2 - The state module
I put together a quick state module that will create and return, as well as update the main state object of this javaScript example. In a real projects I might have a lot of files that will make up the state including a state machine, a game module, along with many plug ins for this modules. However this is a simple little javaScript example where I just have a single file for this sort of thing.
The main public method of this module is the create method, this will create the main state object for this module, and the javaScript example as a whole. This state object contains properties like the x and y position of the turret, the width and height of the area of the turret, as well as a number of other properties that have to do with the rotational speed and rate of fire of the turret.
I then have a number of public methods that are used to mutate a state object created with the main create method of this module. I have things broken down when it comes to this where I have an update method that will just update the target property of the state object, and then another that will update the current facing direction of the turret. I then have another update method that will update the state of a shots object pool that I am using inside this module.
|
|
SO that is it for my main state module for this javaScript example, but this is just to create and update an object that will sever as a model of sorts. When it comes to drawing the state of this model to a canvas element, or any kind of display for that example I am going to need to use some kind of draw method.
3 - The draw module
For this javaScript example I am making use of a canvas element to provide a crude yet effective view of the situation. So of course like just about any other example where I make use of a canvas element I have a draw module that will contains all the methods that I will be using to draw to a canvas element. WHen it comes to this module I have a draw method that will render a simple solid background for the canvas, draw the turret, and draw shots coming from the turret.
|
|
4 - The main.js javaScript file
I am now going to want to have a main.js file in which I can make use of all the above javaScript code and add a main app loop, along with some event handers. Often when it comes to this kind of file I might have a state machine, and a whole bunch of other things going on. However this javaScript example is fairly simple so I only need one state to work with really.
Here I create and append the canvas element that i will be using for the example, then create the state object with the sate module that I worked out above. I then have my main app loop in which I update the state of the state object, and draw some things using the draw module also in each app loop.
|
|
When this example is up and running I have a turret that moves from its current facing direction to the heading direction if I just leave it alone. When I click on the canvas the facing direction moved to the point on the canvas that I am clicking on, and when it gets within the fire range it begins to fire shots.
5 - Conclusion
So it looks like I have the basic idea that I has in mind up and running just great, In fact this time around I would say that it is working better than the old buggy code that I am not replacing in my canvas example that will make use of what I worked out here today. So it should go without saying that I will apply what I have worked out here in this example in my turret defense canvas example to say the least. I also have a lot of ideas of other games that might involve this kind of system so it might pop up in some other future projects here and there as time goes by.
There is a lot more that I am going to want to work out for my canvas example, and even more to do if I am going to turn it into some kind of major project that might to prove to be a game that people might actually want to play. For now maybe it is a good idea to just keep working on simple little stand alone examples like this, and then start to create a more advanced version of what I work out when working on my actual project. I sometimes thing that it is a good idea to do that, just create a simple, basic, hello world style stand alone javaScript example of some kind of feature that I would like to add to a game. Once I have the basic idea up and running okay it is then just a question of how to go about shoe horning it into a project.