Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.MovieClip;
- import flash.geom.Point;
- import flash.display.Sprite;
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- public class BlackSpider extends MovieClip{
- var pointsArray:Array = [];
- var currentPoint:int = 0;
- public var parentMC:Object;
- public var moveSpeed:Number = 0.5;
- public var maxHP:int = 15;
- public var currentHP:int;
- public var currentHealthPerc:int
- var hpSprite:Sprite = new Sprite();
- var hpBarWidth = 25
- var wayPointDistance:int = 5;
- var canMove:Boolean = false;
- var stunned:Boolean = false;
- var slowed:Boolean = false;
- var slowAmount:Number = 1;
- // a number that controls how much slower the enemy moves, 0.5 = 50% slower.
- var stunnedTimer:Timer;
- var slowedTimer:Timer;
- var canBeSlowed:Boolean = true;
- var canBeStunned:Boolean = true;
- var isFlying:Boolean = false;
- var targetPoint:Point;
- var reachedEnd:Boolean = false;
- var alive:Boolean;
- public function BlackSpider() {
- // constructor code
- addChild(hpSprite)
- hpSprite.x = -hpBarWidth/2
- }
- public function init(wayPoints:Array,waveHealthBonus:int){
- pointsArray = wayPoints
- getNextTargetPoint();
- canMove = true;
- maxHP += waveHealthBonus
- // adds the extra health based on wave number, i.e later waves have more health
- currentHP = maxHP
- alive = true;
- }
- public function moveEnemy(){
- currentHealthPerc = ((currentHP/maxHP)* 100)
- drawHealthBar();
- if(!stunned && canMove && !reachedEnd){
- var tempSpeed:Number = moveSpeed;
- var xDistance:Number = targetPoint.x - this.x;
- var yDistance:Number = targetPoint.y - this.y;
- var radian:Number = Math.atan2(yDistance, xDistance);
- this.enemyMC.rotation = int(radian*360/(Math.PI*2));
- if(!slowed){
- x += Math.cos(radian) * moveSpeed;
- y += Math.sin(radian) * moveSpeed;
- }
- else if(slowed){
- x += Math.cos(radian) * (moveSpeed*slowAmount)
- y += Math.sin(radian) * (moveSpeed*slowAmount)
- }
- if(getDist(this,targetPoint) <= wayPointDistance){
- if(currentPoint < pointsArray.length){
- currentPoint++
- getNextTargetPoint();
- }
- else {
- // has reached end of the path
- reachedEnd = true;
- }
- }
- }
- }
- public function stun(stunTime:int){
- if(!stunned){
- stunned = true;
- stunnedTimer = new Timer(stunTime,1)
- stunnedTimer.start();
- stunnedTimer.addEventListener(TimerEvent.TIMER_COMPLETE,unStun)
- this.enemyMC.gotoAndStop("stunned");
- }
- }
- function unStun(t:TimerEvent){
- stunned = false;
- stunnedTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,unStun)
- this.enemyMC.gotoAndStop(1);
- }
- public function slow(slowTime:int,slowAmt:Number){
- if(!slowed){
- slowed = true;
- slowedTimer = new Timer(slowTime,1)
- slowedTimer.start();
- slowAmount = slowAmt;
- slowedTimer.addEventListener(TimerEvent.TIMER_COMPLETE,unSlow)
- this.enemyMC.gotoAndStop("slowed");
- }
- }
- function unSlow(t:TimerEvent){
- slowed = false;
- slowedTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,unSlow)
- this.enemyMC.gotoAndStop(1);
- }
- function getNextTargetPoint(){
- if(currentPoint <= pointsArray.length-1){
- // move to point in array
- targetPoint = new Point(pointsArray[currentPoint].x,pointsArray[currentPoint].y)
- }
- else {
- // move to end point
- targetPoint = new Point(parentMC.endPoint.x,parentMC.endPoint.y)
- }
- }
- public function takeDamage(amount:Number){
- currentHP -= amount
- if(currentHP <= 0){
- alive = false;
- canMove = false;
- }
- }
- function getDist(firstObject:Object,secondObject:Object){
- var distance:Number = Math.sqrt( ( firstObject.x - secondObject.x ) * ( firstObject.x - secondObject.x ) + ( firstObject.y - secondObject.y ) * ( firstObject.y - secondObject.y ) );
- return distance;
- }
- function drawHealthBar(){
- var greenWidth = (currentHealthPerc/100)* hpBarWidth
- var redWidth = ((100-currentHealthPerc)/100)* hpBarWidth
- hpSprite.graphics.clear();
- hpSprite.graphics.beginFill(0x00FF17)
- hpSprite.graphics.drawRect(0,-12,greenWidth,4)
- hpSprite.graphics.beginFill(0xFF0000)
- hpSprite.graphics.drawRect(greenWidth,-12,redWidth,4)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement