Guest User

Untitled

a guest
Nov 29th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.    
  4.     import flash.display.MovieClip;
  5.     import flash.events.Event;
  6.     import flash.display.Sprite;
  7.     import flash.display.Graphics;
  8.     import flash.utils.getDefinitionByName;
  9.     import flash.filters.GlowFilter;
  10.     import flash.utils.getTimer;
  11.     import flash.geom.Point;
  12.     import flash.utils.Timer;
  13.    
  14.     public class LevelObject extends MovieClip
  15.     {
  16.         private var collisionList:CollisionList;
  17.         private var removed:Boolean = false;
  18.        
  19.         private var objectType:Object;
  20.         private var objectSkin:MovieClip;
  21.         private var rotationDegree:int;
  22.         private var pathVelocity:int;
  23.         private var pathArray:Array;
  24.         private var xSpeed:Number;
  25.         private var ySpeed:Number;
  26.         private var pathAngle:Number;
  27.         private var pathPoint:int = 0;
  28.         private var pathArrayLength:int;
  29.         private var pathLength:Number;
  30.         private var posX:int;
  31.         private var posY:int;
  32.         private var pathDelay:int;
  33.         private var pathDelayCurrent:int;
  34.        
  35.         //Properties
  36.         private var healthMax:int;
  37.         private var health:int;
  38.         private var glow:GlowFilter;
  39.         private var rotationAngle:int;
  40.         private var killFrames:Array = [];
  41.        
  42.         //Sparks
  43.         private var particle:Particle;
  44.         private var particles:Array = [];
  45.         private var particleSpeed:int;
  46.         private var angleOffset:Number;
  47.        
  48.         public function LevelObject(type, pX, pY, rDeg, pVelocity, pDelay, pArray)
  49.         {
  50.             objectType = type;
  51.             rotationDegree = rDeg;
  52.             pathVelocity = pVelocity;
  53.             pathArray = pArray;
  54.             pathArrayLength = pathArray.length/2;
  55.             posX = pX;
  56.             posY = pY;
  57.             pathDelay = pDelay;
  58.             pathDelayCurrent = pDelay;
  59.             this.x = posX;
  60.             this.y = posY;
  61.             addSkin();
  62.             addProperties();
  63.             collisionList = new CollisionList(this, Ball.getBall());
  64.             this.addEventListener(Event.ENTER_FRAME, collisionCheck)
  65.         }
  66.        
  67.         private function addSkin():void
  68.         {
  69.             objectSkin = new (objectType);
  70.             this.addChild(objectSkin);
  71.             this.removeChildAt(0);
  72.         }
  73.        
  74.         private function addProperties():void
  75.         {
  76.             switch(objectType)
  77.             {
  78.                 case(RectNormal):
  79.                
  80.                 healthMax = 10;
  81.                
  82.                 glow = new GlowFilter();
  83.                 glow.color = 0x3864e0;
  84.                 glow.quality = 2;
  85.                 glow.blurX = 5;
  86.                 glow.blurY = 5;
  87.                 this.filters = [glow];
  88.                
  89.                 killFrames = [2, 8, 14, 21];
  90.                
  91.                 break;
  92.                
  93.                 case(RectHard):
  94.                 healthMax = 3;
  95.                 glow = new GlowFilter();
  96.                 glow.color = 0xFF0000;
  97.                 glow.quality = 2;
  98.                 glow.blurX = 5;
  99.                 glow.blurY = 5;
  100.                 this.filters = [glow];
  101.                 break;
  102.                
  103.                 case(RoundHard):
  104.                 healthMax = 3;
  105.                 break;
  106.             }
  107.                 health = healthMax;
  108.                 if (rotationDegree != 0)
  109.                 {
  110.                     this.addEventListener(Event.ENTER_FRAME, rotate)
  111.                 }
  112.                 if (pathArray[0])
  113.                 {
  114.                     calculatePath();
  115.                     this.addEventListener(Event.ENTER_FRAME, followPath)
  116.                 }
  117.         }
  118.        
  119.         private function collisionCheck(e:Event):void
  120.         {
  121.                 if (collisionList.checkCollisions().length)
  122.                 {
  123.                     var collision = collisionList.checkCollisions()[0];
  124.                     var overlap:int = collision.overlapping.length;
  125.                     var sin:Number = Math.sin(collision.angle);
  126.                     var cos:Number = Math.cos(collision.angle);
  127.                     var vx0:Number = Math.cos(Ball._angle) * cos + Math.sin(Ball._angle) * sin;
  128.                     var vy0:Number = Math.sin(Ball._angle) * cos - Math.cos(Ball._angle) * sin;
  129.                     var vx = vx0 * cos - vy0 * sin;
  130.                     var vy = vy0 * cos + vx0 * sin;
  131.                     vx -= cos * overlap / (Ball.getBall().width*0.5);
  132.                     vy -= sin * overlap / (Ball.getBall().width*0.5);
  133.                     Ball._angle = Math.atan2(vy,vx);
  134.                     damageLevelObject(1);
  135.                 }
  136.         }
  137.        
  138.         private function damageLevelObject(damage):void
  139.         {
  140.             health -= damage;
  141.             for (var j:int = 0; j<5; j++)
  142.             {
  143.             particle = new Particle(Spark, stage, Ball.getBall().x, Ball.getBall().y, glow);
  144.             angleOffset = Math.random()*(Math.PI/2)-(Math.PI/4);
  145.             particleSpeed = Math.random()*7+1;
  146.             particle.removeTimer = 5;
  147.             particle.xSpeed = Math.cos(Ball._angle+angleOffset)*particleSpeed;
  148.             particle.ySpeed = Math.sin(Ball._angle+angleOffset)*particleSpeed;
  149.             particle.rotationDegree = (Ball._angle+angleOffset)/Math.PI*180;
  150.             particles.push(particle);
  151.             }
  152.             addEventListener(Event.ENTER_FRAME, animateParticles)
  153.             if(health <= 0 )
  154.             {
  155.                 if (removed == true) return;
  156.                 health = 0;
  157.                 kill();
  158.                 removed = true;
  159.             }
  160.         }
  161.        
  162.         private function kill():void
  163.         {
  164.             this.removeEventListener(Event.ENTER_FRAME, collisionCheck);
  165.             var randomKillFrame:int = Math.random()*killFrames.length;
  166.             var obj:MovieClip = this.getChildAt(0) as MovieClip;
  167.             obj.gotoAndPlay(killFrames[randomKillFrame]);
  168.         }
  169.  
  170.         private function rotate(e:Event):void
  171.         {
  172.             this.rotation += rotationDegree;
  173.         }
  174.        
  175.         private function calculatePath():void
  176.         {
  177.             pathLength = Math.sqrt(Math.pow(pathArray[2*pathPoint+1]-this.y, 2) + Math.pow(pathArray[2*pathPoint]-this.x, 2));
  178.             xSpeed = (pathArray[2*pathPoint]-this.x)/pathLength*pathVelocity;
  179.             ySpeed = (pathArray[2*pathPoint+1]-this.y)/pathLength*pathVelocity;
  180.             pathAngle = Math.atan2(ySpeed, xSpeed);
  181.             if (pathLength == 0)
  182.             {
  183.                 pathPoint++;
  184.                 if (pathPoint == pathArrayLength)
  185.                 {
  186.                     pathPoint = 0;
  187.                 }
  188.                 calculatePath();
  189.             }
  190.         }
  191.        
  192.         private function followPath(e:Event):void
  193.         {
  194.             if (pathDelayCurrent != 0)
  195.             {
  196.                 pathDelayCurrent--;
  197.             }
  198.             else
  199.             {
  200.                 this.x += xSpeed;
  201.                 this.y += ySpeed;
  202.                 if (int(Math.atan2((pathArray[2*pathPoint+1]-this.y)/pathLength*pathVelocity, (pathArray[2*pathPoint]-this.x)/pathLength*pathVelocity)) != int(pathAngle))
  203.                 {
  204.                     this.x = pathArray[2*pathPoint];
  205.                     this.y = pathArray[2*pathPoint+1];
  206.                 }
  207.                 if (this.x == pathArray[2*pathPoint] && this.y == pathArray[2*pathPoint+1])
  208.                 {
  209.                     pathPoint++;
  210.                     pathDelayCurrent = pathDelay;
  211.                     if (pathPoint == pathArrayLength)
  212.                     {
  213.                         pathPoint = 0;
  214.                     }
  215.                 }
  216.                 calculatePath();
  217.             }
  218.         }
  219.        
  220.         private function animateParticles(e:Event)
  221.         {
  222.             for (var i:int = 0; i<particles.length; i++)
  223.             {
  224.                 particles[i].update();
  225.                 particles[i].removeTimer--;
  226.                 if (particles[i].removeTimer == 0)
  227.                 {
  228.                     particles[i].destroy();
  229.                 }
  230.             }
  231.         }
  232.  
  233.     }
  234.    
  235. }
Add Comment
Please, Sign In to add comment