Little progress on my latest project. I’m running not into code issues–I wish I were as those are much easier to solve–but questions like should it really work this way and do you need combat in a game to create risk and why does internal monologue register as italics, anyway, I mean, who thought that up and so on. It’s frustrating, but it will pass.
Anyway, I put the source code to Easy Does It into github, right here. I hope it’s of use to somebody.
One big code style change I forced on myself over the last few months was using the prototypal model in deriving JS objects. For Foam, Fissure, and Pavo, I used classic inheritance and object creation: function objects and the new operator. It looked familiar enough from my days as a C++ developer, and when you’re trying to get a foot into the door, familiar is good.
However, in JS, classic inheritance feels like what it kind of is: an attempt to shoehorn a familiar pattern into a language that was built with something else in mind. Prototypal inheritance is that something else, and I have to say, I’ve come to enjoy it. I rebuilt my WebGL wrapper, Foam, into a prototypal library called Soar and I’m happy with the results. Easy Does It is all prototypal, as is the game I’m working on now. I haven’t seen a new operator in months.
For objects that I’m going to create more than one instance of–drawable objects in particular–I’ve found this pattern incredibly useful. No idea if it has a formal name, but it’s like having a static function on a C++ class.
GAME.enemy = {
init: function() {
// create shader object shared by all instances
this.shader = shader.create(...);
},
create: function() {
// create an instance and populate it,
var o = Object.create(GAME.enemy);
o.position = this.getRandomPosition();
o.damage = this.getRandomDamage();
return o;
}
};
...
// initialization code
// creates all shared resources
GAME.enemy.init();
...
// create lots of objects
for (var i = 0; i < lots; i++) {
list[i] = GAME.enemy.create();
}
...
// activate the shader for this type of object
list[i].shader.activate();
// draw all instances of this object
for (var i = 0; i < lots; i++) {
list[i].draw();
}
Changes to the base object are, of course, propogated to all instances. I find this oddly pleasing, more so than I find the idea of a fixed base “class” as in C++ and Java. I have to admit it’s taken some time to warm to it, but here I am. Good place. Good place.