//--------------------FIRE ----------------
var FireWidth=10;
var Fire= CreateSurface(FireWidth, 12, CreateColor(100,0,0));
var FireArr=new Array(FireWidth);
var FireCol=new Array(256);
function InitFire()
{ //Here we define a matrix to hold values for "hotness"
for(var i=0 ; i<FireWidth; i++) {
FireArr[i]= new Array(Fire.height);
for(var j=0 ; j<Fire.height; j++) {
FireArr[i][j]= 0 ;
}
}
//Now we define a fire palette
for(var i=0;i<=7; i++) FireCol[i]=CreateColor(i*22,0,0); //Black to Red
for(var i=0;i<=15;i++) FireCol[i+7]=CreateColor(192,i*12,0); //Red to yellow
for(var i=0;i<=30;i++) FireCol[i+7+15]=CreateColor(192,192,i*7); //yellow to white
}
function DoFire()
{
var FHm1=Fire.height-1;
var FWm1=FireWidth-1;
//On ground level, create "hotness"
for (var i=0;i<FireWidth;i++)
FireArr[i][FHm1]= Math.floor(Math.random()*50);
Fire= CreateSurface(10, 12, CreateColor(0,0,0)); //Recreate the surface (we will strech and flip it)
for(var j=1;j<FHm1;++j)
for(var i=1;i<FWm1;++i)
{
//Ok, here we tell the pixels on top, to mix in a firelike manner
FireArr[i][j]= Math.floor((FireArr[i][j]+FireArr[i+1][j+1]+FireArr[i-1][j+1])/3.2 ) ;
//if (FireArr[i][j]<0) FireArr[i][j]=0; //We should check for boundaries... oh well.
Fire.setPixel(i,j, FireCol[(FireArr[i][j])] ); //Put the calculated pixel on the Surface
}
//Now we draw it on screen... and it looks like a sun... well, almost :/
Fire.rescale(30, 12);
var FireBlitPosX=147+Math.sin(GetTime()/1000)*100;
Fire.blit(FireBlitPosX,46);
Fire.flipVertically();
Fire.blit(FireBlitPosX,66);
}
//For the mixing in a firelike manner, uou can also try one of these:
//FireArr[i][j]= Math.floor((FireArr[i][j]+FireArr[i][j+1]+FireArr[i-1][j]+FireArr[i+1][j])/4 ) ;
//FireArr[i][j]= Math.floor((FireArr[i][j]+FireArr[i][j+1]+FireArr[i+1][j])/3 ) ;