Advertisement
nutter666

Black Spider Enemy

Sep 9th, 2012
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  {
  2.    
  3.     import flash.display.MovieClip;
  4.     import flash.geom.Point;
  5.     import flash.display.Sprite;
  6.     import flash.utils.Timer;
  7.     import flash.events.TimerEvent;
  8.    
  9.     public class BlackSpider extends MovieClip{
  10.        
  11.         var pointsArray:Array = [];
  12.         var currentPoint:int = 0;
  13.         public var parentMC:Object;
  14.        
  15.         public var moveSpeed:Number = 0.5;
  16.         public var maxHP:int = 15;
  17.         public var currentHP:int;
  18.         public var currentHealthPerc:int
  19.        
  20.         var hpSprite:Sprite = new Sprite();
  21.         var hpBarWidth = 25
  22.        
  23.         var wayPointDistance:int = 5;
  24.        
  25.         var canMove:Boolean = false;
  26.         var stunned:Boolean = false;
  27.         var slowed:Boolean = false;
  28.         var slowAmount:Number = 1;
  29.         // a number that controls how much slower the enemy moves, 0.5 = 50% slower.
  30.        
  31.         var stunnedTimer:Timer;
  32.         var slowedTimer:Timer;
  33.        
  34.         var canBeSlowed:Boolean = true;
  35.         var canBeStunned:Boolean = true;
  36.         var isFlying:Boolean = false;
  37.        
  38.         var targetPoint:Point;
  39.        
  40.         var reachedEnd:Boolean = false;
  41.         var alive:Boolean;
  42.  
  43.         public function BlackSpider() {
  44.             // constructor code
  45.         addChild(hpSprite)
  46.         hpSprite.x = -hpBarWidth/2
  47.         }
  48.        
  49.         public function init(wayPoints:Array,waveHealthBonus:int){
  50.         pointsArray = wayPoints
  51.         getNextTargetPoint();
  52.         canMove = true;
  53.         maxHP += waveHealthBonus
  54.         // adds the extra health based on wave number, i.e later waves have more health
  55.         currentHP = maxHP
  56.         alive = true;
  57.         }
  58.        
  59.         public function moveEnemy(){
  60.         currentHealthPerc = ((currentHP/maxHP)* 100)
  61.         drawHealthBar();
  62.            
  63.         if(!stunned && canMove && !reachedEnd){
  64.        
  65.         var tempSpeed:Number = moveSpeed;
  66.         var xDistance:Number = targetPoint.x - this.x;
  67.         var yDistance:Number = targetPoint.y - this.y;
  68.        
  69.         var radian:Number = Math.atan2(yDistance, xDistance);
  70.         this.enemyMC.rotation = int(radian*360/(Math.PI*2));
  71.         if(!slowed){
  72.         x += Math.cos(radian) * moveSpeed;
  73.         y += Math.sin(radian) * moveSpeed;
  74.         }
  75.         else if(slowed){
  76.         x += Math.cos(radian) * (moveSpeed*slowAmount)
  77.         y += Math.sin(radian) * (moveSpeed*slowAmount)
  78.         }
  79.        
  80.         if(getDist(this,targetPoint) <= wayPointDistance){
  81.         if(currentPoint < pointsArray.length){
  82.         currentPoint++
  83.         getNextTargetPoint();
  84.         }
  85.         else {
  86.         // has reached end of the path
  87.         reachedEnd = true;
  88.         }
  89.         }
  90.         }      
  91.  
  92.         }
  93.        
  94.         public function stun(stunTime:int){
  95.         if(!stunned){
  96.         stunned = true;
  97.         stunnedTimer = new Timer(stunTime,1)
  98.         stunnedTimer.start();
  99.         stunnedTimer.addEventListener(TimerEvent.TIMER_COMPLETE,unStun)
  100.         this.enemyMC.gotoAndStop("stunned");
  101.         }
  102.         }
  103.        
  104.         function unStun(t:TimerEvent){
  105.         stunned = false;
  106.         stunnedTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,unStun)
  107.         this.enemyMC.gotoAndStop(1);
  108.         }
  109.        
  110.         public function slow(slowTime:int,slowAmt:Number){
  111.         if(!slowed){
  112.         slowed = true;
  113.         slowedTimer = new Timer(slowTime,1)
  114.         slowedTimer.start();
  115.         slowAmount = slowAmt;
  116.         slowedTimer.addEventListener(TimerEvent.TIMER_COMPLETE,unSlow)
  117.         this.enemyMC.gotoAndStop("slowed");
  118.         }
  119.         }
  120.        
  121.         function unSlow(t:TimerEvent){
  122.         slowed = false;
  123.         slowedTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,unSlow)
  124.         this.enemyMC.gotoAndStop(1);
  125.         }
  126.        
  127.         function getNextTargetPoint(){
  128.         if(currentPoint <= pointsArray.length-1){
  129.         // move to point in array
  130.         targetPoint = new Point(pointsArray[currentPoint].x,pointsArray[currentPoint].y)
  131.         }
  132.         else {
  133.         // move to end point
  134.         targetPoint = new Point(parentMC.endPoint.x,parentMC.endPoint.y)
  135.         }
  136.         }
  137.        
  138.        
  139.         public function takeDamage(amount:Number){
  140.         currentHP -= amount
  141.         if(currentHP <= 0){
  142.         alive = false;
  143.         canMove = false;
  144.         }
  145.         }
  146.        
  147.         function getDist(firstObject:Object,secondObject:Object){
  148.         var distance:Number = Math.sqrt( ( firstObject.x - secondObject.x ) * ( firstObject.x - secondObject.x ) + ( firstObject.y - secondObject.y ) * ( firstObject.y - secondObject.y ) );
  149.         return distance;
  150.         }
  151.        
  152.         function drawHealthBar(){
  153.         var greenWidth = (currentHealthPerc/100)* hpBarWidth
  154.         var redWidth = ((100-currentHealthPerc)/100)* hpBarWidth
  155.         hpSprite.graphics.clear();
  156.         hpSprite.graphics.beginFill(0x00FF17)
  157.         hpSprite.graphics.drawRect(0,-12,greenWidth,4)
  158.         hpSprite.graphics.beginFill(0xFF0000)
  159.         hpSprite.graphics.drawRect(greenWidth,-12,redWidth,4)
  160.         }
  161.        
  162.  
  163.     }
  164.    
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement