Porting "Burst Fish" to Prefix
Sunday, 25 September 2011
A couple of days ago I stumbled on this great little JavaScript game tutorial and I thought it would be interesting to port it to Prefix as an example of something a little more "real world" than the hundreds of unit tests I've been using.
This post is about Prefix a modern language that compiles to JavaScript. A language where C# is the inspiration - not the goal. Read more on the Prefix Project Page.
Burst Fish is a tutorial game written by Seb Lee-Delisle that I found on the excellent creativejs.com site. The original tutorial can be found here http://creativejs.com/tutorials/create-an-ipad-optimised-game/.
I thought this would be an excellent project to port to Prefix as both an example project and as a first experiment to see how Prefix works in the real world. Aside from one missing piece of functionality and a few bugs in the compiler itself, porting it went remarkably well. Here it is:
- View the Game in your Browser (You'll need a webkit based browser - Chrome or Safari)
- Download the Source
(Please note I haven't released Prefix yet so although you can look at the source you can't compile it yourself yet. If you're interested in trying the compiler ping me on Twitter or email me).
To show how the Prefix code looks, here's an extract of the Emitter class used to generate the exploding fish particles:
// Emitted is used to generate the explosion particles
class Emitter
{
// Constructor
public Emitter(dynamic domContainer)
{
this.domContainer = domContainer;
}
// Fields
private dynamic domContainer;
private Particle[] _particles = new Particle[];
private Particle[] _spareParticles = new Particle[];
// Called by main game loop to update particules
public void update()
{
for (var i=0; i<_particles.length; i++)
{
var particle = _particles[i];
if (!particle.update())
{
_spareParticles.push(particle);
}
}
}
// Make an explosion at specified position
public void makeExplosion(double xpos, double ypos, double zpos)
{
// Just create bunch of particles
for (var i=0; i<30; i++)
{
var particle = makeParticle();
particle.init(xpos, ypos, zpos);
particle.update();
}
}
// Make a particle by initializing a new one, or by grabbing one from
// the spare list
private Particle makeParticle()
{
Particle particle;
if (_spareParticles.length>0)
{
particle = _spareParticles.shift();
}
else
{
particle = new Particle(domContainer);
_particles.push(particle);
}
return particle;
}
}
// class Emitter
var Emitter = function()
{
this._particles = [];
this._spareParticles = [];
};
Emitter.typeName = 'Emitter';
Emitter.prototype.domContainer = null;
Emitter.prototype._particles = null;
Emitter.prototype._spareParticles = null;
Emitter.prototype.ctor$1 = function(domContainer)
{
Emitter.call(this);
this.domContainer = domContainer;
return this;
};
Emitter.prototype.update = function()
{
for (var i=0; i<this._particles.length; i++)
{
var particle=this._particles[i];
if (!particle.update())
{
this._spareParticles.push(particle);
}
}
};
Emitter.prototype.makeExplosion = function(xpos, ypos, zpos)
{
for (var i=0; i<30; i++)
{
var particle=this.makeParticle();
particle.init(xpos, ypos, zpos);
particle.update();
}
};
Emitter.prototype.makeParticle = function()
{
var particle;
if (this._spareParticles.length>0)
{
particle = this._spareParticles.shift();
}
else
{
particle = (new Particle().ctor$1(this.domContainer));
this._particles.push(particle);
}
return particle;
};
The game works as well as the original and although there are a few bits in the generated code that could be cleaned up, as a first attempt I'm fairly pleased with it.
Leave a comment