How to Create a Phaser MMORPG – Part 2

At the beginning of this tutorial series, we started building our Phaser 3 MMORPG. In Part 1, we did the following:

  • We set up the basic project and installed the required dependencies.
  • We added SocketIO to our project and added the server side logic for when a player connects and disconnects to our game.
  • We wrapped up by adding the SocketIO library to the client side code.

In Part 2 of this tutorial series, we will continue adding the web socket logic to client-side code, update the logic for adding players to our game and start adding the logic for allowing the players to attack.

If you didn’t complete Part 1 and would like to continue from here, you can find the code for it here.

You can download all of the files associated with the source code for Part 2 here.

Let’s get started!

Refactoring Client Logic

Before we start working on the logic for creating our player when the currentPlayers web socket message is received, we are going to refactor the logic in the methodcreate of our Phaser game. Currently, our methodcreatecontains a lot of logic and to make our game more manageable,so we are going to split the logic out into a few different functions.

To do this, open public/assets/js/game.js and replace all of the logic in the create function with the following code:

In the code above, we moved all of the logic that was in the create function into separate functions, and we removed the collision logic for the object layer in our game.

If you save your code changes, restart the server, and visit http://localhost:3000/game.html in your browser, you should see that the game still loads. If you move your character around the map, you should notice that the character can now walk through the trees.

Updating the Player Creation Logic

With the create function logic refactored, we will now work on updating the player creation logic. To do this, replace the create function in the WorldScene class, in public/assets/js/game.js with the following code:

In the code above, we did the following:

  • First, we removed the this.updateCamera(); line. We will be moving this to the createPlayer function.
  • We then added event listeners for the currentPlayers and newPlayer web socket messages.
  • In the function that is triggered when the currentPlayers event is received, we create an array of all the keys in the players object and loop through them. We then check to see if that object’s playerId matches the socket id of the currently connected player.
    • If the id matches, then we call the createPlayer function and pass that function the player object.
    • If the id does not match, then we call a new function called addOtherPlayers and pass that function the player object.
  • In the function that is triggered when the newPlayer event is received, we call the addOtherPlayers function and pass that function the playerInfo object.

Next, replace the logic for the createPlayer function inside the WorldScene class with the following code:

In the code above, we did the following:

  • First, we updated the x and y position of the player game object to be 0. Since we will be placing the player game object inside a container, that object’s location will be relative to the container’s location.
  • Next, we created a new container and placed that container at the location of the playerInfo object that was created on the server.
  • Then, we enabled physics on the container and added the player game object to the container.
  • Finally, we called the updateCamera function and updated the setCollideWorldBound logic to be tied to the new container instead of the player game object.

Now, we need to update a few places in our code where we are referencing the player game object and replace it with the new container object. First, in the updateCamera function in the WorldScene class, update the following line: this.cameras.main.startFollow(this.player); to be:

Next, we will need to replace the update function logic in the WorldScene class with the following code:

In the code above, we did the following:

  • First, we added a check to see if the new container object existed. We did this to make sure we didn’t run any of our update logic until the container object has been created.
  • Next, we updated all of the setVelocity lines to be tied to the container game object instead of the player game object.

The last thing we need to do before we can test our code changes is to update the createEnemies logic. In the createEnemies function in the WorldScene class, remove the following code:

Now, if you save your code changes and refresh your browser you should see that the game still loads and that the player is now at a new location.

If you refresh your browser, you should see that the player gets created at a new location each time.

Creating other players

With the logic in place for creating the main player, we will now add the logic for the addOtherPlayers function.  To keep track of the other players in our game, we will first need to create a new Phaser Physics Group. To do this, add the following line at the top of the create function inside the WorldScene class, below the this.socket = io() line:

Then, add the following code below the createPlayer function in the WorldScene class:

In the code above, we did the following:

  • First, we created a new game object using the information contained in the playerInfo object.
  • Then, we choose a random tint for the otherPlayer game object and stored that player’s id inside the otherPlayer object so we can reference that value later.
  • Finally, we added the otherPlayer game object to the otherPlayers group.

