Falling Sand With English Comments
/**
* Copyright Abarrow ( http://wonderfl.net/user/Abarrow )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/172e
*/
package
{
import com.bit101.components.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
[SWF ( width = 465, height = 465, frameRate = 70 )];
public class Main extends Sprite
{
private var _CANVAS:BitmapData = new BitmapData ( stage.stageWidth , stage.stageHeight , false , 0 ) ;
private var _ERA:PushButton ;
private var _WAL:PushButton ;
private var _SAN:PushButton ;
private var _WAT:PushButton ;
private var _CLICK:Boolean = false ;
private var _COLOR:uint = 0x836034;
private var _BX:int = 0 ;
private var _BY:int = 0 ;
public function Main():void {
addChild ( new Bitmap ( _CANVAS ) ) ;
_ERA = new PushButton ( this, 10 , 20, "ERA" ) ;
_WAL = new PushButton ( this, 120 , 20, "WAL" ) ;
_SAN = new PushButton ( this, 230 , 20, "SAN" ) ;
_WAT = new PushButton ( this, 340 , 20, "WAT" ) ;
_WAL.enabled = false ;
_ERA.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x000000 ; _ERA.enabled = false ; _WAL.enabled = true ; _SAN.enabled = true ; _WAT.enabled = true ; } ) ;
_WAL.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x836034 ; _ERA.enabled = true ; _WAL.enabled = false ; _SAN.enabled = true ; _WAT.enabled = true ; } ) ;
_SAN.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0xF7E779 ; _ERA.enabled = true ; _WAL.enabled = true ; _SAN.enabled = false ; _WAT.enabled = true ; } ) ;
_WAT.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x80A2F0 ; _ERA.enabled = true ; _WAL.enabled = true ; _SAN.enabled = true ; _WAT.enabled = false ; } ) ;
stage.addEventListener ( MouseEvent.MOUSE_DOWN , function ():void { _CLICK = true ; } ) ;
stage.addEventListener ( MouseEvent.MOUSE_UP , function ():void { _CLICK = false; } ) ;
addEventListener ( Event.ENTER_FRAME , RUN ) ;
}
public function RUN ( e:Event ):void {
//locks the canvas enabling many changes
_CANVAS.lock() ;
//sand from the top
for ( var I:int = 200 ; I < 250 ; ++ I ) {
if ( Math.random() < .1 ) {
_CANVAS.setPixel ( I , 60 , 0xF7E779 ) ;
}
}
//draws
if (_CLICK && 60 < mouseY && 60 < _BY ) {
for(var hx=-3;hx<4;hx++){
for(var hy=-3;hy<4;hy++){
line(_BX+hx,_BY+hy,mouseX+hx,mouseY+hy,_COLOR);
}
}
/*for ( var J:int = 0 ; J < 20 ; ++ J ) {
var R:Number = J / 20 ;
_CANVAS.fillRect ( new Rectangle ( _BX * R + mouseX * ( 1 - R ) , _BY * R + mouseY * ( 1 - R ) , 5 , 5 ) , _COLOR ) ;
}*/
}
_BX = mouseX ;
_BY = mouseY ;
//cycle through pixels from bottom left to top right
for ( var X:int = 0 ; X < _CANVAS.width ; ++ X ) {
for ( var Y:int = _CANVAS.height - 1 ; Y >= 0 ; -- Y ) {
var C:uint = _CANVAS.getPixel ( X , Y ) ;
//the pixel
if ( C == 0 ) {
//air
//nothing happens
continue ;
}else if ( C == 0xF7E779 ) {
//sand
var T:uint ;
var TX:int ;
{
T = _CANVAS.getPixel ( X , Y + 1 ) ;//pixel bellow
if ( T == 0 ) {
//there is air bellow us
//fall down
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}else if ( T == 0x80A2F0 && Math.random() < .5 ) {
//there is water bellow us
//half of the time the water goes above the sand
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
{
TX = X + Math.floor ( Math.random() * 7 ) - 3 ;
//tx=x + or - 1 to 3
T = _CANVAS.getPixel ( TX , Y ) ;
//a pixel near the pixel aka a wall or no wall
if ( T == 0 ) {
//no wall lets go
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}else if ( T == 0x80A2F0 && Math.random() < .8 ) {
//water between us
//lets switch spots
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}
}
}else if ( C == 0x80A2F0 ) {
//water
{
T = _CANVAS.getPixel ( X , Y + 1 ) ;
//the pixel bellow us
if ( T == 0 ) {
//there is air bellow us
//fall down
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
{
TX = X + Math.floor ( Math.random() * 7 ) - 3 ;
//tx=x + or - 1 to 3
T = _CANVAS.getPixel ( TX , Y ) ;
//a pixel near the pixel aka a wall or no wall
if ( T == 0 ) {
//no wall lets go
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}
}
}
}
}
//great we're done
_CANVAS.unlock() ;
}
public function line ( x0:int, y0:int, x1:int, y1:int, color:uint ):void
{
var dx:int;
var dy:int;
var i:int;
var xinc:int;
var yinc:int;
var cumul:int;
var x:int;
var y:int;
x = x0;
y = y0;
dx = x1 - x0;
dy = y1 - y0;
xinc = ( dx > 0 ) ? 1 : -1;
yinc = ( dy > 0 ) ? 1 : -1;
dx = dx < 0 ? -dx : dx;
dy = dy < 0 ? -dy : dy;
_CANVAS.setPixel32(x,y,color);
if ( dx > dy )
{
cumul = dx >> 1;
for ( i = 1 ; i <= dx ; ++i )
{
x += xinc;
cumul += dy;
if (cumul >= dx)
{
cumul -= dx;
y += yinc;
}
_CANVAS.setPixel32(x,y,color);
}
}else
{
cumul = dy >> 1;
for ( i = 1 ; i <= dy ; ++i )
{
y += yinc;
cumul += dx;
if ( cumul >= dy )
{
cumul -= dy;
x += xinc ;
}
_CANVAS.setPixel32(x,y,color);
}
}
}
}
}