forked from: 砂ゲー
水追加
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aHca
*/
// forked from kuma360's 砂ゲー
//水追加
package
{
import com.bit101.components.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
[SWF ( width = 465, height = 465, frameRate = 60 )];
public class Main extends Sprite
{
private var _CANVAS:BitmapData = new BitmapData ( stage.stageWidth , stage.stageHeight , false , 0 ) ;
private var _B0:PushButton ;
private var _B1:PushButton ;
private var _B2:PushButton ;
private var _B3:PushButton ;
private var _CLICK:Boolean = false ;
private var _COLOR:uint = 0x836034;
private var _BX:int = 0 ;
private var _BY:int = 0 ;
private const _graphicsData:Array =["89504e470d0a1a0a0000000d49484452000000300000000a0806000000f2e3dd4c0000001974455874536f6674776172650041646f626520496d616765526561647971c9653c0000010e4944415478dab495d111c2200c861326d011ea188e6037d04757700c57f05137d071ec08ba416c8aa91002ca1de60ea93fe123f9f12a1011e4c61457a0702ee5d78e167c9c3e6ef8a6f9a00d21cf91de03cecf7c187f97390c29a8f7ec84d398ef22f0fa0ec906d17508c8d0a67c63fd1f7c7f0388408ffb67c3a2f30e291d97abc82506595ac2199d36f5167cb966066df714fd0eb51eb936ea32b4fe8dd392efa0109cbc3b74a6ce33af450e158273cfc7a14a4f6ec508071511ba916bccd239b8c8dc9ac5ffd508572ac8ea5e1f125e7fc9ad52f1b935d6ada6c27c57ea5c8abb9c1039870be491733f6c267caee1ebbd72568e9fbc851832ffc93c07d2ce8679bab84433de36adf92f01060061a1bafe090c09b10000000049454e44ae426082"];
private var _lemmings:Vector.<LemmingSprite> = new Vector.<LemmingSprite>(_lemmingsNum, true);
private var _loader:Loader;
private function prepareAsset(n:int):void{
var bytes:ByteArray = new ByteArray();
var data1:Array=_graphicsData[n].split("");
var data2:Array=[];
var d1l:int = data1.length;
for (var i:int=0;i<d1l;i+=2){
data2.push("0x"+data1[i]+data1[i+1]);
}
var d2l:int = data2.length;
for (var j:int=0;j<d2l;j++){
bytes[j] = data2[j];
}
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,assetLoaded);
_loader.loadBytes(bytes);
}
private var _assetsMemoryBank:Vector.<BitmapData> = new Vector.<BitmapData>;
private var _assetsNum:uint = 0;
private function assetLoaded(event:Event):void {
var t:BitmapData = new BitmapData(_loader.content.width,_loader.content.height,true,0x00FFFFFF);
t.draw(_loader.content,new Matrix());
_assetsMemoryBank.push(t);
_assetsNum++;
if (_assetsNum<_graphicsData.length) {prepareAsset(_assetsNum);}
else {initAnimation();}
}
private function initAnimation():void {
addChild(new Bitmap(_renderData)) as Bitmap;
// addChild( new Stats() );
/*
for (var i:uint=0;i<_lemmingsNum;i++){
_lemmings[i]=createLemming(randomNumber(0,_stageW),randomNumber(0,_stageH),[0,1,2,3,4,5,6,7],_assetsMemoryBank[0],70,randomNumber(0,8),6);
}
*/
// addEventListener(Event.ENTER_FRAME, renderDisplay);
addEventListener ( Event.ENTER_FRAME , RUN ) ;
}
private function createLemming(x:Number,y:Number,imAr:Array,bmd:BitmapData,spd:uint,im:uint,imW:uint):LemmingSprite
{
var p:LemmingSprite=new LemmingSprite();
setProps(p, {x:x,y:y,imAr:imAr,imArL:imAr.length,bmd:bmd,spd:spd,ti:getTimer(),im:im,imW:imW,imH:bmd.height});
return( p );
}
private function randomNumber(low:uint, high:uint):uint{
return Math.round(Math.random() * (high - low) + low);
}
public function Main():void {
addChild ( new Bitmap ( _CANVAS ) ) ;
_B0 = new PushButton ( this, 10 , 20, "ERASE" ) ;
_B1 = new PushButton ( this, 120 , 20, "WALL" ) ;
_B2 = new PushButton ( this, 230 , 20, "SAND" ) ;
_B3 = new PushButton ( this, 340 , 20, "WATER" ) ;
_B1.enabled = false ;
_B0.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x000000 ; _B0.enabled = false ; _B1.enabled = true ; _B2.enabled = true ; _B3.enabled = true ; } ) ;
_B1.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x836034 ; _B0.enabled = true ; _B1.enabled = false ; _B2.enabled = true ; _B3.enabled = true ; } ) ;
_B2.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0xF7E779 ; _B0.enabled = true ; _B1.enabled = true ; _B2.enabled = false ; _B3.enabled = true ; } ) ;
_B3.addEventListener ( MouseEvent.CLICK , function ():void { _COLOR = 0x80A2F0 ; _B0.enabled = true ; _B1.enabled = true ; _B2.enabled = true ; _B3.enabled = false ; } ) ;
stage.addEventListener ( MouseEvent.MOUSE_DOWN , function ():void { _CLICK = true ; } ) ;
stage.addEventListener ( MouseEvent.MOUSE_UP , function ():void { _CLICK = false; } ) ;
prepareAsset(0);
}
public function RUN ( e:Event ):void {
_CANVAS.lock() ;
////////////////////////////////////
for ( var I:int = 200 ; I < 250 ; ++ I ) {
if ( Math.random() < .1 ) {
// TODO Lの生成
// _CANVAS.setPixel ( I , 60 , 0xF7E779 ) ;
}
}
////////////////////////////////////
if ( _CLICK && 60 < mouseY && 60 < _BY ) {
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 ;
////////////////////////////////////
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 ) ;
if ( C == 0 ) {
continue ;
}
// TODO C == 1 右向き
if ( C == 1 ) {
{//落下
T = _CANVAS.getPixel ( X , Y + 1 ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
}
// TODO C == 2 左向き
if ( C == 2 ) {
{//落下
T = _CANVAS.getPixel ( X , Y + 1 ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
}
if ( C == 0xF7E779 ) {
var T:uint ;
var TX:int ;
{//落下
T = _CANVAS.getPixel ( X , Y + 1 ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
//水より砂の方が重い,適当な確率で場所の置換を許す。
if ( T == 0x80A2F0 && Math.random() < .5 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
{//左右移動
TX = X + Math.floor ( Math.random() * 7 ) - 3 ;
T = _CANVAS.getPixel ( TX , Y ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}
//水より砂の方が重い,適当な確率で場所の置換を許す。
if ( T == 0x80A2F0 && Math.random() < .8 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}
}
}
if ( C == 0x80A2F0 ) {
{//落下
T = _CANVAS.getPixel ( X , Y + 1 ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( X , Y + 1 , C ) ;
continue ;
}
}
{//左右移動
TX = X + Math.floor ( Math.random() * 7 ) - 3 ;
T = _CANVAS.getPixel ( TX , Y ) ;
if ( T == 0 ) {
_CANVAS.setPixel ( X , Y , T ) ;
_CANVAS.setPixel ( TX , Y , C ) ;
continue ;
}
}
}
}
}
_CANVAS.unlock() ;
}
}
}
import flash.display.BitmapData;
class LemmingSprite {
public var x:Number;//x
public var y:Number;//y
public var imAr:Array;//frames array
public var imArL:uint;//number of frames
public var bmd:BitmapData;//bitmapData reference
public var spd:uint;//frequence in ms of animation
public var ti:uint;//inner time
public var im:uint;//current frame counter
public var imW:uint;//width for subdivision
public var imH:uint;//height for subdivision
}