canvas example of a beach war game
For this weeks canvas example I started working on an idea that I had for a simple strategy type game. The basic idea of what I had in mind is just a simple 2d grid type game with three index values for ground types that are water, beach, and land. The player can build structures on land, but not on beach or water cells. In the water enemy boats can spawn and attempt to attack and invade the beach.
I was not thinking in terms of much more beyond that, but there are several games that I have played in the past that come to mind that where kind of fun that where like that in one way or another. So I wanted to take a moment to throw together a basic prototype of some kind of game where the player is defending a beach.
The current state of this canvas example is not what I had in mind just yet as of the time of this writing, but it is turning into something interesting that is showing some potential. It is not the kind of project that involves user interaction at least for the moment, so for now it is a kind of game where the computer is playing against itself sort of speak. That feature of having the computer automate the process of playing is something that I often find myself putting in anyway.
1 - The utils library for canvas example beach
So with many of these canvas examples I end up making a utility library where I park methods that I think I will be using in more that one module at one point or another. Or another reason why I might place something there is to just reduce the complexity of another module such as the main game state module as those have a tenancy to get rather complicated. many methods and objects here might eventually get pulled into their own files at some point, but a general utility library seems like a good starting point to park these kinds of things.
In any case this utils.js file is a collection of methods that are relevant to the modules for this canvas example that include the modules for state, rendering, and any plug-ins I might make at some point if I end up making a plug-in system.
|
|
For now the module consists of the distance formula, and a an experience point system that consists of two methods. The distance formula is a usual suspect for a lot of these canvas examples, but the experience point system is thus far something that I am just using in this project, at least at the point of this writing anyway.
The XP system of a game alone can often become a complex project on its own, however for now I just wanted a very simple basic system for this kind of thing. As I see it an XP system should have at least two methods one that can be used to create a state object for sorts by passing a level, and level cap to get a state object, and the other by passing an XP point value. The state object then contains properties like current level, the level in fraction form, the xp value, and values for the next level and to the next level.
2 - The game module
Here is the main game module that will return two public methods that are use to create a game state, as well as update that game state for this canvas example. The state object that the modules creates contains a grid as well as pools of display objects for player turrets, enemy ships, and shots. The main update method of this module accepts an instance of this state object and updates the state based on the amount of time that has elapsed sense the last update.
|
|
I do not want to write to much more about this module at this time, the reason why is that I might continue working on this example a bit more and as such the state of the module might change a lot, or break down into many modules actually.
3 - Draw.js
Now for the draw module that will help with rendering the current status of a game state object for this canvas example. The module contains usual suspects for many of my canvas example projects including methods that render a background, and debug info. As of this writing I am still just working out the core of what this project is, and as such I am not doing anything with sprite sheets. Instead I just have methods for drawing certain display objects by way of 2d drawing context calls for various canvas drawing methods.
|
|
4 - Main.js and index.html
Time to pull all of this together with a main.js file and a little html for now.
I have not got around to making a map system for this just yet, so for now I am just using a hard coded map as a javaScript string. I pass this string to the create method as an areaData property of the argument object for the crate method of the game module.
Once I have my game state object I just pass it to the update method of the game module in the body of a main app loop.
|
|
I then have some HTML that pulls this all together with a single hard coded canvas element.
|
|
For niw the example is working as expected. Turrets show up on the tiles where turrets can be built, and boats spawn and move around. The turrets fire at the boats, and when the loose all hit points they are purged out from a display object pool. There is still much more I would want to get done with this example when it comes to turning it into something that is truly interesting. I think that this project might end up being one of my examples that I will be getting back to now and then when I get some time. It goes without saying that it still needs a lot of work, still the vague general idea that I had in mind is there up and running for what that is worth.
5 - Conclusion
This might end up being one of my canvas example that I might keep coming back to now and then in an effort to break the habit of starting a project but never really finishing it. I like how it is turning out so far, but this one does need a whole lot more work, and I am also not completely sure what the finished product will be if I keep working at it. The main thing I wanted t focus on with this one is to just try to make something that is fun.
With that being said maybe I will keep working on this canvas example, and if so I am sure i will come back and expand this post even further. I would like to break the habit once and for all, if not with this canvas example, one of them at least.