home *** CD-ROM | disk | FTP | other *** search
- package core.bonuses
- {
- import Box2D.Collision.Shapes.b2CircleDef;
- import Box2D.Collision.Shapes.b2Shape;
- import Box2D.Collision.b2ContactPoint;
- import Box2D.Common.Math.b2Math;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Dynamics.b2Body;
- import core.BContactListener;
- import core.events.CoreEvent;
- import core.objects.Circle;
- import core.objects.PlayerBall;
- import core.objects.Rect;
- import flash.display.Graphics;
- import flash.geom.Point;
-
- public class BonusPushField extends AbstractBonus
- {
-
-
- private var pushFieldBodies:Array;
-
- private var pushFieldShape:b2Shape;
-
- private var player:PlayerBall;
-
- private var pushFieldRadius:Number;
-
- public function BonusPushField()
- {
- super(true);
- }
-
- override protected function deactivate() : void
- {
- player.getBody().DestroyShape(pushFieldShape);
- pushFieldShape = null;
- engine.contactListener.dispatcher.removeEventListener(BContactListener.COLLIDE_PERSIST,collidePersistHandler);
- super.deactivate();
- }
-
- override protected function init() : void
- {
- super.init();
- lifeTime = 40 * 7;
- player = engine.playerBall;
- pushFieldRadius = 200;
- pushFieldBodies = [];
- }
-
- private function doActivatePushField() : void
- {
- var shapeDef:b2CircleDef = new b2CircleDef();
- shapeDef.radius = pushFieldRadius / engine.m_physScale;
- shapeDef.isSensor = true;
- pushFieldShape = player.getBody().CreateShape(shapeDef);
- engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_PERSIST,collidePersistHandler);
- }
-
- override protected function initSounds() : void
- {
- var PushfieldBonusClass:Class = engine.assets.getAssetClass("SndPushfieldBonus");
- sndActivate = new PushfieldBonusClass();
- }
-
- private function updatePushField() : void
- {
- var otherBody:b2Body = null;
- var sub:b2Vec2 = null;
- var len:Number = NaN;
- var impulse:b2Vec2 = null;
- while(pushFieldBodies.length > 0)
- {
- otherBody = pushFieldBodies.pop();
- sub = b2Math.SubtractVV(otherBody.GetWorldCenter(),player.getBody().GetWorldCenter());
- len = sub.Normalize();
- if(len * engine.m_physScale < pushFieldRadius)
- {
- impulse = sub.Copy();
- impulse.Multiply(pushFieldRadius / engine.m_physScale - len);
- otherBody.ApplyImpulse(impulse,otherBody.GetWorldCenter());
- drawPushFieldLine(otherBody);
- }
- }
- }
-
- override protected function updateHandler(e:CoreEvent) : void
- {
- if(!pushFieldShape)
- {
- doActivatePushField();
- }
- else
- {
- updatePushField();
- }
- super.updateHandler(e);
- }
-
- override public function activate(activateTime:Number) : void
- {
- super.activate(activateTime);
- }
-
- private function collidePersistHandler(e:CoreEvent) : void
- {
- var otherBody:b2Body = null;
- var playerBody:b2Body = null;
- var playerShape:b2Shape = null;
- var point:b2ContactPoint = e.data as b2ContactPoint;
- var body1:b2Body = point.shape1.m_body;
- var body2:b2Body = point.shape2.m_body;
- if(body1.m_userData == player || body2.m_userData == player)
- {
- otherBody = body1.m_userData == player ? body2 : body1;
- playerBody = body1.m_userData == player ? body1 : body2;
- playerShape = body1.m_userData == player ? point.shape1 : point.shape2;
- if(playerShape == pushFieldShape && (otherBody.m_userData is Circle || otherBody.m_userData is Rect))
- {
- if(pushFieldBodies.indexOf(otherBody) == -1)
- {
- pushFieldBodies.push(otherBody);
- }
- }
- }
- }
-
- private function drawPushFieldLine(otherBody:b2Body) : void
- {
- var dx:Number = NaN;
- var dy:Number = NaN;
- var pos1:b2Vec2 = player.getBody().GetPosition();
- var pos2:b2Vec2 = otherBody.GetPosition();
- var p1:Point = new Point(pos1.x * engine.m_physScale,pos1.y * engine.m_physScale);
- var p2:Point = new Point(pos2.x * engine.m_physScale,pos2.y * engine.m_physScale);
- var dist:Number = pushFieldRadius - Point.distance(p1,p2);
- var lineThickness:Number = dist / pushFieldRadius * 10;
- var lineAlpha:Number = dist / pushFieldRadius;
- var gr:Graphics = engine.canvas.effectsContainer.graphics;
- gr.lineStyle(lineThickness,16777215,lineAlpha);
- gr.moveTo(p1.x,p1.y);
- gr.lineTo(p2.x,p2.y);
- gr.lineStyle(0,16777215,lineAlpha);
- var bounces:uint = 2 + Math.round(Math.random() * 6);
- for(var i:uint = 0; i < bounces; i++)
- {
- gr.moveTo(p2.x,p2.y);
- dx = -10 + Math.random() * 20;
- dy = -10 + Math.random() * 20;
- gr.lineTo(p2.x + dx,p2.y + dy);
- }
- }
- }
- }
-