So today I got around to making another example that involves phaser ce plugins. This time around the aim was to make a simple defense style game plugin. The process of even making a simple defense game can some times be a compacted one, a greate deal of logic needs to be in effect to govern things like when an enemy is to spawn, and what happens when it reaches a certain point, such as the side of the screen which is typical for most of these kinds of games. In this post I will be writing about a plugin that I made that contains much of the basic component of a simple defense style game.
The plugin I am writing about here is not really a complete game by itself, but contains a fair amount of logic that can then be used in conjunction with other plugins that can produce a full game. This idea here is to offset blocks of logic into septate collections of code that can work by itself, but will work best of enhanced with additional options components.
This is not a getting started post with plug-ins ), or with phaser in general. I am also using a lot of other phaser features in this post as well, such as groups, and sprites that you should get up to speed with before hand if you have not done so all ready.
In this post I am using phaser community edition 2.11.1
So the plug-in that I put together for this post is not a done deal necessarily, if I where to continue developed this is would likely eventually break apart into several plugins. Regardless if that is the case or not it would al least contain a few more features that I have just not got around to. However it should still serve the purpose of providing and example of the beginnings of what might eventually be a defense game. Pulling code into modules or plug-ins like this is just necessary for the purpose of keeping things well organized.
Anyway the plugin contains many helper functions that are used to do things like create, and update a pool of enemies, as well as the dimensions of a game board area. It can accept a few arguments from an options object that allow for me to change things like the position of the game board relative to the game world. In this section I will be covering all of these methods, what they do, and link to other posts that elaborate on the various phaser ce features that are used.
I started off the plug-in with a function literal assigned to a signal global variable. This function will be called once in the create method of a state object to setup the plug-in. After that I set up some defaults for sprite sheet keys, the col and row size of the grid, the offset position and spawn rate.
Later in development if I continue working on this I will likely add a great deal more in terms of these kinds of variables but for the sake of this post it might be best to keep things simple.
Here I have a method that will create tiles. Although I am not making much use of it in this post the reasoning is that I would eventually add buildings that can by constructed by the player. Here I am using sprite data objects to store some values that can be quickly retrieve when making some else outside of this plug-in such as an interface.
For each tile I input enabled it so that when the player clicks a tile sprite it will fire a custom event I have made using Phaser.signal.
So the create tile method that I mentioned above is used in this method that is used to create nested groups of rows of tiles in another group. I decided to go this way rather than using a tile map. One of the reasons why was because I ran into difficulties when setting an offset for the tile map layer instance. if I really wanted to I could do it, but it makes things more complicated because the map data is still set relative to the game world rather than the offset that I give. So a solution involving nested groups of sprites proved to be a simpler solution to make a grid.
So I also have a method that is used to create a group of enemy sprites. In this project I went with creating a pool of enemy sprites that end up being stored in a dead state, which can then be revived and placed into a game area. Once they are killed then then go back to this pool. I often like to go with this approach rather than a solution involving creating sprites on demand, and then completely destroying them when they are no longer needed.
So here is a method that updated enemies on each frame tick. For now all I do is just move the enemies from the left side to the right side of the grid. Once the enemy sprite reaches the right side of the grid the player looses health, and the enemy is killed and return to the pool of enemy sprites.
I ended up working out a solution where enemies are moved from a pool of enemy sprite objects to the group that contains my rows of tiles. So I needed a method that updated a list of enemy sprites.
Here I have a method that spawns an enemy sprite by reviving a dead sprite from the pool of enemy sprites, and moving it to the group of rows of tile sprites.
So now that I have all my helpers worked out I can now create and append the plug-in object. For this plug-in I have an init, and update method.
So to use the plug-in I just need to create an instance of Phaser.Game,a dn call the Plugin_defence global function, passing the instance of the game as well as an option options object.
I can set keys for sprite sheets in the options object that I pass to the plug-in, but for this example I created sprites for the default keys that the plug-in looks for. like with many of the simple sprite sheets that I make for my blog posts on phaser I went with a canvas solution for making a sprite sheet.
Here I have the actual Phaser.game instance. In the create method of the state object I create my sprite sheets, and then call the plug-ins main function passing the instance of phaser.Game, and an options object. I also tested out my custom event that I have created that fires each time a tile is click by adding a hander to it, and create a text object to display the status of the players health.
When all is up and working what I have so far is a situation in which enemy sprites spawn and move from one side of the grid to the other when they do that player looses health,and then end up back in a pool to be reused again later. I can also click on them to kill them as well. Not much to write about for now. Still plug-ins are a great way to keep my code well organized when making a project.