

//chat.print("Running torch.js...\n");

var i, b, base, delayer;

if (!(base = this.find(".../base")))
	return;

// Kick off the random number generator, since it's not seeding
//  properly for some reason...
//for (i=0; i<5; i++)
//	Math.random();

b = base.bounds;
b = [(b[0][0]+b[1][0])*0.5,
	  b[1][1],
	 (b[0][2]+b[1][2])*0.5];

this.origin = b;

chat.print("Lighting "+this.name);
// chat.print("Initializing torch '"+this.name+"' at "+b);

delayer = new Object;
delayer.count = Math.random() * 100 + 1;
delayer.where = b;

delayer.timestep = function(now) {

	if (--this.count > 0)
		return;

	var snd;  // Actually makes it a property of 'this', I think.

	snd          = Sound("fire-loop.wav");
	snd.position = this.where;
	snd.volume   = .8;// Or whatever
	snd.near     = .5;
	snd.far      = 50;
	snd.repeats  = 0; // Play forever
	snd.play();

	removeAnimator(this);	// We've done our bit.
}
addAnimator(delayer);


this.f = [SubWorld("FlameUp.aer").add(),
		  SubWorld("FlameDown.aer").add()];

this.n = 0;

for(i=0; i<2; i++) {
	this.f[i].position = b;
	this.f[i].visible  = (i == 0);
}

this.flakes = [];
for(i=0; i<6; i++)
{
	this.flakes[i] = SubWorld("Flake.aer").add();
}

this.timestep = function(now) {

	var i, o;

	this.n ^= 1;

	for(i=0; i<2; i++)
	{
		this.f[i].visible = (i == this.n);
	}

	o = this.origin;

	for(i=0; i<6; i++) {
		var f, a;
		a = now + i + o[0] + 1000.;	// o[0] = x = random offset between torches...
		f = this.flakes[i];
		f.fixed = true; // So as not to slow down collisions for other things
		f.setPosition(o[0] + Math.cos(a) * .1, o[1] + (a*3.7)%1.4, o[2] + Math.sin(a) * .1);
		f.orientation = Rotation('Y', a * 7);
	}
}

addAnimator(this);

