forked from: 【AS100本ノック】10回目:爆破

by mex_ichikawa forked from 【AS100本ノック】10回目:爆破 (diff: 234)
AS100本ノック
* 10回目のお題は「爆破」
* あなたなりの「爆破」を表現してください。
* --
* マウスで一筆書きしてね
* ばらばらヒュー
♥2 | Line 213 | Modified 2010-02-01 15:09:24 | MIT License
play

ActionScript3 source code

/**
 * Copyright mex_ichikawa ( http://wonderfl.net/user/mex_ichikawa )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jUnp
 */

// forked from mex's 【AS100本ノック】10回目:爆破
/* 
 * AS100本ノック
 * 10回目のお題は「爆破」
 * あなたなりの「爆破」を表現してください。
 * --
 * マウスで一筆書きしてね
 * ばらばらヒュー
 */
package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.libspark.betweenas3.BetweenAS3;
	import org.libspark.betweenas3.core.easing.IEasing;
	import org.libspark.betweenas3.easing.*;
	import org.libspark.betweenas3.events.*;
	
	import org.libspark.betweenas3.tweens.IObjectTween;
	import org.libspark.betweenas3.tweens.ITween;
	
	[SWF(width=465, height=465, backgroundColor=0xFFFFFF, frameRate=60)]
	public class Explosion extends Sprite
	{
		public static const STAGE_WIDTH:Number = 465;
		public static const STAGE_HEIGHT:Number = 465;
		private static const WIDTH_SIZE:Number = 50;
		private var _drawItem:ExplosionItem;
		private var _divisionItem:ExplosionDivision;
		private var _tween:ITween;
		public function Explosion()
		{
			init();
		}
		private function init():void
		{
			_drawItem = new ExplosionItem(stage);
			_drawItem.event.addEventListener(Event.COMPLETE, drawCompleteEvent);
			addChild(_drawItem);
		}
		
		private function drawCompleteEvent(event:Event):void
		{
			if(_tween) _tween.stop();
			var item:Bitmap;
			
			if(_divisionItem)
			{
				for each(item in _divisionItem.getParts())
				{
					removeChild(item);
				}
			}
			
			_divisionItem = new ExplosionDivision(_drawItem);
			var tweens:Array = [];
			for each(item in _divisionItem.getParts())
			{
				addChild(item);
				tweens.push(
					BetweenAS3.serial(
						BetweenAS3.tween(
							item,
							makeStatus(item),
							null,
							Math.random() + 2,
							Quart.easeOut
						),
						BetweenAS3.tween(
							item,
							{y: STAGE_HEIGHT-10},
							null,
							1,
							Quart.easeIn
						)
					)
				);
			}
			_tween = BetweenAS3.delay(BetweenAS3.parallelTweens(tweens), 1);
			_tween.addEventListener(TweenEvent.COMPLETE, tweenCompleteHandler);
			_tween.play();
			_drawItem.graphics.clear();
		}
		
		private function makeStatus(item:Bitmap):Object
		{
			var obj:Object = {};
			obj.x = ((item.x - STAGE_WIDTH/2) * (10*Math.random())) + STAGE_WIDTH/2;
			obj.y = ((item.y - STAGE_HEIGHT/2) * (10*Math.random())) + STAGE_HEIGHT/2;
			obj.scaleX = item.x / (STAGE_WIDTH/2) * 2;
			obj.scaleY = item.y / (STAGE_HEIGHT/2) * 2;
			obj.alpha = 0.5;
			obj.rotationZ = (item.y - STAGE_HEIGHT/2 < 0 ? 360 : -360) * Math.random();
			
			return obj;
		}
		
		private function tweenCompleteHandler(event:TweenEvent):void
		{
			_drawItem.isActive = true;
		}
	}
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;

import frocessing.color.ColorHSV;

class ExplosionItem extends Sprite
{
	private var _event:EventDispatcher;
	private var _g:Graphics;
	private var _type:Vector.<int>;
	private var _pos2D:Vector.<Number>;
	private var _isDown:Boolean;
	private var _stage:Stage;
	private var _hsv:ColorHSV;
	public var isActive:Boolean;
	public function ExplosionItem(s:Stage)
	{
		_stage = s;
		isActive = true;
		init();	
	}
	private function init():void
	{
		_hsv = new ColorHSV(0, 1, 1, 1);
		_event = new EventDispatcher();
		_g = graphics;
		_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		_stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
	}
	public function get event():EventDispatcher
	{
		return _event;
	}
	private function mouseDownHandler(event:MouseEvent):void
	{
		if(isActive)
		{
			_hsv.h = (new Date()).time % 360;
			_g.clear();
			_type = new Vector.<int>;
			_pos2D = new Vector.<Number>;
			_type.push(1);
			_pos2D.push(event.stageX);
			_pos2D.push(event.stageY);
			_isDown = true;
		}
	}
	private function mouseUpHandler(event:MouseEvent):void
	{
		if(isActive)
		{
			_g.clear();
			_type.push(2);
			_pos2D.push(_pos2D[0]);
			_pos2D.push(_pos2D[1]);
			_g.lineStyle(3, _hsv.value);
			_g.beginFill(_hsv.value, 0.5);
			_g.drawPath(_type, _pos2D);
			_g.endFill();
			_isDown = false;
			isActive = false;
			_event.dispatchEvent(new Event(Event.COMPLETE));
		}
	}
	private function mouseMoveHandler(event:MouseEvent):void
	{
		if(_isDown && isActive)
		{
			_g.clear();
			_type.push(2);
			_pos2D.push(event.stageX);
			_pos2D.push(event.stageY);
			_g.lineStyle(3, _hsv.value);
			_g.drawPath(_type, _pos2D);
			
		}
	}
}

class ExplosionDivision
{
	private static const BLOCK_WIDTH:Number = 10;
	private static const BLOCK_HEIGHT:Number = 10;
	
	private var _data:BitmapData;
	private var _items:Vector.<Bitmap>;
	public function ExplosionDivision(item:ExplosionItem)
	{
		_data = new BitmapData(Explosion.STAGE_WIDTH, Explosion.STAGE_HEIGHT, true);
		_data.draw(item);
		init();
	}
	private function init():void
	{
		var bm:Bitmap,bmd:BitmapData, vec:Vector.<uint>;
		_items = new Vector.<Bitmap>;
		for(var i:Number=0,imax:Number=Explosion.STAGE_HEIGHT; i<imax; i+=BLOCK_HEIGHT)
		{
			for(var j:Number=0,jmax:Number=Explosion.STAGE_WIDTH; j<jmax; j+=BLOCK_WIDTH)
			{
				bmd = new BitmapData(BLOCK_WIDTH, BLOCK_HEIGHT, true);
				bmd.copyPixels(_data, new Rectangle(j, i, BLOCK_WIDTH, BLOCK_HEIGHT), new Point(0,0));
				bm = new Bitmap(bmd);
				vec = bmd.getVector(new Rectangle(0,0,BLOCK_WIDTH,BLOCK_HEIGHT));
				if(!vec.every(everyCallBack))
				{
					bm.x = j;
					bm.y = i;
					_items.push(bm);
				}
			}
		}
	}
	
	private function everyCallBack(item:uint, index:int, vector:Vector.<uint>):Boolean
	{
		return item == 0xFFFFFFFF;
	}
	
	public function getParts():Vector.<Bitmap>
	{
		return _items;
	}
	
}