forked from: forked from: forked from: うお
// forked from hacker_g2scu8pr's forked from: forked from: うお
// forked from RoundRoom's forked from: うお
// forked from RoundRoom's うお
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.events.Event;
import flash.utils.*;
[SWF(backgroundColor="0")]
public class NoiseField extends Sprite {
//
private var arrow_array:Array = [];
private var field_array:Array = [];
private var w:int = 31;
private var h:int = 31;
private var rectX:int;
private var rectY:int;
//
private var bm:Bitmap;
private var perlin_bmd:BitmapData;
private var seed:int = Math.round(Math.random() * 100);
private var speed:int = 1;
private var offsetPoint:Point = new Point(0, 0);
private var damping:Number = .000003;
public function NoiseField() {
rectX = stage.stageWidth / w;
rectY = stage.stageWidth / h;
perlin_bmd = new BitmapData(w, h);
perlin_bmd.perlinNoise(w / 3, h / 3, 1, seed, false, false, 1, true, [offsetPoint]);
bm = new Bitmap(perlin_bmd);
bm.width = rectX * w;
bm.height = rectY * h;
addChild(bm);
for (var r:Number = 0; r < h; r++) {
field_array[r] = [];
arrow_array[r] = [];
for (var c:Number = 0; c < w; c++) {
field_array[r][c] = {x:c, y:r, vx:0, vy:0, p:0};
if ((r != 0) && (c != 0) && (r != (h - 1)) && (c != (w - 1))) {
var arrow:Arrow = new Arrow();
arrow.x = c * rectX + rectX / 2;
arrow.y = r * rectY + rectY / 2;
addChild(arrow);
}
arrow_array[r].push(arrow);
arrow = null;
}
}
upDateField();
setInterval(upDateField, 1000);
setInterval(addFish, 500);
}
private function upDateField():void {
offsetPoint.offset(speed, 0);
perlin_bmd.perlinNoise(w / 3, h / 3, 1, seed, false, false, 1, true, [offsetPoint]);
for (var r:uint = 0; r < h; r++) {
for (var c:uint = 0; c < w; c++) {
field_array[r][c]["p"] = perlin_bmd.getPixel(c, r) * damping;
}
}
for (r = 1; r < h - 1; r++) {
for (c = 1; c < w - 1; c++) {
field_array[r][c]["vx"] = (field_array[r - 1][c]["p"] - field_array[r + 1][c]["p"]);
field_array[r][c]["vy"] = (field_array[r][c + 1]["p"] - field_array[r][c - 1]["p"]);
arrow_array[r][c].rotation = twoPointToAngle(0, field_array[r][c]["vx"], 0, field_array[r][c]["vy"]);
}
}
}
private function addFish():void {
var fish:Fish = new Fish();
fish.x = root.mouseX;
fish.y = root.mouseY;
fish.addEventListener(Event.ENTER_FRAME, swim);
fish.addEventListener("remove", remove);
addChild(fish);
fish = null;
}
private function swim(event:Event):void {
event.target.count++;
event.target.xp = Math.round(event.target.x / rectX)
event.target.yp = Math.round(event.target.y / rectY);
try { event.target.vx += (field_array[event.target.yp][event.target.xp]["vx"] - event.target.vx) / 3 } catch (e:Error) { event.target.vx = event.target.vx; }
try { event.target.vy += (field_array[event.target.yp][event.target.xp]["vy"] - event.target.vy) / 3 } catch (e:Error) { event.target.vy = event.target.vy; }
event.target.x += event.target.vx < 0 ? Math.floor(event.target.vx) : Math.ceil(event.target.vx);
event.target.y += event.target.vy < 0 ? Math.floor(event.target.vy) : Math.ceil(event.target.vy);
event.target.rotation = twoPointToAngle(0, event.target.vx, 0, event.target.vy);
if ((event.target.x < -event.target.width) || (event.target.y < -event.target.height) || (event.target.x > stage.stageWidth + event.target.width) || (event.target.y > stage.stageHeight + height) || (event.target.count > 150)) {
event.target.removeEventListener(Event.ENTER_FRAME, swim);
event.target.dispatchEvent(new Event("remove"));
}
}
private function twoPointToAngle(x1:Number, x2:Number, y1:Number, y2:Number):Number {
var tmpX:Number = x1 - x2;
var tmpY:Number = y1 - y2;
var angle:Number = (Math.atan2(tmpY, tmpX) * 180 / Math.PI);
return angle;
}
private function remove(event:Event):void {
var clip:Fish = event.target as Fish;
removeChild(clip);
clip = null;
}
}
}
import flash.display.Shape;
import flash.display.Sprite;
import flash.utils.*;
class Fish extends Sprite {
public var vx:Number;
public var vy:Number;
public var xp:Number;
public var yp:Number;
public var count:uint = 0;
private var graph1:Shape;
private var graph2:Shape;
public function Fish() {
count = 0;
vx = Math.random() * 50 - 25;
vy = Math.random() * 50 - 25;
graph1 = new Shape();
graph1.graphics.beginFill(0xFFFFFF);
graph1.graphics.moveTo(0, 0);
graph1.graphics.curveTo(1.4, -2.1, 8, -2.5);
graph1.graphics.curveTo(10, -4.5, 11, -4.5);
graph1.graphics.curveTo(11, -2.5, 10, -2.5);
graph1.graphics.curveTo(14, -2.5, 22, -1.5);
graph1.graphics.curveTo(26.6, -3, 27, -2);
graph1.graphics.curveTo(26.6, -1, 26, -1);
graph1.graphics.curveTo(27.6, -1, 28.8, 1.2);
graph1.graphics.curveTo(27.6, 2.5, 22, -.5);
graph1.graphics.curveTo(14, 2.5, 10, 2.5);
graph1.graphics.curveTo(11, 2.5, 11, 4.5);
graph1.graphics.curveTo(10, 4.5, 8, 2.5);
graph1.graphics.curveTo(1.4, 2.1, 0, 0);
graph1.graphics.endFill();
graph2 = new Shape();
graph2.graphics.beginFill(0xFFFFFF);
graph2.graphics.moveTo(0, 0);
graph2.graphics.curveTo(1.4, -2.1, 8, -2.5);
graph2.graphics.curveTo(10, -3.5, 11, -3.5);
graph2.graphics.curveTo(11, -2.5, 10, -2.5);
graph2.graphics.curveTo(14, -2.5, 22, .5);
graph2.graphics.curveTo(27.6, -2, 28.8, -1);
graph2.graphics.curveTo(27.6, 1, 26, 1);
graph2.graphics.curveTo(26.6, 2, 27, 2.2);
graph2.graphics.curveTo(26.6, 3.5, 22, 1.5);
graph2.graphics.curveTo(14, 2.5, 10, 2.5);
graph2.graphics.curveTo(11, 2.5, 11, 3.5);
graph2.graphics.curveTo(10, 3.5, 8, 2.5);
graph2.graphics.curveTo(1.4, 2.1, 0, 0);
graph2.graphics.endFill();
graph2.visible = false;
addChild(graph1);
addChild(graph2);
setInterval(anime, 100);
}
private function anime():void {
graph1.visible = !graph1.visible;
graph2.visible = !graph2.visible;
}
}
class Arrow extends Sprite {
public function Arrow() {
graphics.lineStyle(1, 0xFFFFFF, 1)
graphics.moveTo(3, 0);
graphics.lineTo(-3, 0);
graphics.moveTo(0, -3);
graphics.lineTo(-3, 0);
graphics.lineTo(0, 3);
}
}