Snowflake
hexCell and Math - http://www.redblobgames.com/grids/hexagons/
/**
* Copyright greentec ( http://wonderfl.net/user/greentec )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vjf5
*/
package {
import com.bit101.components.Label;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.display.BlendMode;
import flash.utils.ByteArray;
public class FlashTest extends Sprite {
public var _bitmapData:BitmapData;
public var _bitmap:Bitmap;
public var neighbors:Array = [];
public var mapSize:int = 29;
public var map:Array = [];
public var cloneMap:Array;
public var edgeLength:int = 5;
public var edgeW:int;
public var edgeH:int;
public var _width:int = 465;
public var _height:int = 465;
public var oneCell:Sprite;
public var gen:int = 0;
public var _label:Label;
public function FlashTest() {
// write as3 code here..
stage.scaleMode = "noScale";
_bitmapData = new BitmapData(_width, _height, false, 0x0);
_bitmap = new Bitmap(_bitmapData);
addChild(_bitmap);
oneCell = new Sprite();
var _G:Graphics = oneCell.graphics;
var i:int;
var angle:Number;
_G.clear();
_G.beginFill(0xddddff);
_G.moveTo(edgeLength, 0);
for (i = 1; i <= 6; i += 1)
{
angle = 2 * Math.PI / 6 * i;
_G.lineTo(edgeLength * Math.cos(angle), edgeLength * Math.sin(angle));
}
_G.endFill();
for (i = 0; i < 6; i += 1)
{
neighbors[i] = [];
}
neighbors[0] = [+1, -1, 0];
neighbors[1] = [+1, 0, -1];
neighbors[2] = [0, +1, -1];
neighbors[3] = [-1, +1, 0];
neighbors[4] = [-1, 0, +1];
neighbors[5] = [0, -1, +1];
edgeW = edgeLength * 3 / 2;
edgeH = edgeLength * Math.sqrt(3) / 2;
initMap();
map[0][0][0] = true;
_label = new Label(this, 10, 10, "gen : " + String(gen));
addEventListener(Event.ENTER_FRAME, onLoop);
}
public function onLoop(e:Event):void
{
if(gen<mapSize)
{
gen += 1;
checkNeighbor();
//drawMap();
_label.text = "gen : " + String(gen);
}
else if(gen == mapSize)
{
removeEventListener(Event.ENTER_FRAME, onLoop);
//_bitmap.rotationX += 0.5;
}
}
public function checkNeighbor():void
{
var i:int, j:int, k:int;
var m:int;
var tx:int, ty:int, tz:int;
var neighborNum:int;
cloneMap = clone(map);
for (i = -1 * mapSize; i < mapSize + 1; i += 1)
{
for (j = -1 * mapSize; j < mapSize + 1; j += 1)
{
for (k = -1 * mapSize; k < mapSize + 1; k += 1)
{
if (i + j + k == 0)
{
neighborNum = 0;
for (m = 0; m < 6; m += 1)
{
tx = neighbors[m][0] + i;
ty = neighbors[m][1] + j;
tz = neighbors[m][2] + k;
if (tx <= mapSize && tx >= -mapSize &&
ty <= mapSize && ty >= -mapSize &&
tz <= mapSize && tz >= -mapSize)
{
if (map[tx][ty][tz] == true)
{
neighborNum += 1;
}
}
}
if (neighborNum == 1)
{
cloneMap[i][j][k] = true;
_bitmapData.draw(oneCell, new Matrix(1, 0, 0, 1, _width / 2 + i * edgeW, _height / 2 + (0 - j + k) * edgeH));
}
}
}
}
}
map = clone(cloneMap);
}
public function initMap():void
{
var i:int, j:int, k:int;
for (i = -1 * mapSize; i < mapSize + 1; i += 1)
{
map[i] = [];
for (j = -1 * mapSize; j < mapSize + 1; j += 1)
{
map[i][j] = [];
for (k = -1 * mapSize; k < mapSize + 1; k += 1)
{
map[i][j].push(false);
//if (i + j + k == 0)
//{
//map[i][j][k] = false;
//}
}
}
}
}
public function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
}
}