Box2DFlashAS3が対応したと聞いて。
WonderFlでBox2DFlashAS3が対応してくれて、嬉しい!
* 若干、興奮気味です。
*
* ソースが異常に長くなってしまいました。
*
/*
*
* WonderFlでBox2DFlashAS3が対応してくれて、嬉しい!
* 若干、興奮気味です。
*
* ソースが異常に長くなってしまいました。
*
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
[SWF(backgroundColor="0x000000")]
public class Main extends Sprite {
public var m_sprite:Sprite;
public var m_world:b2World;
public var m_physScale:Number = 30.0;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
public var m_mouseJoint:b2MouseJoint;
static public var mouseXWorldPhys:Number;
static public var mouseYWorldPhys:Number;
static public var mouseXWorld:Number;
static public var mouseYWorld:Number;
public var m_input:Input;
private var mousePVec:b2Vec2 = new b2Vec2();
public function Main():void {
//コンテナ
m_sprite = new Sprite();
addChild(m_sprite);
m_input = new Input(m_sprite);
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
//WORLD
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000.0, -1000.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
//デバック用
var dbgDraw:b2DebugDraw= new b2DebugDraw();
var dbgSprite:Sprite= new Sprite();
m_sprite.addChild(dbgSprite);
dbgDraw.m_sprite= dbgSprite;
dbgDraw.m_drawScale= 30.0;
dbgDraw.m_fillAlpha= 0.3;
dbgDraw.m_lineThickness= 1.0;
dbgDraw.m_alpha=1.0;
dbgDraw.m_xformScale=1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(dbgDraw);
//オブジェクト作成
var wallObject:b2PolygonDef = new b2PolygonDef();
wallObject.density = 0.0;
var wallDef:b2BodyDef = new b2BodyDef();
var wallBody:b2Body;
//Left
wallDef.position.Set(10/2 / m_physScale, 465/2 / m_physScale);
wallObject.SetAsBox(10/2 / m_physScale, 465/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Right
wallDef.position.Set((465-10/2) / m_physScale, 465/2 / m_physScale);
wallObject.SetAsBox(10/2 / m_physScale, 465/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Top
wallDef.position.Set(465/2 / m_physScale, 10/2 / m_physScale);
wallObject.SetAsBox(465/2 / m_physScale, 10/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
//Bottom
wallDef.position.Set(465/2 / m_physScale, (465-10/2) / m_physScale);
wallObject.SetAsBox(465/2 / m_physScale, 10/2 / m_physScale);
wallBody = m_world.CreateBody(wallDef);
wallBody.CreateShape(wallObject);
wallBody.SetMassFromShapes();
var bd:b2BodyDef;
var circ:b2CircleDef = new b2CircleDef();
var box:b2PolygonDef = new b2PolygonDef();
var jd:b2DistanceJointDef = new b2DistanceJointDef();
circ.radius = 12.5 / m_physScale;
circ.density = 1.0;
circ.friction = 0.4;
circ.restitution = 0.3;
box.SetAsBox(20 / m_physScale, 20 / m_physScale);
box.density = 1.0;
box.friction = 0.4;
box.restitution = 0.3;
bd = new b2BodyDef();
bd.position.Set(250 / m_physScale, 250 / m_physScale);
var circbd:b2Body = m_world.CreateBody(bd);
circbd.CreateShape(circ);
circbd.SetMassFromShapes();
bd.position.Set(230 / m_physScale, 280 / m_physScale);
var circbd2:b2Body = m_world.CreateBody(bd);
circbd2.CreateShape(circ);
circbd2.SetMassFromShapes();
bd.position.Set(270 / m_physScale, 280 / m_physScale);
var circbd3:b2Body = m_world.CreateBody(bd);
circbd3.CreateShape(circ);
circbd3.SetMassFromShapes();
jd.Initialize(circbd, circbd2, circbd.GetPosition(), circbd2.GetPosition());
m_world.CreateJoint(jd);
jd.Initialize(circbd2, circbd3, circbd2.GetPosition(), circbd3.GetPosition());
m_world.CreateJoint(jd);
jd.Initialize(circbd3, circbd, circbd3.GetPosition(), circbd.GetPosition());
m_world.CreateJoint(jd);
for (var i:uint = 0; i < 20; i++ ) {
box.SetAsBox((10 + Math.random() * 25) / m_physScale, (10 + Math.random() * 25) / m_physScale);
bd.position.Set(Math.random() * 465 / m_physScale, Math.random() * 465 / m_physScale);
var boxbd:b2Body = m_world.CreateBody(bd);
boxbd.CreateShape(box);
boxbd.SetMassFromShapes();
}
for (i = 0; i < 20; i++ ) {
circ.radius = (10 + Math.random() * 30) / m_physScale;
bd.position.Set(Math.random() * 465 / m_physScale, Math.random() * 465 / m_physScale);
var circbd4:b2Body = m_world.CreateBody(bd);
circbd4.CreateShape(circ);
circbd4.SetMassFromShapes();
}
}
private function update(event:Event):void {
m_world.Step(m_timeStep, m_iterations);
m_sprite.graphics.clear();
UpdateMouseWorld();
MouseDrag();
Input.update();
// joints
for (var jj:b2Joint = m_world.m_jointList; jj; jj = jj.m_next){
DrawJoint(jj);
}
}
//======================
// Update mouseWorld
//======================
public function UpdateMouseWorld():void{
mouseXWorldPhys = (Input.mouseX)/m_physScale;
mouseYWorldPhys = (Input.mouseY)/m_physScale;
mouseXWorld = (Input.mouseX);
mouseYWorld = (Input.mouseY);
}
//======================
// Mouse Drag
//======================
public function MouseDrag():void{
// mouse press
if (Input.mouseDown && !m_mouseJoint){
var body:b2Body = GetBodyAtMouse();
if (body)
{
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = body;
md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
md.maxForce = 300.0 * body.GetMass();
md.timeStep = m_timeStep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
body.WakeUp();
}
}
// mouse release
if (!Input.mouseDown){
if (m_mouseJoint)
{
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
}
// mouse move
if (m_mouseJoint)
{
var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
m_mouseJoint.SetTarget(p2);
}
}
//======================
// GetBodyAtMouse
//======================
public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body{
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
// Query the world for overlapping shapes.
var k_maxCount:int = 10;
var shapes:Array = new Array();
var count:int = m_world.Query(aabb, shapes, k_maxCount);
var body:b2Body = null;
for (var i:int = 0; i < count; ++i)
{
if (shapes[i].GetBody().IsStatic() == false || includeStatic)
{
var tShape:b2Shape = shapes[i] as b2Shape;
var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
if (inside)
{
body = tShape.GetBody();
break;
}
}
}
return body;
}
//======================
// Draw Joint
//======================
public function DrawJoint(joint:b2Joint):void
{
var b1:b2Body = joint.m_body1;
var b2:b2Body = joint.m_body2;
var x1:b2Vec2 = b1.m_linearVelocity;
var x2:b2Vec2 = b2.m_linearVelocity;
var p1:b2Vec2 = joint.GetAnchor1();
var p2:b2Vec2 = joint.GetAnchor2();
m_sprite.graphics.lineStyle(1,0x44aaff,1/1);
switch (joint.m_type)
{
case b2Joint.e_distanceJoint:
case b2Joint.e_mouseJoint:
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;
case b2Joint.e_pulleyJoint:
var pulley:b2PulleyJoint = joint as b2PulleyJoint;
var s1:b2Vec2 = pulley.m_groundAnchor1;
var s2:b2Vec2 = pulley.m_groundAnchor2;
m_sprite.graphics.moveTo(s1.x * m_physScale, s1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.moveTo(s2.x * m_physScale, s2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
break;
default:
if (b1 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
}
else if (b2 == m_world.m_groundBody){
m_sprite.graphics.moveTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x1.x * m_physScale, x1.y * m_physScale);
}
else{
m_sprite.graphics.moveTo(x1.x * m_physScale, x1.y * m_physScale);
m_sprite.graphics.lineTo(p1.x * m_physScale, p1.y * m_physScale);
m_sprite.graphics.lineTo(x2.x * m_physScale, x2.y * m_physScale);
m_sprite.graphics.lineTo(p2.x * m_physScale, p2.y * m_physScale);
}
}
}
}
}
//===========================================================
//=========================================================//
// -=ANTHEM=-
// file: .as
//
// copyright: Matthew Bush 2007
//
// notes:
//
//=========================================================//
//===========================================================
//===========================================================
// Input class
//===========================================================
import flash.display.*;
import flash.events.*;
class Input{
//======================
// constructor
//======================
public function Input(stageMc:Sprite){
m_stageMc = stageMc;
// init ascii array
ascii = new Array(222)
fillAscii();
// init key state array
keyState = new Array(222);
keyArr = new Array();
for (var i:int = 0; i < 222; i++){
keyState[i] = new int(0);
if (ascii[i] != undefined){
keyArr.push(i);
}
}
// buffer
bufferSize = 5;
keyBuffer = new Array(bufferSize);
for (var j:int = 0; j < bufferSize; j++){
keyBuffer[j] = new Array(0,0);
}
// add key listeners
stageMc.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress, false, 0, true);
stageMc.stage.addEventListener(KeyboardEvent.KEY_UP, keyRelease, false, 0, true);
// mouse listeners
stageMc.stage.addEventListener(MouseEvent.MOUSE_DOWN, mousePress, false, 0, true);
stageMc.stage.addEventListener(MouseEvent.CLICK, mouseRelease, false, 0, true);
stageMc.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove, false, 0, true);
stageMc.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave, false, 0, true);
mouse.graphics.lineStyle(0.1, 0, 100);
mouse.graphics.moveTo(0,0);
mouse.graphics.lineTo(0,0.1);
}
//======================
// update
//======================
static public function update():void{
// array of used keys
/*var kArr:Array = new Array(
Globals.keyP1Up,
Globals.keyP1Down,
Globals.keyP1Left,
Globals.keyP1Right,
Globals.keyP1Attack1,
Globals.keyP1Attack2,
Globals.keyP1Jump,
Globals.keyP1Defend,
Globals.keyResetGame,
Globals.keyInvertBg,
Globals.keyChangeBg,
Globals.keyPauseGame);*/
// update used keys
for (var i:int = 0; i < keyArr.length; i++){
if (keyState[keyArr[i]] != 0){
keyState[keyArr[i]]++;
}
}
// update buffer
for (var j:int = 0; j < bufferSize; j++){
keyBuffer[j][1]++;
}
// end mouse release
mouseReleased = false;
mousePressed = false;
mouseOver = false;
}
//======================
// mousePress listener
//======================
public function mousePress(e:MouseEvent):void {
mousePressed = true;
mouseDown = true;
mouseDragX = 0;
mouseDragY = 0;
}
//======================
// mousePress listener
//======================
public function mouseRelease(e:MouseEvent):void {
mouseDown = false;
mouseReleased = true;
}
//======================
// mousePress listener
//======================
public function mouseLeave(e:Event):void {
mouseReleased = mouseDown;
mouseDown = false;
}
//======================
// mouseMove listener
//======================
public function mouseMove(e:MouseEvent):void {
// Fix mouse release not being registered from mouse going off stage
if (mouseDown != e.buttonDown){
mouseDown = e.buttonDown;
mouseReleased = !e.buttonDown;
mousePressed = e.buttonDown;
mouseDragX = 0;
mouseDragY = 0;
}
mouseX = e.stageX - m_stageMc.x;
mouseY = e.stageY - m_stageMc.y;
// Store offset
mouseOffsetX = mouseX - mouse.x;
mouseOffsetY = mouseY - mouse.y;
// Update drag
if (mouseDown){
mouseDragX += mouseOffsetX;
mouseDragY += mouseOffsetY;
}
mouse.x = mouseX;
mouse.y = mouseY;
}
//======================
// getKeyHold
//======================
static public function getKeyHold(k:int):int{
return Math.max(0, keyState[k]);
}
//======================
// isKeyDown
//======================
static public function isKeyDown(k:int):Boolean{
return (keyState[k] > 0);
}
//======================
// isKeyPressed
//======================
static public function isKeyPressed(k:int):Boolean{
timeSinceLastKey = 0;
return (keyState[k] == 1);
}
//======================
// isKeyReleased
//======================
static public function isKeyReleased(k:int):Boolean{
return (keyState[k] == -1);
}
//======================
// isKeyInBuffer
//======================
static public function isKeyInBuffer(k:int, i:int, t:int):Object {
return (keyBuffer[i][0] == k && keyBuffer[i][1] <= t);
}
//======================
// keyPress function
//======================
public function keyPress(e:KeyboardEvent):void {
//strace ( e.keyCode + " : " + ascii[e.keyCode] );
// set keyState
keyState[e.keyCode] = Math.max(keyState[e.keyCode], 1);
// last key (for key config)
lastKey = e.keyCode;
}
//======================
// keyRelease function
//======================
public function keyRelease(e:KeyboardEvent):void {
keyState[e.keyCode] = -1;
// add to key buffer
for (var i:int = bufferSize-1; i > 0 ; i--){
keyBuffer[i] = keyBuffer[i - 1];
}
keyBuffer[0] = [e.keyCode, 0];
}
//======================
// get key string
//======================
static public function getKeyString(k:uint):String{
return ascii[k];
}
//======================
// set up ascii text
//======================
private function fillAscii():void{
ascii[65] = "A";
ascii[66] = "B";
ascii[67] = "C";
ascii[68] = "D";
ascii[69] = "E";
ascii[70] = "F";
ascii[71] = "G";
ascii[72] = "H";
ascii[73] = "I";
ascii[74] = "J";
ascii[75] = "K";
ascii[76] = "L";
ascii[77] = "M";
ascii[78] = "N";
ascii[79] = "O";
ascii[80] = "P";
ascii[81] = "Q";
ascii[82] = "R";
ascii[83] = "S";
ascii[84] = "T";
ascii[85] = "U";
ascii[86] = "V";
ascii[87] = "W";
ascii[88] = "X";
ascii[89] = "Y";
ascii[90] = "Z";
ascii[48] = "0";
ascii[49] = "1";
ascii[50] = "2";
ascii[51] = "3";
ascii[52] = "4";
ascii[53] = "5";
ascii[54] = "6";
ascii[55] = "7";
ascii[56] = "8";
ascii[57] = "9";
ascii[32] = "Spacebar";
ascii[17] = "Ctrl";
ascii[16] = "Shift";
ascii[192] = "~";
ascii[38] = "up";
ascii[40] = "down";
ascii[37] = "left";
ascii[39] = "right";
ascii[96] = "Numpad 0";
ascii[97] = "Numpad 1";
ascii[98] = "Numpad 2";
ascii[99] = "Numpad 3";
ascii[100] = "Numpad 4";
ascii[101] = "Numpad 5";
ascii[102] = "Numpad 6";
ascii[103] = "Numpad 7";
ascii[104] = "Numpad 8";
ascii[105] = "Numpad 9";
ascii[111] = "Numpad /";
ascii[106] = "Numpad *";
ascii[109] = "Numpad -";
ascii[107] = "Numpad +";
ascii[110] = "Numpad .";
ascii[45] = "Insert";
ascii[46] = "Delete";
ascii[33] = "Page Up";
ascii[34] = "Page Down";
ascii[35] = "End";
ascii[36] = "Home";
ascii[112] = "F1";
ascii[113] = "F2";
ascii[114] = "F3";
ascii[115] = "F4";
ascii[116] = "F5";
ascii[117] = "F6";
ascii[118] = "F7";
ascii[119] = "F8";
ascii[188] = ",";
ascii[190] = ".";
ascii[186] = ";";
ascii[222] = "'";
ascii[219] = "[";
ascii[221] = "]";
ascii[189] = "-";
ascii[187] = "+";
ascii[220] = "\\";
ascii[191] = "/";
ascii[9] = "TAB";
ascii[8] = "Backspace";
//ascii[27] = "ESC";
}
//======================
// member data
//======================
// key text array
static public var ascii:Array;
static private var keyState:Array;
static private var keyArr:Array;
static private var keyBuffer:Array;
static private var bufferSize:int;
// last key pressed
static public var lastKey:int = 0;
static public var timeSinceLastKey:Number = 0;
// mouse states
static public var mouseDown:Boolean = false;
static public var mouseReleased:Boolean = false;
static public var mousePressed:Boolean = false;
static public var mouseOver:Boolean = false;
static public var mouseX:Number = 0;
static public var mouseY:Number = 0;
static public var mouseOffsetX:Number = 0;
static public var mouseOffsetY:Number = 0;
static public var mouseDragX:Number = 0;
static public var mouseDragY:Number = 0;
static public var mouse:Sprite = new Sprite();
// stage
static public var m_stageMc:Sprite;
}
// End of file
//===========================================================
//===========================================================