Simple map system for level selection javaScript example
This week I am continuing to work on one of my canvas examples that is just simply called turret defense because I am really bad at names. Anyway for the game I would like to have a level selection map where there are a bunch of display objects for each level in the game.
So then the general idea is to just have a collection of display objects, one for each level, and then at least two states in a crude yet effective state machine. When I click on one of these display objects in the map state I then go to the game state, but with the game state object created with custom options set by way of a game options object that is part of the level selection object in this map system.
With that said in this post I will be going over a very simple example of this kind of game map system, and for the game object I will just be using what I worked out in yesterdays post on working out a system for rotation and fire control.
What I will be writing about here then today will be yet another one of my javaScript example posts, where the aim is to work out some simple module, system, or mechanic, and not always worrying about making a full, finished product. There are a lot of ideas that come up when it comes to just having this kind of system in a game, but for the sake of this post I would like to have just a simple basic system for this sort of thing.
1 - The map module
First of I will get right to the main event of this javaScript example and start writing about the map module rater than any additional files that will be needed in order to have something that will make use of it, I can get to that later in this post. This module will contain at least two public methods, one to create a map object, and another that will serve as a way to know if a map item object has been clicked or not.
Like all of my other modules like this there is a main create public method, this is the method that I will be calling in my main.js javaScript file, or in a state machine module, or any other module where I will want to create an instance of a main level map object. The main map object will contain data that has to do with the current offset of the map when it comes to scrolling, for this example I went with a system where the map just scrolls along on a single axis as I would like to keep things a simple as possible with this one. So I have an object called move map that is not the current offset value, but it is a number of properties that are used to help with the process of mutating that value. After that in the main map object I have a current offset value, a max value for the offset value, and an objects array. This objects array is where I will be storing data for each display object in the map, and will also contain game module options for the current level.
Another public method that I have here is a get object at method, this is simply used to get an object at a given location in the map. So then this is what I will be using in an event handler somewhere outside of this module to find out if the player clicked a display object in the map or not.
|
|
I then have a main update method that will take a map object along with a secs value to update the current map offset position of the map. For now I have a system where the player can click on any area of the map and if they move far enough away from a start point that will cause the ma to begin to move. The rate at which the map moves will change depending on the distance from the start point, this seems to work fine for now, but in a more advanced system I would maybe want to add some features for keyboard support and other ways of moving the map that involve just pointing to an edge of the map or something to that effect.
One last additional feature that I put in is an on object with functions for each type of general pointer event. There is using the on.start function in event handler of a mouse down and touch start events for example. This is then just want way to go about changing the state of the offset value that is really the main feature of this map level select system. Really that is all that this needs to do, just move around on a map and then click on an item that will then result in the player moving on to another state where they are playing a game, or setting some additional options for the game before doing so.
2 - The utils lib
Like many of my javaScript and canvas examples I have a main utility library that serves as a way to have a base set of methods to use in the example. Mayne of these methods are usual suspect type methods that I take with be from one project to another, however in other examples I might work out some methods that have to do with the specific example that are new, but I might just want to park here for one reason or another.
The only method here that I am using in the map module is the distance method, which is a method that I am typically going to want to have with just about any example such as this. The other methods are used in the game module, and oter files such as the main javaScript file, but I might also use them elsewhere, so I park them here.
|
|
3 - The game module
Now for the game state module for this javaScript example of a map system. You see the map system is just a way to go about having the player select and maybe mutate some options that are then passed to the main game state modules create method to create a new game state. Then the over all application progresses to the game state of the application and the game will begin to play. For this javaScript example I am just more or less using the same state module that I would out in yesterdays post where I worked out some basics when it comes to having a turret that the player can control.
|
|
4 - The draw module
Just like with any other example where I am using canvas to create a view for the application I have a draw module that contains methods that help me draw to a canvas element. I always have a draw method to draw the background of the canvas which is almost always a solid simple solid color background, I also have methods that i am using to draw the state of display objects in the main game state of the over all example. The main method of interest here as far as the subject mater of this post is concerned though if of course the draw map method.
|
|
5 - The main javaScript file
Now for the main javaScript file will I will have a basic state machine for a map menu, and main game states. On top of this crude yet effective state machine I also have a main app loop for the example, and anything else that should be placed in such a javaScript file. I am also creating an instance of the main map object here, and in addition to this I am also pushing in some display objects for the map in addition to this.
|
|
When I have this example up and running the basic, crude, unpolished idea is working for what it is worth. I have a bunch of display objects on the canvas, I can move up and down to see more display objects, and when I click on one I progress to a new state in which the main game object is created with options set depending on the object I click on. Of course there is way more to add to the level select map system, as well as to the over all structure of the example as a whole. However when it comes to just the map system there really is only so much more that i would want to add and change. I just wanted a simple starting point for this, and I would say that this is what I have, not it is just a question of what he next step might be.
6 - Conclusion
So then it would seem that I have the basic idea up and running at least for what it is worth. However there might be a great deal ore to work on when it comes to making this system something that i will want to use in a real project though. Still that is okay because the aim here was to just make a simple system, not go all out with this, I wanted to just start somewhere with it, and this is what i wanted when it comes to just getting started at least.
Of course there are a lot of ideas for additional features not just with this map system, but also the game itself. This time around in the main javaScript file of this javaScript example I started a simple state machine, I am thinking that a more advanced version of this will make use of at least one more state that the player will enter when clicking on an item on the map menu. This additional state would be used to change a few more settings for the game, before moving on to the actual game state.