Advertisement
vredfox

EntityPlayer.as

Dec 6th, 2018
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lib.shoot.entity
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.TimerEvent;
  5.     import flash.utils.Timer;
  6.     import flash.ui.Keyboard;
  7.     import flash.events.Event;
  8.    
  9.     import fl.motion.Color;
  10.    
  11.     import lib.shoot.InputManager;
  12.     import lib.shoot.MathHelper;
  13.     import lib.shoot.Quadrilateral;
  14.     import lib.shoot.entity.states.PlayerStateGround;
  15.     import lib.shoot.entity.states.PlayerStateDead;
  16.     import lib.shoot.entity.states.PlayerStateDodge;
  17.  
  18.     public class EntityPlayer extends Entity
  19.     {
  20.         private var invulnTimer:Timer;
  21.         private var dodgeTimer:Timer;
  22.        
  23.         private var canDodge:Boolean;
  24.        
  25.         public function EntityPlayer()
  26.         {
  27.             this.healthMax = 3;
  28.             this.health = 3;
  29.            
  30.             this.canDodge = true;
  31.            
  32.             this.abilities.push(Ability.STOMP);
  33.             this.abilities.push(Ability.BOUNCEBACK);
  34.            
  35.             this.setState(new PlayerStateGround());
  36.         }
  37.        
  38.         public override function update():void
  39.         {
  40.             // Noclip movement
  41.             if(!this.physicsEnabled && !this.isDead)
  42.             {
  43.                 if(InputManager.isKeyDown(Keyboard.LEFT))
  44.                 {
  45.                     this.posX -= 3;
  46.                 }
  47.                 if(InputManager.isKeyDown(Keyboard.RIGHT))
  48.                 {
  49.                     this.posX += 3;
  50.                 }
  51.                
  52.                 if(InputManager.isKeyDown(Keyboard.UP))
  53.                 {
  54.                     this.posY -= 3;
  55.                 }
  56.                 if(InputManager.isKeyDown(Keyboard.DOWN))
  57.                 {
  58.                     this.posY += 3;
  59.                 }
  60.             }
  61.            
  62.             super.update();
  63.         }
  64.        
  65.         public override function getBoundingBox():Quadrilateral
  66.         {
  67.             return this.boundingBox.fromPositionAndSize(this.posX - (this.direction == -1 ? 38 : 0), this.posY, 38, 50);
  68.         }
  69.        
  70.         public override function getBoundingBoxPrev():Quadrilateral
  71.         {
  72.             return this.boundingBoxPrev.fromPositionAndSize(this.posXPrev - (this.direction == -1 ? 38 : 0), this.posYPrev, 38, 50);
  73.         }
  74.        
  75.         public override function onAttacked(attacker:Entity):void
  76.         {
  77.             if(!this.isInvulnerable)
  78.             {
  79.                 this.health--;
  80.                
  81.                 this.setState(new PlayerStateDodge());
  82.             }
  83.            
  84.             if(this.health <= 0)
  85.             {
  86.                 this.setState(new PlayerStateDead());
  87.             }
  88.         }
  89.        
  90.         public override function setInvulnverable(invulnerable:Boolean):void
  91.         {
  92.             if(!this.isInvulnerable)
  93.             {
  94.                 this.isInvulnerable = invulnerable;
  95.            
  96.                 var invulnTimeSeconds:Number = 1;
  97.                 var invlinTicksPerSecond:Number = 64;
  98.                
  99.                 this.invulnTimer = new Timer((invulnTimeSeconds / invlinTicksPerSecond) * 1000, invulnTimeSeconds * invlinTicksPerSecond);
  100.                 this.invulnTimer.addEventListener(TimerEvent.TIMER, onInvulnTimerTick);
  101.                 this.invulnTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onInvulnTimerEnd);
  102.                 this.onInvulnTimerTick(null);
  103.                 this.invulnTimer.start();
  104.             }
  105.         }
  106.        
  107.         private function onInvulnTimerTick(evt:TimerEvent):void
  108.         {
  109.             if(this.invulnTimer.running)
  110.             {
  111.                 if(this.alpha < 1)
  112.                 {
  113.                     this.alpha = 1;
  114.                 }
  115.                 else
  116.                 {
  117.                     this.alpha = 0.25;
  118.                 }
  119.             }
  120.         }
  121.        
  122.         private function onInvulnTimerEnd(evt:TimerEvent):void
  123.         {
  124.             this.isInvulnerable = false;
  125.            
  126.             this.transform.colorTransform.alphaMultiplier = 1;
  127.         }
  128.        
  129.         public function onDodge():void
  130.         {
  131.             if(!this.onGround)
  132.             {
  133.                 this.canDodge = false;
  134.        
  135.                 var dodgeTimeSeconds:Number = 1;
  136.                 var dodgeTicksPerSecond:Number = 1;
  137.            
  138.                 this.dodgeTimer = new Timer((dodgeTimeSeconds / dodgeTicksPerSecond) * 1000, dodgeTimeSeconds * dodgeTicksPerSecond);
  139.                 this.dodgeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onDodgeTimerEnd);
  140.                 this.dodgeTimer.start();
  141.             }
  142.         }
  143.        
  144.         private function onDodgeTimerEnd(evt:TimerEvent):void
  145.         {
  146.             this.canDodge = true;
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement