Note: This is quite a long post, so I do encourage you to get fresh cup of coffee or tea, get comfortable, and skip around to the interesting parts if you need to. Also, you can view all of the source code on GitHub, which should be of help when going through this post.
Server.JS -- /server.js
If you've done any coding with NodeJS or Express, server.js is pretty self explanatory. Only 30 lines long, server.js simply sets up our http server, connects to socket.io and routes our socket connections to our SocketHandler script. Express also has a static function making it very easy to server our public folder with just a line of code.
The hosted site (http://quadpong.xyz) is served behind NGinx as well, but I chose to use Express to host the static files as it ends up being easier to configure for a small project like this one.
That's it for the entry point to the application. We'll now go all the way down to our Math, Physics and Game Object libraries and take a look at what makes up the core of the application.
Libraries -- /app/lib
First let's look at physics.js. Primarily a Vector2 library (Math/Physics, whatever!) it also contains the Bounding Box class used by our Game Objects.
Disclaimer: I am not a mathematician and never took Linear Algebra in college, so my knowledge of Vector Mathematics is limited to what I've taught myself on Khan Academy and from what I've needed to know from working with 3D and 2D programming in the projects I've done. I'm sure there are much more efficient formulas to do some of the things in this library. However, it does do the things I need it to.
So, with that out of the way, I went with a system that used Vector2 as a coordinate, rather than describing the beginning and end of the vector. All Vector2s are assumed to start at (0,0). I kinda just made it up as I went, so its not a full fledged Vector2 Library and if you want to make it one, you'll need to do some work to it. This is one area that could use a lot of refactoring and could be made much better.
So down all the way in /app/lib/physics.js we see on line 61, the Vector2 Class constructor. Vector2s are basically x and y coordinates, with tons of functions to return the data needed.
In the preceding lines you'll notice a bunch of private functions, like getDirectionFromVector and getVectorFromDirection, and you can take a look at those, but they're mainly just assisting functions for the main Vector2 functions.
There is one function before the constructor that I'd like to take note of: degreeAdjuster. It's been a long time since I've done trigonometry, and I'm sure there's a better mathematical solution for this, but a simple way to adjust for the way Math functions round when given a value is to determine which quadrant you're in, and adjust the output degree accordingly so that you get an accurate direction from 0 to 360 degrees. In other words, this makes sure that when I get a direction, it's always a degree between 0 and 360 which is what I need for the other functions. It is a little hacky, but get's the job done.
On the left, are the normalize, copy, and getMagnitude functions. Normalize is used to take a vector of any size and mutate it to its unit vector, having a magnitude of 1. This function also uses a helper function roundToPlace, which simply takes a number and rounds it to a specific number of decimal places (in this case 2).
Copy is used quite often, and is a very useful function for making a clone of a Vector2 to mutate and return for uses where you do not want to affect the original. You'll see this used a lot in the classes file.
There is also the getMagnitude which is just a wrapper for our private getMagnitudeFromVector function, which given a vector, will calculate and return its magnitude.
Finally let's look at a few more conspicuous functions, rotateDegrees and reflect, which may not be quite as intuitive as some of the other functions. rotateDegrees is used to rotate the current Vector2 around the origin a certain amount and then returns a new Vector2 based on that amount. This is one of those functions that doesn't quite fit the pattern of some of the others and could use a rework.
The library also has a reflect function which given a normal vector, will reflect the current vector across that vector. Think about a ball bouncing against a wall at an angle. If the normal is the vector coming out from the wall, then it uses that vector to calculate the new vector of the bouncing ball after it hits the wall. This function is primarily used when calculating the bounce of the ball against other game objects.
So enough about the Vector2 class, you can look on GitHub if you want to review the other functions, but they're fairly straightforward.
Next in our Physics.JS file we find the BoundingBox class. This is a simple rectangular bounding box used to determine our collisions in the game. You can examine the constructor on line 251, with a generateBoundingBox function below it, which generates the x1, y1, x2 and y2 values from the position, width, and height.
I won't spend a ton of time talking about the bounding box as it is fairly simple. There is an update function and the intersects function, which is used to determine if two bounding boxes intersect, seen here.
Other than that there are a few degree to radian conversion functions, helper functions and then it's packaged it all up in module.exports.
There are also couple other scripts of note here in our library: a math_extentions script which when required, will add a Clamp function to the Math class which simply clamps a value between a min and max, and a helper.js script which simply has a few guid creation functions. Alright! Now onto our classes.js script!
Classes.js contains three classes: GameObject, which is the main parent class for our hierarchy of classes, and the Ball and Paddle classes which are children from it. The GameObject class contains a Vector2 position, a Vector2 velocity, a name and a BoundingBox. It also has one function at the moment: intersects, which checks another GameObject's bounding box against its own using the function from our physics.js script.
Below is also the Ball class. It inherits from GameObject (on lines 39 and 45) and has a few of it's own fields like colliding (boolean check if it's currently colliding with something), colliding_with (GameObject reference of the other object in a collision), and game (a reference to the game the object is in).
Above is also the updatePosition function, which given a delta time, updates the position based off of the velocity, updates the bounding_box and checks if it is still colliding with anything after it's moved a bit. There are a few more functions:
- bounce_x and bounce_y - swaps the x portion of the velocity and the y portion respectively
- reset - sets the ball position to (0,0)
- start_velocity - sets the initial velocity at the start of a point
- checkBounds - determines if the ball is out of the playing area and scores or bounces accordingly.
Note: One "to do" I have is when refactoring in the future, to be consistent with my snake (underscore) and camel cases. I mix and match them for my function names and it's not the best looking.
There's also one more function we'll look at for the Ball class, bounce_against, which I feel deserves a look at.
Bounce against takes a game object that it is colliding with and checks if it is already colliding with something. If not, it will collide with the game object and bounce against it, reflecting over the normal vector. In addition, I wanted to add a portion of the paddles velocity to the ball, as well as the vector from the center of the paddle to the ball, to allow the player to have some control over the direction of the ball.
At the end of the function I added other_velocity, which is the velocity of the paddle the ball is colliding with, and the additional_velocity, which is a calculated velocity vector based on the position on the paddle where the ball was hit, and then I normalized the vector and set its magnitude to the original magnitude so the ball speed is not changed throughout the game.
Finally in our classes script is the Paddle class on line 161. The Paddle class is the player in a game, it holds the player's remaining life in a game, the action a player or computer is taking, as well as a bunch of other variables that we'll look at in some of the functions.
The Action is the primary directive that a player on the client needs to send to the server. Each Paddle has 3 action states: Move Left ("L"), Move Right ("R"), and Nothing ("N"). These are updated from the client or the computer action function, seen below.
Players can be either human or computer, and if the player is a computer, it will be directed in the game loop to calculate its current action based on the ball position. It's not complex AI by any means, but for the purposes of this game, it works quite well.
The other functions are there to simply calculate and update the position of the paddle based on the action, and update the width of the paddle to shrink it when a point is scored against that player.
In addition there are a few private helper functions used to calculate a starting ball direction, the positive_vector for a player which is used to determine which direction is "Right" in respect to the normal vector being forward, and a function to help generate the appropriate bounding box for a given player number.
That's it for the classes script. This is a simple game, so there are only have 2 main classes: the ball and the player. For a larger game, I'd recommend breaking these down into smaller files as this could easily get unweildly in a single file.
The Game Loop -- /app/game_loop.js
Now we're going to hop up a level to the Game Loop. The game loop contains the bulk of the game flow and logic on the server. Game extends the Event Emitter object, so that it can use those functions to emit updates to the client once per loop.
In /app/game_loop.js we can see the constructor and main logic for our game loop class. On line 10, is the initial object for the game that houses all of the data and functions. It sets up a few variables such as the game id, and some references to the players and ball object which are in the game.
The main game loop starts on line 28, in function gameLoop. This function is run as the update function. This game is built in NodeJS, so it's prudent to be careful of how much processing is chucked on the event loop, and while the game is doing all of the calculations synchronously, I didn't want to repeatedly call the gameLoop over and over without any break for the event loop to work on other things, because that would eat away all of the CPU I have available for the application and it wouldn't be able to run multiple games per process.
So instead of calling the loop again instantly I used setTimeout to queue another call of the function on the event loop stack a specific time later (generally 15 ms from the time the current loop was called which equates to roughly 60 fps, this time is set in the config). The time between when this loop runs and the next call becomes our delta time as set on line 29.
The entirety of the game loop is pretty self describing within the function:
- Set our Delta Time and start time
- Move our Players
- Move the Ball
- Check for collisions between the players and the ball
- Check if the ball is out of bounds
- Set our end time
- Check if the game was won
That's the main game logic. I also added a check to make sure that any loop over 30 ms gets logged so I can see if the server is getting slow as well, and then the function emits out the player and ball data to the socket handler, and checks if the game is over by checking the scores.
Finally it queues up another loop if the game is still active (status === 2) a few milliseconds later.
There is a wide range of both private and public functions on the game object, but we'll just take a look at a few of them, which are of note.
Move player paddles, move ball and check collisions are pretty self explanatory; they iterate over the stored objects, updating their positions, and then checking bounding boxes of the player against the bounding box of the ball.
The end of game function I'll show since it emits the survivor out to the server side socket handler, which we'll look at in a bit. This function checks the life of each player and if only one survivor remains, that player is emitted out to the client in a notification that the game is over. The game status is then set to 3 indicating that the game is done, and can be cleaned up.
For public functions, some notable ones include, addPlayer and addComputer which, as you can imagine, add a player and a computer respectively to the game, and gameStart which puts the game into play and begins to run the game loop. As you'll notice in the gameStart function, it sets the status to 2, which is used by the game loop to determine whether or not to continue to run.
Another function of note is the updatePlayerAction function, which when the game is started, will change the action of the player in the game. This is the way the client sends data to the running game and how state among the players is changed.
And then the on point event will score against players and notify the client.
Thats pretty much all there is to the game loop. It acts out a very basic game system, updating each actor in the scene, and calculating all of the simple physics that happen. For a small game like this, the logic is fairly compact, so it don't use much processing power on each loop.
If you wanted to take this and do some complex pathfinding algorithms, it would be worth offloading that to a separate thread with a C or C++ library for NodeJS. I haven't had the need to create my own library yet, but I know it's possible, since it's what makes the callback model work.
The last main section of code is the socket handler for the server: server_sockets.js.
Socket Handler -- /app/server_sockets.js
The socket handler script, server_sockets,js, is the main communication layer for the server side. It primarily talks with the parallel script on the client side, socket_handler.js to send web socket communications back and forth via emitted events.
When a player connects, they hit handleSocket, which is triggered by socket.io when the client connects via our server.js connection event. The client will then emit a "new_player" event, which is caught in the below function on our socket.
In this event, the handler checks if any games with the game_id passed in have a player with the player_id passed in, and if so, uses a bit of reconnection logic to determine which state the client game should be in, and what players are in that game.
The socket will then emit "reconnect" with the data, letting the client know it's reconnecting and it should take appropriate actions. The player is added to the socket "room" for the game they are rejoining, so they can receive the appropriate events. It'll also emit a "sync_players" event, to make sure the client is aware of all players in this game. However if the player is not reconnecting, it'll simply create some credentials for the player, namely a player ID #, and send that back to the player and treat them as a new user.
There is also have a disconnect event, a leave game event, and a start game event which all are fairly self explanatory. When emitted a game id for leave and start, the handler will either drop the player from the current game or try to start the waiting game that the player is in, and if it has enough players, start it up.
The next event below those three is our join game event. In the current iteration of the game, a player cannot specify which game they would like to join. They simply request to join an open game, and the server will find an empty game in the games list to pair them to.
Here the socket server searches for a game_id that has open slots, adds the player, and then emits back out a bunch of data. The logic is as follows:
- First, have the player's socket join the game room broadcast
- Emit the game settings to the player
- Send back information about the game they are joining
- Emit to all players via io.emit (a broadcast) the new game list data
- Emit to the entire room for that game the new player joining
- If the game is full, emit back to the player trying to join that the game is full
And if there is a server error the handler notifies the player of the reason for the error.
There are a few more events which handle: updating actions, creating games, adding computer and human players, requesting games lists and so on. The rest of these events are pretty straight forward. It either modifies the game or updates the client back with information.
Other Scripts of Note
So that's the bulk of the back end portion of the application. In addition to the main scripts, there is also have a configuration folder in /app/config which holds our game settings configurations, as well as some mocha tests in /app/test. Currently there is just a test to make sure the physics library works as expected, but more should be added to ensure our scripts work properly.
I hope you enjoyed this lengthy post about the server side component to the game. The next post should be much shorter and will talk about the client side, the canvas element, the socket handling, and some other small libraries I used to handle input and the like.
Feel free to follow me on Twitter, and check out the GitHub with all of the code for you to fork and build on!