forked from: [Stardust] + [Box2D]
/**
* Copyright groovy ( http://wonderfl.net/user/groovy )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/iVXJ
*/
// forked from paq's [Stardust] + [Box2D]
package
{
import Box2D.Collision.b2AABB;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import idv.cjcat.stardust.common.clocks.SteadyClock;
import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
import com.actionsnippet.qbox.*;
[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 60)]
public class Main extends Sprite
{
static private var DRAW_SCALE:Number = 100;
private var container:Sprite;
private var emitter:Box2DEmitter;
private var world:b2World;
public function Main():void
{
if (stage){ init(); }
else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// background-stuff
var background:Sprite = new Sprite();
var g:Graphics = background.graphics;
g.beginFill(0x000000);
g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
addChild(background);
container = new Sprite();
addChild(container);
// Box2D
box2DInit();
// Stardust
stardustInit();
// starts everything
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
//emits the circles
emitter.step();
//determines the rate of calculations
world.Step(1 / 24, 20);
}
private function stardustInit():void
{
// this is the thing that creates the circles
//steady clock determines the speed the circles are created
emitter = new Box2DEmitter(new SteadyClock(0.1), world)
var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(container);
renderer.addEmitter(emitter);
}
private function box2DInit():void
{
//this defines the charcteristics of the world
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100, -100);
worldAABB.upperBound.Set(100, 100);
// first variable of the vector = horizontal, second = vertical
var gravity:b2Vec2 = new b2Vec2(1, 1);
world = new b2World(worldAABB, gravity, true);
// creates the green floor
var floorBodyDef:b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set(465 * 0.5 / DRAW_SCALE, 465 / DRAW_SCALE);
var floorShapeDef:b2PolygonDef = new b2PolygonDef();
floorShapeDef.SetAsBox(465 * 0.5 / DRAW_SCALE, 0.1);
var floor:b2Body = world.CreateBody(floorBodyDef);
floor.CreateShape(floorShapeDef);
// Other debug stuff
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = container;
debugDraw.m_drawScale = DRAW_SCALE;
b2DebugDraw.e_jointBit;
debugDraw.m_fillAlpha = 0.8;
debugDraw.m_lineThickness = 10;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
world.SetDebugDraw(debugDraw);
}
}
}
import Box2D.Collision.Shapes.b2CircleDef;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import idv.cjcat.stardust.common.actions.Age;
import idv.cjcat.stardust.common.actions.CompositeAction;
import idv.cjcat.stardust.common.actions.DeathLife;
import idv.cjcat.stardust.common.actions.ScaleCurve;
import idv.cjcat.stardust.common.actions.triggers.DeathTrigger;
import idv.cjcat.stardust.common.actions.triggers.LifeTrigger;
import idv.cjcat.stardust.common.clocks.Clock;
import idv.cjcat.stardust.common.initializers.Life;
import idv.cjcat.stardust.common.initializers.Mask;
import idv.cjcat.stardust.common.initializers.Scale;
import idv.cjcat.stardust.common.math.UniformRandom;
import idv.cjcat.stardust.twoD.actions.Gravity;
import idv.cjcat.stardust.twoD.actions.Move;
import idv.cjcat.stardust.twoD.actions.Spawn;
import idv.cjcat.stardust.twoD.emitters.Emitter2D;
import idv.cjcat.stardust.twoD.fields.UniformField;
import idv.cjcat.stardust.twoD.initializers.DisplayObjectClass;
import idv.cjcat.stardust.twoD.initializers.Position;
import idv.cjcat.stardust.twoD.initializers.Velocity;
import idv.cjcat.stardust.twoD.zones.LazySectorZone;
import idv.cjcat.stardust.twoD.zones.Line;
import idv.cjcat.stardust.twoD.zones.SinglePoint;
import flash.display.Sprite;
class playerActor extends Sprite
{
static private var DRAW_SCALE:Number = 100;
private var _world:b2World;
private var _circleBody:b2Body;
private var _circleShape:b2CircleShape;
private var _scaleX:Number;
private var _x:Number;
private var _y:Number;
private var _move:Boolean;
public function playerActor(world:b2World, move:Boolean = false):void
{
super();
_world = world;
_move = move;
var circleBodyDef:b2BodyDef = new b2BodyDef();
circleBodyDef.position.Set(x / DRAW_SCALE, y / DRAW_SCALE);
var circleShapeDef:b2CircleDef = new b2CircleDef();
circleShapeDef.radius = 0.01;
circleShapeDef.density = 1;
circleShapeDef.friction = 0.5;
circleShapeDef.restitution = 0.5;
addEventListener(Event.REMOVED, onRemoved );
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
if (_move) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, y / DRAW_SCALE), rotation);
if (_scaleX != scaleX) _circleShape.m_radius = scaleX / 10;
if (_x != x) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, _circleBody.GetPosition().y), rotation);
if (_y != y) _circleBody.SetXForm(new b2Vec2(_circleBody.GetPosition().x, y / DRAW_SCALE), rotation);
_scaleX = scaleX;
_x = x;
_y = y;
}
private function onRemoved(e:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.REMOVED, onRemoved);
_world.DestroyBody(_circleBody);
}
}
class Box2DEmitter extends Emitter2D
{
public function Box2DEmitter(clock:Clock, world:b2World)
{
super(clock);
var doc:DisplayObjectClass = new DisplayObjectClass();
//takes the command to 'emit' circles from along the given line
doc.displayObjectClass = Circle;
doc.constructorParams = [world, false];
addInitializer(doc);
addInitializer(new Position(new Line(0, 0, 465, 30)));
addInitializer(new Life(new UniformRandom(100, 0)));
addInitializer(new Scale(new UniformRandom(3, 3)));
var commonActions:CompositeAction = new CompositeAction();
commonActions.mask = 1;
commonActions.addAction(new Age());
commonActions.addAction(new DeathLife());
commonActions.addAction(new ScaleCurve(5, 10));
addAction(commonActions);
}
}
class Circle extends Sprite
{
static private var DRAW_SCALE:Number = 100;
private var _world:b2World;
private var _circleBody:b2Body;
private var _circleShape:b2CircleShape;
private var _scaleX:Number;
private var _x:Number;
private var _y:Number;
private var _move:Boolean;
public function Circle(world:b2World, move:Boolean = false):void
{
super();
_world = world;
_move = move;
var circleBodyDef:b2BodyDef = new b2BodyDef();
circleBodyDef.position.Set(x / DRAW_SCALE, y / DRAW_SCALE);
var circleShapeDef:b2CircleDef = new b2CircleDef();
circleShapeDef.radius = 0.01;
circleShapeDef.density = 1;
circleShapeDef.friction = 0.5;
circleShapeDef.restitution = 0.5;
_circleBody = world.CreateBody(circleBodyDef);
_circleShape = _circleBody.CreateShape(circleShapeDef) as b2CircleShape;
_circleBody.SetMassFromShapes();
addEventListener(Event.REMOVED, onRemoved );
addEventListener(Event.ENTER_FRAME, onEnterFrame)
}
private function onEnterFrame(e:Event):void
{
if (_move) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, y / DRAW_SCALE), rotation);
if (_scaleX != scaleX) _circleShape.m_radius = scaleX / 10;
if (_x != x) _circleBody.SetXForm(new b2Vec2(x / DRAW_SCALE, _circleBody.GetPosition().y), rotation);
if (_y != y) _circleBody.SetXForm(new b2Vec2(_circleBody.GetPosition().x, y / DRAW_SCALE), rotation);
_scaleX = scaleX;
_x = x;
_y = y;
}
private function onRemoved(e:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.REMOVED, onRemoved);
_world.DestroyBody(_circleBody);
}
}