Jump to content

Splitting up a phaser state file?


xronn
 Share

Recommended Posts

Hello! 

I'm creating a game which is open world, so everything you might loads across 10 levels and 10 different states I load into one single "game" state. This means that now I'm pushing on with development I'm start to amass a close to 1,000 lines of code inside my "create" function on my main" game" state. I was wondering if I could take some of the code e.g. the 150 lines used to manage my world, collision etc.. and pop it in it's own file. Then I could just include it knowing that all my world goodies are stored in it's own file?

Thanks!

Link to comment
Share on other sites

One nice Phaser feature is that you can create a file with all the assets you want to load (which are not game logic, in the end), and pass this "asset pack" file to the loader, instead of each asset you want to load.

 

See example here : http://phaser.io/examples/v2/loader/asset-pack

Forum post : http://www.html5gamedevs.com/topic/6807-new-phaser-asset-pack-feature-please-test/

Doc : http://phaser.io/docs/2.4.3/Phaser.Loader.html#pack

 

Nice way to remove lots of lines from your code.

Link to comment
Share on other sites

This is cool thanks! I'll take a look at this in more detail. But is there any way to break up the game logic though so to include, players/NPCs in a file, world in another file, inventory in another etc... Be useful else I've got a massive game.js file with 100s of comments ;)

Link to comment
Share on other sites

Well, it's not really related to Phaser but rather to code design... Without seeing your code it's very hard to help. Try to create classes or factories with explicit responsibilities. For example, you could create an InventoryManager class which deals with all that's inventory-related, a PlayerFactory which will manage Players instantiation etc. If you have a complex map you could create your own Map class and put all related code within, etc.

 

It's not a Phaser debate IMO : it comes down to how you like to code, really.

Link to comment
Share on other sites

I think this brings up a lot of topics, like Skeptron said.

 

Managing multiple files: Are you using lots of script tags placed in order in your HTML file? Are you using shell scripts or batch files? Are you using gulp or grunt to concat all your JS files? Or something like browserify or webpack? (personally, I like the last two)

 

Classes: Chances are your custom sprites could be their own classes. Ditto any particles, possibly any emitters. Each state could be its own class as well.

 

Managers: A platformer game I'm working on has a series of managers: spawning (for seeing if the player has died and needs to be un-killed), death (for making sure the player dies the right way or, in fact, can be killed at all), screen flashes, keyboard combos... each of those is in its own file.

 

I think a lot of it has to do with how you think, since that's all this program really is: a concrete way of expressing what you want your game to be.

Link to comment
Share on other sites

Thanks for the info above, I'll have a look into them. I didn't know if there was something like a php include I could use. 

Just so I could cut a snippet out then include a link to that file in the middle of the create function. I don't really want to start re-writing everything it was just to compress my game.js file 

Link to comment
Share on other sites

If you want to avoid refactoring your code, you could concat all of your files with a command line tool using nodejs.

 

https://github.com/knicklabs/node-concat

 

I would though recommend using task management with either grunt or gulp.  

 

It would take you some time to learn how to use nodejs / gulp / grunt if you never have before, but it is WELL WORTH  your trouble.  make no mistake, it's a huge time saver.

Link to comment
Share on other sites

if it's that big, I'm wondering if it would be worth considering *not* loading everything at once. I'm assuming you can dynamically load phaser state's with require/browserify etc? obviously it'll slow things down to load in places but you could integrate this into the gameplay with cutscenes/static screens maybe? like you get to a castle gate and it loads the interior whilst you're talking outside. depends how big your game is i guess and how much you need to manage memory.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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