Now, if you save your code changes and refresh your browser, the game should load like before. However, if you open a new tab or window and navigate to the game page, you should see the other player in your game.

Note: you may need to move your player around the map to find the other player.

Handling Players disconnecting

Before we move on to creating our enemies, we have one issue we need to resolve with our player creation logic. Currently, if a player keeps refreshing their browser, or if they exit our game that player’s game object will stay in the other player’s game.

To fix this issue, we need to update our client-side logic to listen for the disconnect event, and when this event is received, we can delete that player’s game object. To do this, add the following code to the bottom of the create function:

Let’s review the code we just added:

  • First, we listened for the disconnect event and we defined a function that will be triggered when this event is triggered.
  • When this function is called, that function will receive the id of the player that disconnected. Inside this function, we create an array of all of the otherPlayer game objects by calling the getChildren method on the Phaser group. We then, use the forEach method to loop through the array.
  • While looping through the array of game objects, we check to see if that game objects playerId matches the provided playerId, and if it does then we destroy that game object. Now, if you save your code changes, refresh your browser, and load the game in multiple tabs, you should see that the individual player game objects are removed when they leave the game.

Creating Enemies

With the player creation logic out of the way, we will now start working on creating the enemies in our game. For this tutorial series, we will be creating the enemies on the client side of our game. Normally, we would want to create these enemies on our server and broadcast their location to the client side, however, this is out of scope for this tutorial.

For the enemies, we will be replacing the zones that are randomly placed around the map with player game objects. To do this, the first thing we need to do is load in the images that we will be using for enemies in our game. In the BootScene class, add the following code at the bottom of the preload method:

Then, replace all of the code in the createEnemies function in the WorldScene class with the following code:

In the code above, we did the following:

  • First, we changed the class type of the game object that was being created from Zone to Sprite.
  • Next, we changed the number of enemies created from 30 to 20.
  • Then, when we are creating the enemy game objects we call a new function called getValidLocation to get the location for where to place the enemy game object. Then, when we create the enemy game object we pass the location we just created and we use a new function called getEnemySprite to get the sprite for this enemy game object.
  • Finally, we call the setCollideWorldBounds and setImmovable methods on the enemy game object’s body.

Next, in the WorldScene class add the following code below the createEnemies function:

Let’s review the code we just added:

  • First, in the getEnemySprite function we created an array of the enemy sprites we loaded earlier. Then, we used Math.floor and Math.random to return a random sprite from that array.
  • Then, in the getValidLocation function we create a while loop and inside the while loop, we generate a random x and y position that is within the bounds of our game.
    • Once we do this, we loop through all of the enemy game objects.
    • For each of the enemy game objects, we get the bounds of that game object and we check to see if the x and y positions we created earlier are contained within those bounds.
    • If the x and y positions are not occupied, then we exit out of the while loop and return those positions.

Finally, we need to add a collider between the player container and the enemies group. To do this, add the following line at the bottom of the createPlayer function in the WorldScene class:

Now, if you save your code changes and refresh your browser, you should see the new enemy game objects.

Conclusion

With the new enemy game objects in place, that brings this part of the tutorial to an end. In Part 3, we will wrap up the tutorial by doing the following:

  • Add logic for moving the enemy game objects.
  • Add logic for attacking the enemy game objects.
  • Add logic for allowing the players to chat.

I hope you enjoyed this and found it helpful! If you have any questions, or suggestions on what we should cover next, please let us know in the comments below.

Published by

Scott Westover

Scott Westover is a Fullstack Developer who loves coding, writing tutorials, participating in game jams, and learning about new technologies. When he is not coding, he is either spending time with his wife and two kids or is reading a great book. Scott is looking to form connections and share his knowledge. Connect with Scott on Linkedin.

Share this article

Leave a Reply

avatar
  Subscribe  
Notify of