Monday, January 3, 2011

Namespace, Part Deux...

Okay, well, that didn't exactly go as planned.  While setting up the namespace, I have also been trying to clean up the Linker.  I have been removing the calls to Engine.initObject(), but I just realized (after completing 95% of the refactoring) that it was going to blow chunks the first time I tried it.  It comes down to the fact that I'm using Base.js by Dean Edwards to set up my OO model.  As such, a class definition looks like:

var ClassB = ClassA.extend({
   ...
});


Due to this, ClassA must exist, otherwise calling the extend() method will fail with an exception.  There has to be some sort of delay before it checks to see if ClassA exists!  Well, after some hemmin' and hawin' I think I've settled on an acceptable alternative.  Now each class will look like this:


var ClassB = function() {
   return ClassA.extend({
      ...
   });
}

Not as pretty as before, but I can eliminate the need for the Linker to do as much work as it has been doing.  This will cause a lot less hair pulling when trying to get your game running.  Plus, instead of R.Engine.requires(), I'm changing it (so soon?) to something like the following:

R.Engine.define({
   "class": "R.collision.OBBHull",
   "requires": [
      "R.collision.ConvexHull",
      "R.math.Point2D"
   ]
});

This goes at the top of each class file.  What this does is it allows the engine to know that R.collision.OBBHull has two dependencies.  It can wait until those two dependencies are initialized (loaded and their dependencies are resolved) before initializing.  For those who wish to know, when it's time to initialize:

R.collision.OBBHull = R.collision.OBBHull();

Tada!!  Thoughts?

3 comments:

  1. How Closure library can have goog.require() in the top of the file without having to nest dependencies like that?

    It seems that they have a cleaner and simpler to use model there. You simply declare:

    goog.provide('R.collision.OBBHull');

    goog.require('R.collision.ConvexHull');
    goog.require('R.math.Point2D');

    Done. That's all (ok, not quite, you also need a deps.js file).

    ReplyDelete
  2. I guess that's where I'm different. I don't have the dependencies file. I'm trying to account for a fluid design that may change without having to rebuild the dependencies file repeatedly.

    I dunno... we'll see how this goes. Either way, I don't want to rollback all my changes now. =)

    ReplyDelete
  3. deps.js can be created dynamically by a service as you change files, sou you just don't worry about rebuilding the dependencies file (not that it is that hard). See:

    http://plovr.com/

    ReplyDelete