魚群
LIVE CODING 2010/07/30 17:13 - 21:50
魚をごちゃごちゃさせる
クリックまたはキーを押すと散る
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9fSR
*/
// forked from shohei909's はじけ飛ぶ何か
//LIVE CODING 2010/07/30 17:13 - 21:50
//魚をごちゃごちゃさせる
//クリックまたはキーを押すと散る
package {
import flash.geom.ColorTransform;
import flash.events.*;
import flash.display.Sprite;
import com.bit101.components.FPSMeter;
public class FlashTest extends Sprite {
public var map:CrowdMap = new CrowdMap(465,465);
public var frontSea:SeaMap = new SeaMap(465,465,new ColorTransform(0.5,2,3,1));
public function FlashTest() {
// write as3 code here..
addChild(map);
addChild(frontSea);
//frontSea.startScroll(1);
new FPSMeter(this);
onDown();
map.create(200, 220, 20, 10, 5);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
}
public function onDown(e:Event = null):void{
map.del()==false
}
}
}
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
class CrowdMap extends Bitmap{
public var color:uint = 0xFF000000;
private var cores:Vector.<Object> = new Vector.<Object>();
private var points:Vector.<Object> = new Vector.<Object>();
private static const FISH_PASS1:Array = [[ [1,1.5,0x000000,1.0,0x668899,1.0],[0,10,0],[10,0,1],[19,8,0],[26,0,0],[25,20,0],[19,14,0],[10,20,1] ],[[1,1.5,0x000000,1.0,0xFFFFFF,1.0],[7,0,1],[11,7,1],[7,14,1],[3,7,1]],[[1,0,0x000000,1.0,0x000000,1.0],[7,3,1],[9,7,1],[7,11,1],[4,7,1]]];
private static const FISH_PASS2:Array = [[ [1,1.5,0x000000,1.0,0x668899,1.0],[0,10,0],[10,0,1],[19,8,0],[23,0,0],[24,20,0],[19,14,0],[10,20,1] ],[[1,1.5,0x000000,1.0,0xFFFFFF,1.0],[7,0,1],[11,7,1],[7,14,1],[3,7,1]],[[1,0,0x000000,1.0,0x000000,1.0],[7,3,1],[9,7,1],[7,11,1],[4,7,1]]];
private var fishMap:Vector.<BitmapData> = new Vector.<BitmapData>;
private var transMap:BitmapData;
private var rate:Number = 60;
private var filter:BlurFilter = new BlurFilter(5,5,1);
private var aliveRect:Rectangle;
public function CrowdMap(w:int,h:int){
aliveRect = new Rectangle(-100,-100,w+200,h+200);
transMap = new BitmapData(w,h,true,0x22005533);
fishMap[0] = VectorImageUtil.bitmapData(FISH_PASS1);
fishMap[1] = VectorImageUtil.bitmapData(FISH_PASS2);
super(new BitmapData(w,h,true));
this.addEventListener( "enterFrame", onFrame );
}
public function create(x:int,y:int,n:int,num:int,w:int=10):void{
for(var i:int=0;i<n;i++){
var core:Object = { x:x+i*w,y:y };
cores.push( core );
for(var j:int=0;j<num;j++){
var r:Number = Math.random()*200+100;
var deg:Number = Math.PI*( 0.3-Math.random()*0.6 );
var ct:ColorTransform = new ColorTransform( 1+Math.random(),1+Math.random(),1+Math.random(),1 );
points.push( {
x: core.x+r*Math.cos(deg),
y: core.y+r*Math.sin(deg),
dx: 0,
dy: 0,
targetX: core.x + (50-Math.random()*100),
targetY: core.y + (20-Math.random()*40),
target: core,
chase: true,
life: 40,
color: ct,
count:20*Math.random()
} );
}
}
}
private function onFrame(e:Event):void{
move();
drow();
}
private function drow():void{
var b:BitmapData = bitmapData;
b.lock();
b.merge(transMap,b.rect,new Point(),100,100,100,256);
var l:int = points.length;
for(var i:int=l-1;i>-1;i--){
var p:Object = points[i];
var dir:int = 1;
if(p.x<p.targetX){dir=-1}
p.count++;
b.draw(fishMap[int(p.count/10)%2],new Matrix(dir,0,0,1,p.x-12*dir,p.y), p.color);
}
b.unlock();
}
private function move():void{
var l:int = points.length;
for(var i:int=0;i<l;i++){
var p:Object = points[i];
p.x = (p.x*15+p.targetX)/16;
p.y = (p.y*15+p.targetY)/16;
if(p.chase){
if(Math.random()<0.01){
var core:Object = p.target;
p.targetX = core.x + (120-Math.random()*240);
p.targetY = core.y + (50-Math.random()*100);
}
}else{
p.life--;
p.color.alphaMultiplier = p.life/14;
if(p.life<0 || bitmapData.rect.contains(p.x,p.y)==false){
points.splice(i,1);
l--;i--;
}
}
}
}
public function del(n:int = 0):Boolean{
var l:int = points.length;
if(n < cores.length){
var c:Object = cores[n];
cores.splice(n,1);
for(var i:int=0;i<l;i++){
var p:Object = points[i];
if(p.target == c){
p.chase = false;
var speed:Number = Math.random()*900+200;
var deg:Number = Math.PI*(-Math.random()*0.6+1.3);
p.targetX += speed*Math.cos(deg);
p.targetY += speed*Math.sin(deg);
}
}
return true;
}
return false;
}
}
class SeaMap extends Bitmap{
public var offsets:Array = [new Point,new Point],w:int,h:int,scroll:Number,colorTrans:ColorTransform,randomSeed:int;
public function SeaMap(w:int,h:int,colorTrans:ColorTransform=null,seed:int=-1){
if(colorTrans==null){colorTrans=new ColorTransform()}
if(seed==-1){randomSeed=Math.random()*1000;}
else{randomSeed=seed;}
this.w=w;this.h=h;
this.colorTrans=colorTrans;
super(new BitmapData(w,h,true));
drow();
}
public function startScroll(scroll:Number=2):void{
this.scroll=scroll;
addEventListener("enterFrame",drow);
}
public function drow(e:Event=null):void{
bitmapData.perlinNoise( 120,120,2,randomSeed,false,false,15,false, offsets );
bitmapData.colorTransform(bitmapData.rect,colorTrans);
offsets[0].x += scroll;
offsets[1].x -= scroll/2;
}
}
import flash.display.Shape;
//ベクタ画像用クラス=================================================================================================================================================================
class VectorImage extends Shape{
public var data:Array;
public var styles:Vector.<Array>; //styles[][],,,style:[open/close,lineWeight,lineColor,lineAlpha,fillColor,fillAlpha]
public var dots:Vector.<Array>; //dots[][][],,,dot:[x,y,curve]
//コンストラクタ
public function VectorImage(pass:Array){ read(pass); }
//データの読み込み
public function read(pass:Array):void{
data=pass;
styles = new Vector.<Array>();
dots = new Vector.<Array>();
var size:int = pass.length
for(var i:int=0;i<size;i++){
styles[i] = [];dots[i] = [];
for(var j:int=0;j<6;j++){ styles[i][j]=pass[i][0][j]; }
var l:int =pass[i].length-1;
for(var k:int=0;k<l;k++){
dots[i][k] = [];
for(var n:int=0;n<3;n++){ dots[i][k][n]=pass[i][k+1][n]; }
}
}
drow();
}
//データの描画
public function drow():void{
var size:int = styles.length;
graphics.clear();
for(var i:int=0;i<size;i++){
var l:int=dots[i].length;
graphics.lineStyle(styles[i][1],styles[i][2],styles[i][3]);
if(styles[i][0]==1){ graphics.beginFill(styles[i][4],styles[i][5]); var bf:Array=dots[i][l-1];}
else{ bf=dots[i][0];}
graphics.moveTo( center(bf,dots[i][0],0),center(bf,dots[i][0],1) );
for(var j:int=0;j<l-1;j++){ line(bf,dots[i][j],dots[i][j+1]);bf=dots[i][j]; }
if(styles[i][0]==1){ line( bf, dots[i][j], dots[i][0]); }
else{ line(bf, dots[i][j], dots[i][j] ); }
}
}
private function line(dot1:Array,dot2:Array,dot3:Array):void{
graphics.lineTo( center(dot2,dot1,0),center(dot2,dot1,1) );
if(dot2[2]>0){graphics.curveTo( dot2[0],dot2[1],center(dot2,dot3,0),center(dot2,dot3,1) );}
}
private function center(dot1:Array,dot2:Array,xy:int):Number{
if( dot1[2] == 0){return dot1[xy];}
return (dot1[xy]*dot2[2]+dot2[xy]*dot1[2]*dot1[2])/(dot1[2]*dot1[2]+dot2[2]);
}
}
//=========================================================================================================================================================================================
//ベクタ画像用のユーティリティ==========================================================================================
class VectorImageUtil{
//ベクタ画像用のデータをBitmapDataに変換します。
public static function bitmapData(pass:Array):BitmapData{
var img:VectorImage = new VectorImage(pass);
var rect:Rectangle = img.getRect(img);
var mapData:BitmapData = new BitmapData(rect.width,rect.height,true,0);
mapData.draw(img);
return mapData;
}
}
//=====================================================================================================