package org.philipandrews.up { import Box2D.Collision.Shapes.b2CircleDef; import Box2D.Dynamics.b2Body; import br.com.stimuli.loading.BulkLoader; import com.efnx.fps.fpsBox; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.KeyboardEvent; import flash.geom.Point; import flash.text.TextField; import flash.text.TextFormat; import flash.ui.Keyboard; public class Main extends Sprite { [Embed(source="../bin-debug/img/ballonshape.png")] private var BalloonImg:Class; private var _boxWorld:BoxWorld; private var _loader:BulkLoader; private var _ballonShape:Sprite; private var _radius:Number = 10; private var _shapeBitmapData:BitmapData; private var _ballBitmap:Bitmap; private var _anchor:b2Body; private var _anchorDensity:Number = 70; private var _ballDensity:Number = 4 private var _gravity:Number = 6//.5; private var _antiGravity:Number = 11; private var _antiGravityThrust:Number = _antiGravity * 1.5; private var _leftThrust:Number = _antiGravityThrust * 3.5; private var _rightThrust:Number = _antiGravityThrust * 3.5; private var _springsOn:Boolean = true//false private var _fps:fpsBox; private var _box_center_x:Number private var _box_center_y:Number public function Main() { _fps = new fpsBox(); addChild(_fps); _fps.x = 20 _fps.y = 20 _boxWorld = new BoxWorld(true,_gravity, _antiGravity); // _anchor. addChild(_boxWorld); _ballonShape = new Sprite(); addChild(_ballonShape); var b:Bitmap = new BalloonImg() as Bitmap; b.alpha = 0 // b.visible = false; _ballonShape.addChild(b); addEventListener(Event.ADDED_TO_STAGE, init); } private function onLoaderError(e:Event):void{ trace(e); } private function init(e:Event):void{ stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.EXACT_FIT; _boxWorld.createWalls(stage); _ballonShape.x = Math.round( (stage.stageWidth /2) - (_ballonShape.width/2)) _ballonShape.y = Math.round( (stage.stageHeight /2) - (_ballonShape.height/2)) var ax:Number = Math.round( (stage.stageWidth /2) - (10/2)) _box_center_x = _ballonShape.x + (_ballonShape.width / 2) _box_center_y = _ballonShape.y + (_ballonShape.height + 60) _anchor = _boxWorld.createBox(_box_center_x, _box_center_y, 12, 10, "anchor", _anchorDensity); /* var burner1:b2CircleDef; burner1 = new b2CircleDef(); burner1.radius = 3/BoxWorld.PM // the_box2.SetAsBox(width/PM,2/PM); burner1.friction=0; burner1.localPosition.Set(15/BoxWorld.PM, 0/BoxWorld.PM); burner1.density=0; var burner2:b2CircleDef; burner2 = new b2CircleDef(); burner2.radius = 3/BoxWorld.PM // the_box2.SetAsBox(width/PM,2/PM); burner2.friction=0; burner2.localPosition.Set(-15/BoxWorld.PM, 0/BoxWorld.PM); burner2.density=0; _anchor.CreateShape(burner1); _anchor.CreateShape(burner2); _anchor.SetMassFromShapes(); */ _boxWorld.createBox(_box_center_x-100, _box_center_y-190, 40, 2, "static", 0); _boxWorld.createBox(_box_center_x-350, _box_center_y-80, 40, 2, "static", 0); _boxWorld.createBox(_box_center_x-270, _box_center_y, 40, 2, "static", 0); _boxWorld.createBox(_box_center_x+300, _box_center_y-170, 40, 2, "static", 0); _boxWorld.createBox(_box_center_x+80, _box_center_y+60, 20, 2, "static", 0); _boxWorld.createBox(_box_center_x+160, _box_center_y+60, 20, 2, "static", 0); _boxWorld.createBox(_box_center_x+121, _box_center_y+90, 20, 2, "static", 0); trace("shape", _anchor.GetShapeList()); // _anchor.DestroyShape(_anchor.GetShapeList()); // _anchor = _boxWorld.createBox(600, 120, 10, 10, "anchor"); addMiniBalloons(); stage.addEventListener(Event.RESIZE, onResize); stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); } private function addKeyArrows():void{ var tf:TextFormat = new TextFormat(); var up:TextField = new TextField(); // up. } private function addMiniBalloons():void{ var x:Number = 0; var y:Number = 0; var cols:int = Math.ceil((_ballonShape.width / diameter)); //trace("c ", (_ballonShape.width / 20)); var rows:int = Math.ceil((_ballonShape.height / diameter)); //trace("r ", (_ballonShape.height / 20)); var max:int = cols * rows;//(_ballonShape.width / 20) * (_ballonShape.height / 20) for(var i:int=0; i < max; i++){ var c:Shape = new Shape(); //c.blendMode = BlendMode.MULTIPLY c.graphics.beginFill(0xFF0000); c.graphics.drawCircle(_radius, _radius, _radius); c.graphics.endFill(); if(i % cols == 0 && i > 0){ x = 0 y += diameter }else if (i > 0) x += diameter c.x = x;//i * 20; c.y = y//i * 20; if(checkBallOnShape(c)){ _ballonShape.addChild(c); c.alpha = 0//.1; var global:Point = c.localToGlobal(new Point(_radius, _radius)) var box:b2Body = _boxWorld.createBall(global.x, global.y, _radius, "ball", _ballDensity); if(_springsOn) // _boxWorld.createSpring(_anchor, box); _boxWorld.createJoint(_anchor, box, _anchor.GetPosition().x , _anchor.GetPosition().y-(10/BoxWorld.PM)) // trace(global.x, global.y); /* // distance joint if(global.x < box_center){ _boxWorld.createJoint(_anchor, box, _anchor.GetPosition().x-(10/BoxWorld.PM), _anchor.GetPosition().y-(10/BoxWorld.PM)) } else if(global.x > box_center){ _boxWorld.createJoint(_anchor, box, _anchor.GetPosition().x+(10/BoxWorld.PM), _anchor.GetPosition().y-(10/BoxWorld.PM)) }else{ _boxWorld.createJoint(_anchor, box, _anchor.GetPosition().x , _anchor.GetPosition().y-(10/BoxWorld.PM)) } */ } } } private function get diameter():Number{ return _radius * 2 } private function checkBallOnShape(ball:DisplayObject):Boolean{ if( _shapeBitmapData == null ){ _shapeBitmapData = new BitmapData(_ballonShape.width, _ballonShape.height, true, 0xFFFFFF); _shapeBitmapData.draw(_ballonShape.getChildAt(0)); } if( _ballBitmap == null){ var ballBmd:BitmapData = new BitmapData(diameter, diameter, true, 0xFFFFFF); ballBmd.draw(ball) _ballBitmap = new Bitmap(ballBmd); } _ballBitmap.x = ball.x; _ballBitmap.y = ball.y; // var p1:Point = new Point(_ballonShape.x, _ballonShape.y) var p1:Point = new Point(0, 0) var p2:Point = new Point(_ballBitmap.x, _ballBitmap.y) if(_shapeBitmapData.hitTest(p1, 255, _ballBitmap.bitmapData, p2, 255)){ return true; }else return false; } private function onResize(e:Event):void{ _boxWorld.createWalls(stage); } private function onKeyUp(e:KeyboardEvent):void{ if(e.keyCode == Keyboard.UP) _boxWorld.upVelocity = _antiGravity if(e.keyCode == Keyboard.LEFT) _boxWorld.leftVelocity = 0 if(e.keyCode == Keyboard.RIGHT) _boxWorld.rightVelocity = 0 } private function onKeyDown(e:KeyboardEvent):void{ if(e.keyCode == Keyboard.UP) _boxWorld.upVelocity = _antiGravityThrust if(e.keyCode == Keyboard.LEFT) _boxWorld.leftVelocity = _leftThrust if(e.keyCode == Keyboard.RIGHT) _boxWorld.rightVelocity = _rightThrust } } }