dparis / phzr

A ClojureScript wrapper for the Phaser HTML5 game framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is this library compatible with Servant?

Jarzka opened this issue · comments

Hi

I tried to use this library along with Servant. Servant makes it possible to easily use web workers or threads in ClojureScript, a very important feature in game programming. It compiles all code in to one single JS file, which helps to share context, but it has some difficulties to work with phzr, since both the main UI thread and web workers are started from the same file.

As soon as I require phzr in to my main.cljs file I cannot run it as a web worker using Servant because "document is not defined". I think this is caused by the fact that whenever I include phzr, it runs some initialisation code which calls the document object. Since this object is not defined in web worker context, I cannot include phzr in my project.

Do you have any plans to make this library "Servant compatible"? I think it should be possible to achieve by modifying the code in a way that it does nothing until some initialisation function is called.

@Jarzka Hello! Thanks for trying out phzr!

I've not heard of Servant, so I don't have any specific plans to make phzr compatible with it. That said, Servant looks cool and it'd be great to support web workers.

As far as I know, phzr doesn't do any kind of initialization specifically. It's just a very thin, generated wrapper around the phaser javascript library, and at this point has almost no clojurescript-only code. Have you tried user the raw phaser javascript library with Servant? It's easy for me to imagine that phaser's own initialization code requires the DOM. If so, unless phaser provides a way to disable that initialization logic or bypass the DOM requirement, there's sadly not much phzr can do.

A thread on the phaser forums suggests that phaser itself might be able to included in a web worker context, but that normal messaging between workers and the main rendering thread wouldn't be performant enough to be useful. Maybe that's something addressed by Servant?

Another thing to look into is the way cljsjs packages the external object declarations for phaser. I doubt that would trigger any kind of dependency on the DOM, since it's just declaring a bunch of empty objects and functions to prevent the compiler from mangling the symbols.

In any case, if/when I get some time I'll poke around this issue. Before my schedule went crazy, I had some ambition to make phzr a really nice solution for making HTML5 games and easy web worker support would be a great feature. 😄