My game wants to communicate back to a server. Where is the best place to do this in Phaser 3 using ES6? (can someone please point me to an example, or give me a little hint in the right direction, thanks)
For example I want to have:
class MyAPI
initialize(){} // calls fetch to the server and gets a user token
sendGameScore(){} // sends the user token and score to the server
reportSomethingFromAnotherScene(){} // etc…
getServerTime(){} // etc… but probably handled in the intialize
etc…
After the MyAPI.initialize call, this MyAPI object would hold things like a user token that it sends with every other method - in reality it’s a bit more complicated that this (the user token is really another whole sub-system) but this is the basic gist of it.
Multiple game scenes would be calling these methods, so I would normally implement this kind of thing as a global object with a single instance for the whole app, I am just new to ES6 & Phaser though, so not sure how the best way to go about this is. eg. Is the Phaser 3 registry the best place, or is that more for simple data only objects, or variables? Should I just store the user token in the registry and not have an MyAPI object at all? ie. just static methods that pull the user token out of the registry each time. Store it as an global object on window/document? etc…
My main game scene currently gets recycled on mode change. I currently don’t have a single scene that lasts the whole game. Again I am not sure if that is the best way to structure a Phaser 3 project… but I haven’t found a really good ‘full game’ example yet, that deals with all of these real-world things.
Any hints and tips on this matter would be much appreciated! Thank you.
PS: I started my project with phaser-plus, so it has a ES6, webpack, Phaser 3 stack.