イクラ噴水/ Ikura fountain
増やしてみました&衝突処理を改良しました
More Ikuras!
// forked from Susisu's 湧き出るいくら(60FPS)
// forked from Susisu's 湧き出るいくら
// forked from Susisu's flash on 2011-3-5
package {
import flash.geom.Point;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import net.hires.debug.Stats;
[SWF(frameRate="60")]
public class IkuraNullNull extends Sprite {
private var first:Ikura;
private var canvas:BitmapData;
private var bmp:Bitmap;
private var rect:Rectangle;
private var circle:BitmapData;
private var crect:Rectangle;
private const ZERO_POINT:Point = new Point(0, 0);
private const NUMBER_OF_IKURAS:int = 600;
public function IkuraNullNull() {
canvas = new BitmapData(465, 465, true, 0);
rect = canvas.rect;
bmp = new Bitmap(canvas);
addChild(bmp);
var s:Shape = new Shape();
s.graphics.beginFill(0xff8000);
s.graphics.drawCircle(8, 8, 8);
s.graphics.endFill();
s.graphics.beginFill(0xc03000);
s.graphics.drawCircle(8, 4, 4);
s.graphics.endFill();
s.graphics.beginFill(0xffffff);
s.graphics.drawCircle(4, 4, 2);
s.graphics.endFill();
circle = new BitmapData(16, 16, true, 0);
circle.draw(s);
crect = circle.rect;
var r:Number = Math.random() * 2 * Math.PI;
first = new Ikura(Math.random() * 465,Math.random() * 465, Math.cos(r), Math.sin(r));
var prev:Ikura = first;
for(var i:int = 0; i < NUMBER_OF_IKURAS; i++) {
r = Math.random() * 2 * Math.PI;
var next:Ikura = new Ikura(Math.random() * 465, Math.random() * 465, Math.cos(r), Math.sin(r));
prev.next = next;
prev = next;
}
addEventListener(Event.ENTER_FRAME, enterFrame);
addChild(new Stats());
}
private function enterFrame(e:Event):void{
canvas.lock();
canvas.fillRect(rect, 0);
var p:Ikura = first;
var point:Point = new Point(0, 0);
while(p){
var x:Number = p.x += p.vx;
var y:Number = p.y += p.vy;
if(x < 0) {
p.x = 0;
p.vx = -p.vx * 0.2;
}
else if(x > 465 - 16) {
p.x = 465 - 16;
p.vx = -p.vx * 0.2;
}
if(y < 0) {
p.y = 0;
p.vy = -p.vy*0.2;
}
else if(y > 465 - 16) {
p.y = 465 - 16;
p.vy = -p.vy * 0.2;
}
var q:Ikura = first;
while(q) {
var dx:Number = x - q.x;
var dy:Number = y - q.y;
if(dx < 16 && dx > -16 && dy < 16 && dy > -16) {
var d:Number = dx * dx + dy * dy;
if(d < 256) {
d = Math.sqrt(d);
var invDist:Number = 1 / d;
dx *= invDist;
dy *= invDist;
d = (16 - d) * 0.5;
dx *= d;
dy *= d;
p.vx += dx;
p.vy += dy;
q.vx -= dx;
q.vy -= dy;
dx *= 0.25;
dy *= 0.25;
p.x += dx;
p.y += dy;
q.x -= dx;
q.y -= dy;
}
}
q = q.next;
if(p == q)
break;
}
p.vx *= 0.95;
p.vy *= 0.95;
if(x > 50 && x < 150 || x > 315 && x < 415)
p.vy -= 0.4;
else
p.vy += 0.4;
// canvas.setPixel32(p.x >> 0, p.y >> 0, 0xff000000);
point.x = p.x;
point.y = p.y;
canvas.copyPixels(circle, crect, point, circle, ZERO_POINT, true);
p = p.next;
}
canvas.unlock();
}
}
}
class Ikura {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var next:Ikura;
public function Ikura(x:Number, y:Number, vx:Number, vy:Number){
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
next = null;
}
}