Section 1
//GameOfLife (GameOfLife)
package {
import flash.events.*;
import flash.display.*;
import flash.geom.*;
public class GameOfLife extends Sprite {
private const LIVECELL:uint = 0xFF0000;
private const EDIT_REMOVE:String = "remove";
private const EDIT_ADD:String = "add";
private const DEADCELL:uint = 0;
private var _pause:Boolean;
private var _overlay:Sprite;
private var _editMode:String;
private var _bitmap:Bitmap;
private var _mouseDown:Boolean;
private var _data:BitmapData;
private var _lastMouseLoc:Point;
public function GameOfLife(){
stage.showDefaultContextMenu = false;
reset();
}
private function onMouseMove(_arg1:MouseEvent):void{
var _local2:int;
var _local3:int;
_local2 = Math.floor(_arg1.localX);
_local3 = Math.floor(_arg1.localY);
if (_mouseDown){
if ((((_local2 == _lastMouseLoc.x)) && ((_local3 == _lastMouseLoc.y)))){
return;
};
if (_editMode == EDIT_ADD){
_data.setPixel(_local2, _local3, LIVECELL);
} else {
_data.setPixel(_local2, _local3, DEADCELL);
};
_lastMouseLoc = new Point(_local2, _local3);
};
}
private function initGame():void{
_data = new BitmapData(40, 30, false, DEADCELL);
_mouseDown = false;
_pause = false;
}
private function initOverlay():void{
_bitmap = new Bitmap(_data);
_overlay = new Sprite();
_overlay.addChild(_bitmap);
this.addChild(_overlay);
_overlay.width = 448;
_overlay.height = 336;
_overlay.x = 0;
_overlay.y = 0;
_lastMouseLoc = new Point(-1, -1);
this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
_overlay.addEventListener(MouseEvent.CLICK, this.onClick);
_overlay.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
_overlay.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
_overlay.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
}
private function onMouseUp(_arg1:MouseEvent):void{
_mouseDown = false;
}
private function reset():void{
initGame();
initOverlay();
generateData();
}
private function clearData():void{
var _local1:int;
var _local2:int;
_local1 = 0;
while (_local1 < _data.width) {
_local2 = 0;
while (_local2 < _data.height) {
_data.setPixel(_local1, _local2, DEADCELL);
_local2++;
};
_local1++;
};
_pause = true;
}
private function onEnterFrame(_arg1:Event):void{
var _local2:int;
var _local3:int;
var _local4:Array;
var _local5:Array;
var _local6:BitmapData;
var _local7:int;
var _local8:int;
var _local9:int;
var _local10:int;
var _local11:Point;
var _local12:uint;
if (((_mouseDown) || (_pause))){
return;
};
_local2 = (_data.width - 1);
_local3 = (_data.height - 1);
_local4 = [0, 0, _local2, 1, _local2, 1, _local2, 1];
_local5 = [_local3, 1, 0, 0, _local3, 1, 1, _local3];
_local7 = 0;
_local6 = new BitmapData(_data.width, _data.height, _data.transparent, DEADCELL);
_local8 = 0;
while (_local8 < _local6.width) {
_local9 = 0;
while (_local9 < _local6.height) {
_local7 = 0;
_local10 = 0;
while (_local10 < _local4.length) {
_local11 = new Point((_local8 + _local4[_local10]), (_local9 + _local5[_local10]));
_local12 = _data.getPixel((_local11.x % _local6.width), (_local11.y % _local6.height));
if (_local12 == LIVECELL){
_local7++;
};
_local10++;
};
if (_data.getPixel(_local8, _local9) == LIVECELL){
if ((((_local7 == 2)) || ((_local7 == 3)))){
_local6.setPixel(_local8, _local9, LIVECELL);
} else {
_local6.setPixel(_local8, _local9, DEADCELL);
};
} else {
if (_local7 == 3){
_local6.setPixel(_local8, _local9, LIVECELL);
} else {
_local6.setPixel(_local8, _local9, DEADCELL);
};
};
_local9++;
};
_local8++;
};
_bitmap.bitmapData = _local6;
_data = _local6;
}
private function onKeyDown(_arg1:KeyboardEvent):void{
switch (String.fromCharCode(_arg1.charCode).charAt(0)){
case "s":
_pause = false;
_mouseDown = false;
onEnterFrame(null);
_pause = true;
break;
case " ":
_pause = !(_pause);
break;
case "r":
generateData();
break;
case "c":
clearData();
break;
case "1":
loadPattern("glider");
break;
case "2":
loadPattern("tumbler");
break;
case "3":
loadPattern("spaceship");
break;
case "4":
loadPattern("10cell");
break;
case "5":
loadPattern("oscillator");
break;
};
}
private function onMouseDown(_arg1:MouseEvent):void{
var _local2:int;
var _local3:int;
_local2 = Math.floor(_arg1.localX);
_local3 = Math.floor(_arg1.localY);
if (_data.getPixel(_local2, _local3) == LIVECELL){
_editMode = EDIT_REMOVE;
} else {
_editMode = EDIT_ADD;
};
_mouseDown = true;
}
private function loadPattern(_arg1:String):void{
var _local2:Array;
var _local3:Array;
var _local4:int;
switch (_arg1){
case "glider":
_local2 = [19, 20, 20, 21, 21];
_local3 = [16, 14, 16, 15, 16];
break;
case "tumbler":
_local2 = [17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 19, 21, 21, 21, 21, 21, 22, 22, 22, 23, 23, 23];
_local3 = [16, 17, 18, 13, 14, 18, 13, 14, 15, 16, 17, 13, 14, 15, 16, 17, 13, 14, 18, 16, 17, 18];
break;
case "spaceship":
_local2 = [18, 18, 19, 20, 21, 21, 22, 22, 22];
_local3 = [14, 16, 13, 13, 13, 16, 13, 14, 15];
break;
case "10cell":
_local2 = [15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
_local3 = [15, 15, 15, 15, 15, 15, 15, 15, 15, 15];
break;
case "oscillator":
_local2 = [10, 10, 11, 11, 21, 21, 21, 21, 23, 23, 24, 24, 24, 25, 25, 25, 30, 30, 31, 31];
_local3 = [15, 16, 15, 16, 12, 13, 17, 18, 13, 17, 14, 15, 16, 14, 15, 16, 15, 16, 15, 16];
break;
default:
return;
};
clearData();
_local4 = 0;
while (_local4 < _local2.length) {
_data.setPixel(_local2[_local4], _local3[_local4], LIVECELL);
_local4++;
};
}
private function generateData():void{
var _local1:int;
var _local2:int;
_local1 = 0;
while (_local1 < _data.width) {
_local2 = 0;
while (_local2 < _data.height) {
if (Math.random() > 0.5){
_data.setPixel(_local1, _local2, LIVECELL);
};
_local2++;
};
_local1++;
};
}
private function onClick(_arg1:MouseEvent):void{
if (_data.getPixel(_arg1.localX, _arg1.localY) == LIVECELL){
_data.setPixel(_arg1.localX, _arg1.localY, DEADCELL);
} else {
_data.setPixel(_arg1.localX, _arg1.localY, LIVECELL);
};
}
}
}//package