Created
July 5, 2010 11:32
-
-
Save banthar/464262 to your computer and use it in GitHub Desktop.
fire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash.events.Event; | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
private class IPoint | |
{ | |
public var x:Int; | |
public var y:Int; | |
public function new(x:Int=0,y:Int=0) | |
{ | |
this.x=x; | |
this.y=y; | |
} | |
} | |
class Fire extends Bitmap | |
{ | |
static var FIRE_SPEED=0.8; | |
var fifo:Array<IPoint>; | |
private var state:Void->Void; | |
private var visited:BitmapData; | |
public function new(bitmapData:BitmapData) | |
{ | |
super(bitmapData); | |
} | |
public function burn(x:Float,y:Float) | |
{ | |
if(state!=null) | |
return; | |
bitmapData=bitmapData.clone(); | |
fifo=[new IPoint(Math.ceil(x),Math.ceil(y))]; | |
state=fire; | |
visited=new BitmapData(bitmapData.width,bitmapData.height,false,0); | |
addEventListener(Event.ENTER_FRAME,tick); | |
} | |
private function collapse() | |
{ | |
var changed=false; | |
for(x in 0...bitmapData.width) | |
for(ry in 0...bitmapData.height-1) | |
{ | |
var y=bitmapData.height-ry-1; | |
var c=bitmapData.getPixel32(x,y); | |
if(c&0xff000000==0) | |
{ | |
var n=bitmapData.getPixel32(x,y-1); | |
if(n&0xff000000==0) | |
continue; | |
if(Math.random()<0.1) | |
n=0; | |
bitmapData.setPixel32(x,y,n); | |
bitmapData.setPixel32(x,y-1,0); | |
changed=true; | |
} | |
else | |
{ | |
if(y<bitmapData.height-3 && (bitmapData.getPixel32(x-1,y+2)&0xff000000==0 || bitmapData.getPixel32(x+1,y+2)&0xff000000==0)) | |
bitmapData.setPixel32(x,y,0); | |
} | |
} | |
if(!changed) | |
state=null; | |
} | |
private function fire() | |
{ | |
if(fifo.length==0) | |
{ | |
visited.dispose(); | |
visited=null; | |
state=collapse; | |
return; | |
} | |
var len=Math.ceil(fifo.length*FIRE_SPEED); | |
for(i in 0...len) | |
{ | |
var p=fifo.shift(); | |
if(p==null) | |
return; | |
bitmapData.setPixel(p.x,p.y,ashColor()); | |
visited.setPixel(p.x,p.y,1); | |
var neighbours=[ | |
new IPoint(p.x+1,p.y), | |
new IPoint(p.x-1,p.y), | |
new IPoint(p.x,p.y+1), | |
new IPoint(p.x,p.y-1), | |
]; | |
for(n in neighbours) | |
{ | |
if(!isInBounds(n)) | |
continue; | |
if(visited.getPixel(n.x,n.y)!=0) | |
continue; | |
if(bitmapData.getPixel32(n.x,n.y)&0xff000000 ==0) | |
continue; | |
var c=bitmapData.getPixel(n.x,n.y); | |
bitmapData.setPixel(n.x,n.y,fireColor()); | |
if(Math.random()<0.9) | |
fifo.push(n); | |
else | |
fifo.unshift(n); | |
} | |
} | |
} | |
private function fireColor() | |
{ | |
var c=Math.random()*0.50+0.25; | |
return Math.ceil(c*255)*0x000100|0xff0000; | |
} | |
private function ashColor() | |
{ | |
var c=Math.random()*0.1+0.25; | |
return Math.ceil(c*255)*0x010101; | |
} | |
private function isInBounds(p:IPoint):Bool | |
{ | |
return p.x>=0 && p.y>=0 && p.x<bitmapData.width && p.y<bitmapData.height; | |
} | |
private function tick(?_) | |
{ | |
if(state==null) | |
{ | |
removeEventListener(Event.ENTER_FRAME,tick); | |
return; | |
} | |
bitmapData.lock(); | |
state(); | |
bitmapData.unlock(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment