In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

LifeGame

ライフゲームっぽいもの
マウスクリックで周辺にワクチンを投入
Get Adobe Flash player
by okoi 31 Mar 2010
    Embed
/**
 * Copyright okoi ( http://wonderfl.net/user/okoi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vgio
 */

//
//	ライフゲームっぽいもの
//	マウスクリックで周辺にワクチンを投入
//
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	[SWF(width = "465", height = "465", backgroundColor = "0x000000", frameRate = "60")]

	/**
	 * @author okoi
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			graphics.beginFill(0x005500);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			
			
			addChild( new Canvas() );
		}
	}
	
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;

const INIT_VIRUS_NUM:int = 500;
const GENERATE_CIRCLE:int = 20 * 20;
const NEW_GENERATE_NUM:int = 200;		//	1フレ中何箇所新規の微生物を判定するか
const DRUG_CIRCLE:int = 40 * 40;		//	ワクチン有効範囲

/**
 * キャンバスクラス
 */
class Canvas extends Sprite {
	
	private var active:Array = new Array();		//	現役の微生物
	private var next:Array = new Array();		//	次世代の微生物
	private var drug:Boolean = false;
		
	public	function Canvas() 
	{
		super();
		if (stage) Init();
		else addEventListener(Event.ADDED_TO_STAGE, Init);
	}
	
	public	function Init(e:Event = null):void
	{
		removeEventListener(Event.ADDED_TO_STAGE, Init);
		
		this.filters = [new GlowFilter(0xFFFFFF, 0.5, 7, 7, 20, 4, true )];
		
		
		InitVirus();
		
		
		stage.addEventListener(MouseEvent.MOUSE_DOWN, InjectDrug );
		stage.addEventListener(MouseEvent.MOUSE_UP, StopDrug );
		
		addEventListener(Event.ENTER_FRAME, Update );
	}
	
	
	private function Update(e:Event):void 
	{
		var i:int;
		var j:int;
		var ct:int;
		var len:Number;
		
		//	ワクチン処理
		if ( drug )
		{
			var dx:Number = stage.mouseX;
			var dy:Number = stage.mouseY;
			for ( i = active.length - 1; i >= 0; i-- )
			{
				len = (dx - active[i].posX) * (dx - active[i].posX) + (dy - active[i].posY) * (dy - active[i].posY);
				if ( len <= DRUG_CIRCLE )
				{
					active.splice( i, 1 );
				}
			}
		}
		
		//	次世代計算
		next = new Array();
		for ( i = 0; i < active.length; i++ )
		{
			ct = 0;
			var iV:Virus = active[i];
			
			if ( iV.life <= 0 )
			{		
				for ( j = 0; j < active.length; j++ )
				{
					if ( i == j ) continue;
					var jV:Virus = active[j];
					len = (iV.posX - jV.posX) * (iV.posX - jV.posX) + (iV.posY - jV.posY) * (iV.posY - jV.posY);
					if ( len <= GENERATE_CIRCLE )	ct++;
				}
				if ( ct == 2 || ct == 3 )	next.push( active[i] );
			}else
			{
				iV.Grow();
				next.push( active[i] );
			}
		}
		for ( i = 0; i < NEW_GENERATE_NUM; i++ )
		{
			ct = 0;
			var cx:Number = Math.random() * stage.stageWidth;
			var cy:Number = Math.random() * stage.stageHeight;
			for ( j = 0; j < active.length; j++ )
			{
				var v:Virus = active[j];
				len = (v.posX - cx) * (v.posX - cx) + (v.posY - cy) * (v.posY - cy);
				if ( len <= GENERATE_CIRCLE )	ct++;
			}
			if ( ct == 3 )	next.push( new Virus( cx, cy ) );
		}
		
		//	微生物描画
		graphics.clear();
		for ( i = 0; i < active.length; i++ )
		{
			active[i].Draw(graphics);
		}
		if ( drug )
		{
			graphics.beginFill(0xAAAAFF, 0.3);
			graphics.lineStyle( 2, 0xAAAAFF, 0.5 );
			graphics.drawCircle( stage.mouseX, stage.mouseY, Math.sqrt( DRUG_CIRCLE ) );
			graphics.endFill();
		}
		
		//	次世代を反映
		active = next;
		
	}
	
	private function InitVirus():void
	{
		var i:int = 0;
		for ( i = 0; i < INIT_VIRUS_NUM; i++ )
		{
			var x:Number = Math.random() * stage.stageWidth;
			var y:Number = Math.random() * stage.stageHeight;
			active.push( new Virus(x, y, int(Math.random() * 5) + 5 ) );
		}
	}
	/**
	 * ワクチン投入
	 * @param	e
	 */
	private function InjectDrug(e:MouseEvent):void
	{
		drug = true;
	}
	/**
	 * ワクチン投入終了
	 * @param	e
	 */
	private function StopDrug(e:MouseEvent):void
	{
		drug = false;
	}
	
}
/**
 * 微生物クラス
 */
class Virus {
	public var posX:Number;
	public var posY:Number;
	public var life:int;
	public var size:int;
	
	public	function Virus(_x:Number, _y:Number, _life:int = 10)
	{
		posX = _x;
		posY = _y;
		life = _life;
	}
	
	public	function Grow():void
	{
		if ( life <= 0 ) return;
		life--;
		size++;
	}
	
	public	function Draw(g:Graphics):void 
	{
		g.beginFill(0x00AA00);
		g.drawCircle(posX, posY, 1+size);
		g.endFill();
	}
	
}