Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.MovieClip;
- import flash.events.Event;
- import flash.display.Sprite;
- import flash.display.Graphics;
- import flash.utils.getDefinitionByName;
- import flash.filters.GlowFilter;
- import flash.utils.getTimer;
- import flash.geom.Point;
- import flash.utils.Timer;
- public class LevelObject extends MovieClip
- {
- private var collisionList:CollisionList;
- private var removed:Boolean = false;
- private var objectType:Object;
- private var objectSkin:MovieClip;
- private var rotationDegree:int;
- private var pathVelocity:int;
- private var pathArray:Array;
- private var xSpeed:Number;
- private var ySpeed:Number;
- private var pathAngle:Number;
- private var pathPoint:int = 0;
- private var pathArrayLength:int;
- private var pathLength:Number;
- private var posX:int;
- private var posY:int;
- private var pathDelay:int;
- private var pathDelayCurrent:int;
- //Properties
- private var healthMax:int;
- private var health:int;
- private var glow:GlowFilter;
- private var rotationAngle:int;
- private var killFrames:Array = [];
- //Sparks
- private var particle:Particle;
- private var particles:Array = [];
- private var particleSpeed:int;
- private var angleOffset:Number;
- public function LevelObject(type, pX, pY, rDeg, pVelocity, pDelay, pArray)
- {
- objectType = type;
- rotationDegree = rDeg;
- pathVelocity = pVelocity;
- pathArray = pArray;
- pathArrayLength = pathArray.length/2;
- posX = pX;
- posY = pY;
- pathDelay = pDelay;
- pathDelayCurrent = pDelay;
- this.x = posX;
- this.y = posY;
- addSkin();
- addProperties();
- collisionList = new CollisionList(this, Ball.getBall());
- this.addEventListener(Event.ENTER_FRAME, collisionCheck)
- }
- private function addSkin():void
- {
- objectSkin = new (objectType);
- this.addChild(objectSkin);
- this.removeChildAt(0);
- }
- private function addProperties():void
- {
- switch(objectType)
- {
- case(RectNormal):
- healthMax = 10;
- glow = new GlowFilter();
- glow.color = 0x3864e0;
- glow.quality = 2;
- glow.blurX = 5;
- glow.blurY = 5;
- this.filters = [glow];
- killFrames = [2, 8, 14, 21];
- break;
- case(RectHard):
- healthMax = 3;
- glow = new GlowFilter();
- glow.color = 0xFF0000;
- glow.quality = 2;
- glow.blurX = 5;
- glow.blurY = 5;
- this.filters = [glow];
- break;
- case(RoundHard):
- healthMax = 3;
- break;
- }
- health = healthMax;
- if (rotationDegree != 0)
- {
- this.addEventListener(Event.ENTER_FRAME, rotate)
- }
- if (pathArray[0])
- {
- calculatePath();
- this.addEventListener(Event.ENTER_FRAME, followPath)
- }
- }
- private function collisionCheck(e:Event):void
- {
- if (collisionList.checkCollisions().length)
- {
- var collision = collisionList.checkCollisions()[0];
- var overlap:int = collision.overlapping.length;
- var sin:Number = Math.sin(collision.angle);
- var cos:Number = Math.cos(collision.angle);
- var vx0:Number = Math.cos(Ball._angle) * cos + Math.sin(Ball._angle) * sin;
- var vy0:Number = Math.sin(Ball._angle) * cos - Math.cos(Ball._angle) * sin;
- var vx = vx0 * cos - vy0 * sin;
- var vy = vy0 * cos + vx0 * sin;
- vx -= cos * overlap / (Ball.getBall().width*0.5);
- vy -= sin * overlap / (Ball.getBall().width*0.5);
- Ball._angle = Math.atan2(vy,vx);
- damageLevelObject(1);
- }
- }
- private function damageLevelObject(damage):void
- {
- health -= damage;
- for (var j:int = 0; j<5; j++)
- {
- particle = new Particle(Spark, stage, Ball.getBall().x, Ball.getBall().y, glow);
- angleOffset = Math.random()*(Math.PI/2)-(Math.PI/4);
- particleSpeed = Math.random()*7+1;
- particle.removeTimer = 5;
- particle.xSpeed = Math.cos(Ball._angle+angleOffset)*particleSpeed;
- particle.ySpeed = Math.sin(Ball._angle+angleOffset)*particleSpeed;
- particle.rotationDegree = (Ball._angle+angleOffset)/Math.PI*180;
- particles.push(particle);
- }
- addEventListener(Event.ENTER_FRAME, animateParticles)
- if(health <= 0 )
- {
- if (removed == true) return;
- health = 0;
- kill();
- removed = true;
- }
- }
- private function kill():void
- {
- this.removeEventListener(Event.ENTER_FRAME, collisionCheck);
- var randomKillFrame:int = Math.random()*killFrames.length;
- var obj:MovieClip = this.getChildAt(0) as MovieClip;
- obj.gotoAndPlay(killFrames[randomKillFrame]);
- }
- private function rotate(e:Event):void
- {
- this.rotation += rotationDegree;
- }
- private function calculatePath():void
- {
- pathLength = Math.sqrt(Math.pow(pathArray[2*pathPoint+1]-this.y, 2) + Math.pow(pathArray[2*pathPoint]-this.x, 2));
- xSpeed = (pathArray[2*pathPoint]-this.x)/pathLength*pathVelocity;
- ySpeed = (pathArray[2*pathPoint+1]-this.y)/pathLength*pathVelocity;
- pathAngle = Math.atan2(ySpeed, xSpeed);
- if (pathLength == 0)
- {
- pathPoint++;
- if (pathPoint == pathArrayLength)
- {
- pathPoint = 0;
- }
- calculatePath();
- }
- }
- private function followPath(e:Event):void
- {
- if (pathDelayCurrent != 0)
- {
- pathDelayCurrent--;
- }
- else
- {
- this.x += xSpeed;
- this.y += ySpeed;
- if (int(Math.atan2((pathArray[2*pathPoint+1]-this.y)/pathLength*pathVelocity, (pathArray[2*pathPoint]-this.x)/pathLength*pathVelocity)) != int(pathAngle))
- {
- this.x = pathArray[2*pathPoint];
- this.y = pathArray[2*pathPoint+1];
- }
- if (this.x == pathArray[2*pathPoint] && this.y == pathArray[2*pathPoint+1])
- {
- pathPoint++;
- pathDelayCurrent = pathDelay;
- if (pathPoint == pathArrayLength)
- {
- pathPoint = 0;
- }
- }
- calculatePath();
- }
- }
- private function animateParticles(e:Event)
- {
- for (var i:int = 0; i<particles.length; i++)
- {
- particles[i].update();
- particles[i].removeTimer--;
- if (particles[i].removeTimer == 0)
- {
- particles[i].destroy();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment