Jump to content

Phaser+Nodejs+MongoDB+Requirejs


aharryhughes
 Share

Recommended Posts

A friend of mine and myself just recently graduated from the Ironyard(a full-stack web development boot camp) and have begun work on our final project, choosing to do a game through Phaser. My friend had already begun work on the game and had it working, but not to the scale we wished to bring it. The end goal was to use Nodejs to navigate through a login, sign up, and game page, saving the user info(as well as the game state) to a mongoose/mongodb. We also wanted to use socket.io to make it multiplayer, working on mobile and desktop.

In its state before beginning this endevour, it had six game states(Boot, Preloader, MainMenu, level outside, level house) all as functions with prototypes, rendered by placing them in scripts in the head of the index.html file and then running a script to add all these to the game state manager on the windows load. link here https://github.com/AHarryHughes/Top-Down-Shooter/tree/master

When we added the Node.js server, we had to add all the game and Phaser files to the static directory in nodejs, serving them up to the browser, the problem with this is that now we were unable to modularize the code. I attempted to utilize Requirejs but it can't seem to find my src file instead I just get an error reading "404 main.src not found" when my src="main.js" inside the script. That current code is here https://github.com/AHarryHughes/Top-Down-Shooter/tree/modPt3

I have also seen that browsers are starting to support type=module in scripts and am thinking this may be a path for better modularization of our game, but I'm looking for advice on the subject if anybody has experience using it.

Another problem I see in the distance is saving the game to the MongoDB server, it is a wave based game utilizing tower defense and gun purchasing, so the only thing to save would be which wave, what tower is placed and where, as well as what guns the player has. I'm having trouble seeing how the phaser game will be able to communicate with the server.

Any advice would be greatly appreciated, I'm new to this blog but I've gathered a lot of helpful information thus far from other posts on this subject, but it seems my problem might be a little more specific than any other posts I've read, if I'm wrong in this, a point in the right direction would help just as much, thank you.

Link to comment
Share on other sites

Regarding the communication of the DB data, that's very simple : put the GET/POST calls in the client code, which will run once on the client side. For example, you can write that in the BootState the client code will make all necessary calls to the server (get my save, for instance). On the server side, make a route for the "get my save" call and return it. Once the client code receives the data, it goes on to the LoadState and loads appropriate assets to match player wave. Nothing fancy here, or maybe I misunderstood your question.

I do something similar in my game where I ask the server for the hero and settings of the player in the BootState, then continue with the LoadState, loading proper assets and configuring the game accordingly.

Regarding modules, I'm not sure what you're asking for. Is it about having a clean codebase or is it about having separate node modules? Did you check that : https://github.com/belohlavek/phaser-es6-boilerplate ?

Link to comment
Share on other sites

  • 1 year later...
On 9/5/2017 at 2:13 AM, Skeptron said:

Regarding the communication of the DB data, that's very simple : put the GET/POST calls in the client code, which will run once on the client side. For example, you can write that in the BootState the client code will make all necessary calls to the server (get my save, for instance). On the server side, make a route for the "get my save" call and return it. Once the client code receives the data, it goes on to the LoadState and loads appropriate assets to match player wave. Nothing fancy here, or maybe I misunderstood your question.

I do something similar in my game where I ask the server for the hero and settings of the player in the BootState, then continue with the LoadState, loading proper assets and configuring the game accordingly.

Hello! Could you possibly elaborate on how you achieve this? From my understanding, calls to servers in JS are asynchronous, so how do you get the raw data into your Phaser game and how do you make the game wait until the data has arrived to move to your LoadState?

Thank you!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...