Tuesday, February 12, 2013

Code Extracting for Fun (and less typing for small hands)

‹prev | My Chain | next›

I have a 350 line game candidate for 3D Game Programming for Kids. No kid (and no adult for that matter) is going to want to type in 350 lines of code. That's just crazy. Fortunately, much of the code is already boilerplate from templates in the ICE Code Editor. Some of the remaining code can either be placed in new templates or moved into a library. Tonight, I am going to move as much as possible out into a library to get a better idea of how large the actual code is.

It has been a while, but my fork of Mr Doob's code editor can be run locally as a simple node.js / express.js app:
➜  code-editor git:(gamingjs) ✗ node app
Express server listening on port 3000 in development mode
It would be nice if the order of the libraries did not matter, but, since my mouse library requires both Three.js and Physijs, I need to source them first, then my extracted mouse library:
<body></body>
<script src="/52/Three.js"></script>
<script src="/52/physi.js"></script>
<script src="/mouse.js"></script>
<script src="/52/ChromeFixes.js"></script>
<script>
The /52 libraries are local copies of the various libraries compatible with Three.js r52, which eventually make it up to http://gamingJS.com/ice.

Actually, I am able to move mouse.js ahead of the other two libraries if I add a timeout before monkey patching the built-in addEventListener():
function extendPhysijsAddEventListener() {
  if (typeof(Physijs) == 'undefined') return setTimeout(extendPhysijsAddEventListener, 5);

  var pjs_ael = Physijs.ConvexMesh.prototype.addEventListener;
  Physijs.ConvexMesh.prototype.addEventListener = function(event_name, callback) {
    if (event_name == 'drag') Mouse.addEventListener('drag', this, callback);
    pjs_ael.call(this, event_name, callback);
  };
}
extendPhysijsAddEventListener();
That does not seems particularly robust, so I will likely require readers to stick to proper ordering of the scripts (or add it to the boilerplate code).

With the mouse library extracted, my game is down to... 267 lines. Progress, but not enough. A bit more work eventually gets me down to 250 lines of code, with roughly 30 of it boilerplate. That is about 50 lines of code past my limit for a chapter. It is unfortunate, but not altogether unexpected. The game is moderately complex, after all:


The game includes a player (the red ball), movable ramps, stationary obstacles, screen boundaries, and a goal. On top of that, the recently added multi-level play is just too much. Indeed, if I remove the multi-level code, I get down to 200 lines of code. So it seems that, if I want multi-level games in the book, then I am going to need a separate chapter. I am not sure my editor is going to be happy about that.

The mouse handling code is in pretty ugly shape, but is probably good enough for my needs. So I commit that and put to the 3D Game Programming for Kids site. With that, I do not think there is much left for me in this chapter save to write the actual chapter. If a separate chapter is required, I may as well start on a new scoreboard library, which can be used to keep track of total time elapsed and score in the multi-level game. That is something for tomorrow.


Day #660

No comments:

Post a Comment