途中)ライフゲーム:二次セルオートマトン
forked from ライフゲーム:一次セルオートマトン (diff: 28)
ActionScript3 source code
/**
* Copyright yasnis ( http://wonderfl.net/user/yasnis )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ueiH
*/
// forked from yasnis's ライフゲーム:一次セルオートマトン
package {
import flash.utils.IDataInput;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
public class FlashTest extends Sprite {
private var scale:int = 3;
private var x_max:int = 500;
private var y_max:int = 500;
private var bmpd_a:BitmapData = new BitmapData(x_max, y_max, false, 0xffffffff);
private var bmpd_b:BitmapData = new BitmapData(x_max, y_max, false, 0xffffffff);
private var currentBitmap:BitmapData;
private var bmp:Bitmap = new Bitmap(bmpd_a);
private var interval:int = 20;
private var timer:Timer = new Timer(interval, 0);
public function FlashTest() {
// write as3 code here..
//return; //レンダリングさせない
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resetLayout);
addChild(bmp);
resetLayout(null);
timer.start();
//timer.addEventListener(TimerEvent.TIMER, updateTimer);
//addEventListener(Event.ENTER_FRAME, enterframeHandler);
}
private function updateTimer(e:TimerEvent):void {
calcLife();
updateBitmap();
}
private function enterframeHandler(e:Event):void {
calcLife();
updateBitmap();
}
private function resetLayout(e:Event):void {
x_max = stage.stageWidth/scale;
y_max = stage.stageHeight/scale;
bmpd_a = new BitmapData(x_max, y_max, false, 0xffffffff);
bmpd_b = new BitmapData(x_max, y_max, false, 0xffffffff);
bmp.bitmapData = bmpd_a;
bmp.x = bmp.y = 0;
bmp.width = stage.stageWidth;
bmp.height = stage.stageHeight;
initialize();
}
private function initialize():void {
bmpd_a.lock();
bmpd_a.fillRect(bmpd_a.rect,0xffffffff);
var w:int = bmpd_a.width;
var h:int = bmpd_a.height;
for (var i:int = 0; i < h; i++) {
for (var j:int = 0; j < w; j++) {
bmpd_a.setPixel(j,i,Math.random()<.5?0x000000:0xffffff);
}
}
bmpd_a.unlock();
}
private function calcLife():void {
/*
currentnode = new Vector.<int>(x_max);
var l:int = oldnode.length;
var life:int = 0;
for (var i:int = 0; i < l; i++) {
life = 0;
if (i == 0) {
life += 4 * oldnode[l - 1];
life += 1 * oldnode[i + 1];
}else if (i == l - 1) {
life += 4 * oldnode[i - 1];
life += 1 * oldnode[0];
}else {
life += 4 * oldnode[i-1];
life += 1 * oldnode[i + 1];
}
life += 2 * oldnode[i];
currentnode[i] = (life == 7 || life == 4 || life == 1)?1:0;
}
oldnode = currentnode;
*/
}
private function updateBitmap():void {
/*
if (currentstep == y_max) {
currentstep = y_max-1;
bmpd.scroll(0, -1);
}
bmpd.lock();
var l:int = oldnode.length;
for (var i:int = 0; i < l; i++) {
bmpd.setPixel32(i, currentstep, oldnode[i]?0xff000000:0xffffffff);
}
bmpd.unlock(new Rectangle(0, currentstep, x_max, 1));
currentstep++;
*/
}
}
}
