A game framework JavaScript example
This week I made another major JavaScript example this time it is a current standard game framework. This project is actually me using a whole bunch of different projects that I have made over time to create one massive central project of a javaScript example. The thing about this here is that I am getting tired of writing the same code over and over again each time I start a new project, which is often going to be the case when it comes to making the project a vanilla javaScript project. After all that term means I am writing all the code from the ground up rather than using some popular framework. So if I am getting tired of writing everything all over again each time I start a new project, but I do not want to use someone else framework, then I guess I just have to make my own.
As of this writing this game frame work has all the basic features that I would want to have in a game frame work at least. A must have feature of a project such as this would be a state machine which is in place all ready. On top of that there is a canvas module that supports layering, and an object pool that I can use to quickly create a collection of display objects.
1 - The utils lib
First off when it comes to making any kind of major project like this I am going to want to have a main utility library. This is a place where I will park and and all methods that I might want to use across one or more modules in the over all project, and I can not thing of nay better place to park them. I have wrote another javaScript example post on a general from of this kind of utility module, but I use a custom cut form of it for any given project.
For this game framework thus far I have made one important additional that I will likely add to my general from of this kind of utilizes lib, which is a basic http client. For this I quickly put together something that works okay thus far that just makes use of XMLHttpRequest to preform http requests. One of the features that I want to have in this game framework is an external assets loader of some kind, so this is a much have method.
So far I just have some of the usual suspect methods when it comes to game development. With that said there is a bounding box methods, as well as a distance method. I also often end up needing a mathematical modulo method when working out game logic also.
|
|
2 - The object pool lib
A must have feature of any kind of game framework might be to have some kind of object pool module. For this I started out with what it is that I worked out for my canvas example post on object pools, and I made just a few minor changes thus far.
The module is used by calling the main create method of the module, and when doing so I pass some options when it comes to how to go about setting up the object pool. One of the most impotent options of this would be the count option which is what the fixed size of the object pool module should be. I also can give methods that are to be fired when an object is spawned, updated and purged.
|
|
3 - The canvas lib and plug-ins
I am going to want to have a canvas module as part of this game framework as that is just a major part of making any kind of html and javaScript game these days. The module should have at least a few basic features that such a module should have, and the first thing that comes to mind for this sort of thing is to have layers actually. Last week I started another javaScript example that is just this more or less which is of course my first canvas module javaScript example. So I started with what I had for that post, and just started making a few changes here and there, and added some plug ins for it.
3.1 - The canvas lib
Here is the canvas lib source code as it currently stands as of this writing. In the body of the core source code of this module I have a few built in draw methods. One f which is to create a current layer, another is to draw a background. I also have built in draw methods for printing some text to a layer, as well as a collection of points created with one of the points methods built into this module or added by way of a plug in.
Speaking of points there are also just one built in points method which will create and return a box in the from of a collection of points. After that it is just a bunch of helper functions for the public methods of this module, one of which is for creating the main stack of layers that is used with this modules other public methods. On top of course having a pubic method for creating a stack of layers there is also the main draw and create points methods as well as a load method which is what I use ti extend the features of this module.
|
|
3.2 - The buttons canvas plug-in
I made a buttons plug in for this canvas module when I started a buttons feature for this game framework. This canvas plug in will add two draw methods then, one of which will draw a single button object, the other will draw all buttons for the current state.
|
|
3.3 - The mod-pool canvas plug-in
When I added the object pool to the over all project I also wanted to add a basic draw method for a pool of objects. This is something that will need to be refined at least a little more even when it comes to having a bland general form of this kind if draw method. The module however can also serve as a starting point for making another kind of canvas plug in that I might have to make on a per project basis. For example say I am making a game where I want to display hp bars for display objects, and other info that has to do with the specific nature of a game, I will need to go beyond something simple like this then.
|
|
3.4 - The points-circle plug-in
This points circle plug in is yet another plug in that I added just for the hell of it. I may make some of these features built into the canvas module itself at some point maybe, but for now I have them as optional pug ins outside of the canvas module. This plug in will add just to points methods to the canvas module that are circle and oval points method. So then that is it just having some methods for creating a circle and oval in terms of a collection of points.
|
|
4 - The game frame lib
Now for the current state of the main game frame module that is what I will be using in demo files to create a main state machine object, and add state objects to it. In this module I have to create methods for creating state machine objects, one of which is to create the main state machine object, and the other is to create a more base object of a state machine object that is used for nested states when making a game module.
I then have a bunch of helper methods that are many used with the main create state machine public method. For example there is a helper that will create and add a state object to the main state object in the event that there is a loader option in the given options object. There are also helper methods that are used to check if a button has been click when it comes to the buttons collection feature of state objects.
The additional public methods thus far have to do with pushing additional state objects to a main state object, and setting what the current state is. The punch state object method is a way to make sure that empty properties of a state object are filled with something at least. The set state method should always be used to change states when and where needed to make sure that the end and start hooks of state objects will fire.
|
|
5 - Demos thus far
So then Now that I have my game framework together I will want to make at least a few quick demos of this framework to make sure that it is working just the way that I would like to to with the features that I have added thus far.
5.1 - Basic hello world demo
So the the first demo that I put together is just the usual hello world type demo where the goal is to just print hello world on the screen. However for this demo I wanted to also test out a few of the other features of this game framework other than the print draw method that I have built into the canvas framework.
|
|
5.1 - Demo of Menus
When I added buttons as a feature of the game framework I wanted to set start a demo to make sure that they are working the way that they should be. There is a bit more that I might want to add when it comes to this feature at some point but for now this is the basic demo of the buttons feature.
the basic idea of buttons is to just have the option of having a collection of button objects when creating a state object. When I create a button object I have the option to add an on click method that will be the logic that is to fire when one of these buttons are click by the user with the mouse or a touch screen. For now in this example i am just using these buttons to change the current state object.
|
|
5.1 - Demo of the loader
I am going to want to start making games that make use of external image assets so I will want to have a assets loader in the game framework. As of this writing there is such an assets loader in the framework, and in order to use it I just need to add a loader object to the options object that I pass to the create main state machine object method of the game frame work.
|
|
6 - Conclusion
So far this framework seems to be working okay, at least when it comes to the silly demos that I have made for it thus far. There may be a few more features and changes that I will want to make for this thing of course, however I do not want to go to nuts with this thing just yet. The core set of features that I would want to have in a game framework are there such as a state machine, basic canvas, and object pool modules. In order to really know what should be added on top of what I have all ready I am going to want to make more demos, and actual projects. However for the most part I am thing that what I will want to do is just further refine the features that are all ready in place.
I would like to keep this framework fairly minimal when it comes to features, but there is the idea of created a whole new framework only not from the ground up, as I would start with this source code thus far. There are features like a grid module, and various kinds of draw methods, and points methods that I might not want to add to a basic framework like this. However when it comes to making a certain set of games, with certain common features there is making a framework with things like that baked into the framework that is a kind of extinction of what I have work out here.