package {
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.initializers.Color;
import idv.cjcat.stardust.common.initializers.Mass;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.Deflect;
import idv.cjcat.stardust.twoD.actions.Gravity;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.actions.SpeedLimit;
import idv.cjcat.stardust.twoD.deflectors.WrappingBox;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.fields.BitmapField;
import idv.cjcat.stardust.twoD.initializers.Position;
import idv.cjcat.stardust.twoD.zones.RectZone;
public class PixelEmitter extends Emitter2D {
public var field:BitmapField;
public function PixelEmitter(clock:Clock) {
super(clock);
//initializers
addInitializer(new Color(0x33CCFF));
addInitializer(new Position(new RectZone(0, 0, 640, 480)));
addInitializer(new Mass(new UniformRandom(5, 2)));
//actions
field = new BitmapField(0, 60);
field.massless = false;
field.scaleX = field.scaleY = 2;
var gravity:Gravity = new Gravity();
gravity.addField(field);
var deflect:Deflect = new Deflect();
deflect.addDeflector(new WrappingBox(0, 0, 640, 480));
addAction(gravity);
addAction(deflect);
addAction(new Move());
addAction(new SpeedLimit(2));
}
}
}
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
import flash.utils.getTimer;
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.clocks.ImpulseClock;
import idv.cjcat.stardust.common.emitters.Emitter;
import idv.cjcat.stardust.twoD.handlers.PixelHandler;
import net.hires.debug.Stats;
public class TestStardust1 extends Sprite
{
protected var stats:Stats;
protected var container:Sprite;
protected var emitter:Emitter;
private var pixelBmpd:BitmapData;
private var pixelBmp:Bitmap;
//时钟
private var clock:ImpulseClock;
//????
private var turbulenceBMPD:BitmapData;
public function TestStardust1()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
root.scrollRect = new Rectangle(0, 0, 640, 480);
this.stage.frameRate = 50;
stats = new Stats();
addChild(stats);
this.container = new Sprite();
addChild(container);
this.pixelBmpd = new BitmapData(640,480,true,0);
this.pixelBmp = new Bitmap(this.pixelBmpd);
container.addChild(this.pixelBmp);
turbulenceBMPD = new BitmapData(320,240,false);
clock = new ImpulseClock(800);
clock.impulse();
this.emitter = new PixelEmitter(clock);
emitter.particleHandler = new PixelHandler(this.pixelBmpd);
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
var timer:Timer = new Timer(1500);
timer.addEventListener(TimerEvent.TIMER, tick);
timer.start();
tick();
}
private function tick(e:TimerEvent = null):void
{
this.turbulenceBMPD.perlinNoise(100,100,2,int(Math.random()*100),false,false,BitmapDataChannel.RED | BitmapDataChannel.GREEN);
PixelEmitter(emitter).field.update(this.turbulenceBMPD);
}
private function onEnterFrame(e:Event):void
{
this.pixelBmpd.unlock();
this.pixelBmpd.fillRect(this.pixelBmpd.rect,0);
this.emitter.step();
this.pixelBmpd.unlock();
}
}
}