Thursday, July 25, 2013

box2dweb, Refactored for Flexibility and Enterprisiness

I wanted to try my hand at making a physics-based HTML 5 game, so I thought I would start by refactoring the box2dweb demo (not working at the moment) into a set of loosely coupled compoenents as a springboard for a custom game engine architecture.  For various reasons, not all of which involve having a working game sooner, I'm gradually removing any dependency on box2dweb from the majority of the code.  Eventually, only adapters will interact with 3rd party software, except for requireJS.

What I have created so far is a refactored version of the demo (with a couple minor behavior changes that have to do with the first game I want to create with it) with a broken Maven build.  :)  The components so far are:
-main - entry point for loading modules.
-Input - takes input from the user and translates it into actions for other components to act on.
-renderingService - displays the state of the game to the user (i.e., does graphics), and also the inverse (translates x,y coordinates on the screen into x,y coordinates in the game world--more on why another day).  The implementation uses box2dweb's Debug view thingy, but the interface is implementation-agnostic.
-physicsService - does the physics.  The implementation uses box2dweb  but provides enough of an interface that other components should be able to use it without knowledge of box2dweb.
-EntityFactory - creates the entities--anything with location in space.  Entities are comprised of a physicsComponent that is a box2dweb entity, plus whatever extra data they need.  Later, they will probably have a graphicsComponent as well.  The box2dweb entity itself is exposed as-is, even though that means the app-at-large is dependent on box2dweb.
-game - runs the show.  Specifically, it does project setup, the game loop, and the master update routine.

I'm using requireJS to wire components together.  The Maven build is backed by the Javascript Maven Plugin.  I have had a lot of trouble getting this to work.  My current problem is that mvn install deletes require.js for some reason, making subsequent builds fail.

I hope to add a unit test or two later, then get serious about making a game with this.  It should be fun.

No comments:

Post a Comment