Advertisement
Uhfgood

Player class

Nov 1st, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 7.88 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxObject;
  5. import flixel.group.FlxTypedGroup;
  6. import flixel.system.FlxSound;
  7. import flixel.util.FlxColor;
  8.  
  9. #if debug_draw
  10. using flixel.util.FlxSpriteUtil;
  11. #end
  12.  
  13. /**
  14.  * ...
  15.  * @author Keith Weatherby II
  16.  */
  17.  
  18. enum MovementModes
  19. {
  20.     IDLE;
  21.     LEFT;
  22.     RIGHT;
  23.     JUMP;
  24.     FALL;
  25.     DO_JUMP;
  26.     DO_FALL;
  27. }
  28.  
  29. class Player extends GameObject
  30. {  
  31.     public var lives : Int = 3;
  32.        
  33.     public var actionCount : Int = 0;
  34.     public var actionTime : Int = 10;
  35.  
  36.     public var timeUntilRun : Int = 9;
  37.    
  38.     public var actions : Map<String, Bool> = new Map();
  39.     public var canDoHypnoAction : Bool = true;
  40.    
  41.     public var vMotion : MovementModes;
  42.     public var hMotion : MovementModes;
  43.     public var doAction : Bool = false;
  44.    
  45.     public var turnOnBolt : Bool = true;
  46.    
  47.     public var sounds : Map<String, FlxSound> = new Map();
  48.     public var bolt : GameObject;
  49.    
  50.     public var carriedEnemy : Enemy;
  51.    
  52.     public function new( gameLevel : PlayState, object : Dynamic )
  53.     {
  54.         super( gameLevel, object );
  55.        
  56.         actions[ "action" ] = false;
  57.         actions[ "jump" ] = false;
  58.         actions[ "fall" ] = false;
  59.         actions[ "left" ] = false;
  60.         actions[ "right" ] = false;
  61.        
  62.         hMotion = vMotion = IDLE;
  63.     }
  64.    
  65.     override public function update():Void
  66.     {
  67.         this.ProcessInput();
  68.         this.DoActions();
  69.         this.Animate();
  70.         super.update();    
  71.     }
  72.    
  73.     public function ProcessInput()
  74.     {
  75.         doAction = actions[ "action" ];
  76.        
  77.         if ( actions[ "left" ] )
  78.             hMotion = LEFT;
  79.         else
  80.         if ( actions[ "right" ] )
  81.             hMotion = RIGHT;
  82.         else
  83.             hMotion = IDLE;
  84.        
  85.         if ( actions[ "jump" ] )
  86.             vMotion = DO_JUMP;
  87.         else
  88.         if ( actions[ "fall" ] )       
  89.             vMotion = DO_FALL;
  90.         else
  91.         {
  92.             if( this.velocity.y > 0 )
  93.                 vMotion = FALL;
  94.             else
  95.             if( this.velocity.y < 0 )
  96.                 vMotion = JUMP;
  97.             else
  98.             if( this.velocity.y == 0 )
  99.                 vMotion = IDLE;
  100.         }      
  101.     }
  102.    
  103.     public function DoActions()
  104.     {      
  105.         this.acceleration.x = 0;
  106.         if ( doAction )
  107.         {
  108.             if ( canDoHypnoAction )
  109.             {
  110.                 // stop it from doing it more than one press
  111.                 canDoHypnoAction = false;
  112.                 TryHypnosis();
  113.                 TryLevitating();
  114.             }
  115.            
  116.             turnOnBolt = true;
  117.             actionCount++;
  118.             if ( actionCount > actionTime )
  119.                 turnOnBolt = false;
  120.             else
  121.             if ( actionCount > timeUntilRun )
  122.                 this.speed = this.startSpeed * 1.75;
  123.        
  124.             if( turnOnBolt )
  125.                 sounds[ "hypno001" ].play();
  126.         }
  127.         else
  128.         {
  129.             canDoHypnoAction = true;
  130.             actionCount = 0;
  131.             turnOnBolt = false;
  132.             this.speed = this.startSpeed;
  133.         }
  134.        
  135.         if ( hMotion == LEFT )
  136.             this.move( FlxObject.LEFT );
  137.         else
  138.         if ( hMotion == RIGHT )
  139.             this.move( FlxObject.RIGHT );
  140.         else
  141.         if( this.velocity.y == 0 )
  142.             this.move( FlxObject.NONE );
  143.        
  144.         if ( vMotion == DO_JUMP )
  145.         {
  146.             if ( onGround && isTouching( FlxObject.DOWN ) )
  147.             {
  148.                 this.velocity.y = -this.maxVelocity.y / 2;
  149.                 sounds[ "jump001" ].play();
  150.             }
  151.         }
  152.         else
  153.         if ( vMotion == DO_FALL )
  154.         {
  155.             var stillOnMap : Bool = false;
  156.             if ( level.gameMap != null )
  157.             {
  158.                 if ( y < level.gameMap.heightInPixels - level.gameMap.tileHeight )
  159.                     stillOnMap = true;
  160.             }
  161.            
  162.             if ( isTouching( FlxObject.DOWN ) && stillOnMap )
  163.             {
  164.                 if( level.gameMap.IsTouchingTile( this.x + ( this.width / 2 ), this.y + this.height, "passthrough" ) )
  165.                 {
  166.                     y += level.gameMap.tileHeight;
  167.                     sounds[ "fall001" ].play();
  168.                 }
  169.             }
  170.         }
  171.     }
  172.    
  173.     /*
  174.         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175.         #if debug_draw
  176.         var end = this.getMidpoint().x + dist;
  177.         var lineStyle : LineStyle = { color: FlxColor.RED, thickness: 1 };
  178.         Reg.debugCanvas.drawLine( this.getMidpoint().x, this.getMidpoint().y, end, this.getMidpoint().y, lineStyle);
  179.         #end
  180.         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181.     */
  182.     public function TryHypnosis()
  183.     {  
  184.         var dist = this.facing == FlxObject.LEFT ? -36 : 36;
  185.         var rayLength = 0;
  186.         var closestEnemy : Enemy = null;
  187.         var closestHit : Int = 640;
  188.        
  189.         for ( enemy in level.enemyGroup )
  190.         {
  191.             if ( enemy.hypnoMode == AWAKE )
  192.             {
  193.                 var isFacingEnemy : Bool =
  194.                     ( this.facing == FlxObject.LEFT && enemy.facing == FlxObject.RIGHT ) ||
  195.                         ( this.facing == FlxObject.RIGHT && enemy.facing == FlxObject.LEFT );
  196.  
  197.                 if ( isFacingEnemy )
  198.                 {
  199.                     rayLength = this.HorizontalRayHit( enemy, dist );
  200.                     if ( rayLength > 0 )
  201.                     {
  202.                         if ( rayLength <= closestHit )
  203.                         {
  204.                             closestHit = rayLength;
  205.                             closestEnemy = enemy;
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.        
  212.         if ( closestEnemy != null )
  213.         {
  214.             closestEnemy.SetHypnoMode( HYPNOTIZED );
  215.             closestEnemy.hypnoTimeCounter = 0;
  216.         }  
  217.     }
  218.    
  219.     public function TryLevitating()
  220.     {  
  221.         var dist = this.facing == FlxObject.LEFT ? -36 : 36;
  222.         var rayLength = 0;
  223.         var closestEnemy : Enemy = null;
  224.         var closestHit : Int = 640;
  225.        
  226.         for ( enemy in level.enemyGroup )
  227.         {
  228.             if ( enemy.hypnoMode == HYPNOTIZED )
  229.             {
  230.                 rayLength = this.HorizontalRayHit( enemy, dist );
  231.                 if ( rayLength > 0 )
  232.                 {
  233.                     if ( rayLength <= closestHit )
  234.                     {
  235.                         closestHit = rayLength;
  236.                         closestEnemy = enemy;
  237.                     }
  238.                 }
  239.             }
  240.         }
  241.        
  242.         if ( closestEnemy != null )
  243.         {
  244.             closestEnemy.SetHypnoMode( LEVITATE );
  245.             carriedEnemy = closestEnemy;
  246.         }  
  247.     }
  248.    
  249.     /*
  250.         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  251.         #if debug_draw
  252.         if ( closestEnemy != null )
  253.         {
  254.             var end = dist < 0 ? ( this.getMidpoint().x - closestHit ) : ( this.getMidpoint().x + closestHit );
  255.             var lineStyle : LineStyle = { color: FlxColor.GREEN, thickness: 1 };
  256.             Reg.debugCanvas.drawLine( this.getMidpoint().x, this.getMidpoint().y, end, this.getMidpoint().y, lineStyle);
  257.         }
  258.         #end
  259.         ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  260.     */
  261.     public function ControlPlayer( action : Bool, LEFT : Bool, RIGHT : Bool, JUMP : Bool, FALL : Bool )
  262.     {
  263.         actions[ "action" ] = action;
  264.         actions[ "left" ] = LEFT;
  265.         actions[ "right" ] = RIGHT;
  266.         actions[ "jump" ] = JUMP;
  267.         actions[ "fall" ] = FALL;  
  268.     }
  269.        
  270.     public function Animate()
  271.     {  
  272.         this.bolt.visible = this.turnOnBolt;
  273.  
  274.         if ( this.hMotion == LEFT || this.hMotion == RIGHT )
  275.             this.animation.play( "walking" );
  276.         else
  277.             this.animation.play( "idle_01" );
  278.        
  279.         if ( this.doAction )
  280.         {
  281.             if( this.actionCount > this.timeUntilRun )
  282.                 if( this.hMotion != IDLE )
  283.                     this.animation.play( "running" );
  284.                
  285.             if ( this.facing == FlxObject.RIGHT )
  286.             {
  287.                 this.bolt.x = this.x + this.width;
  288.                 this.bolt.offset.x = 0;
  289.             }
  290.             else
  291.                 this.bolt.x = x - this.bolt.width;
  292.                
  293.             this.bolt.y = this.y + ( ( this.height - this.bolt.height ) / 4 );
  294.         }
  295.                
  296.         if ( this.vMotion == DO_JUMP || this.vMotion == JUMP )
  297.             this.animation.play( "jumping" );
  298.         else
  299.         if ( this.vMotion == FALL )
  300.             this.animation.play( "dropping" );
  301.     }
  302.    
  303.     public function CommitSuicide() : Void
  304.     {
  305.         if ( lives > 0 )
  306.         {
  307.             lives--;
  308.             level.UpdateHud();
  309.             level.reset = true;
  310.         }
  311.         else
  312.         {
  313.             level.SetGameOver( true );
  314.         }
  315.  
  316.     }
  317.  
  318.     public function AddExtraLife() : Void
  319.     {
  320.         lives++;
  321.         level.UpdateHud();
  322.     }
  323.    
  324.     public function LoadSounds( soundList : String )
  325.     {
  326.         var ext : String = "ogg";
  327.        
  328.         #if flash
  329.             ext = "mp3";
  330.         #end
  331.        
  332.         // create an empty token array, this will store each sound name
  333.         var tokens : Array< String > = [];
  334.         tokens = soundList.split( ", " );
  335.        
  336.         for ( token in tokens )
  337.         {
  338.             sounds[ token ] = FlxG.sound.load( "assets/sounds/" + token + "." + ext );
  339.         }
  340.        
  341.     }
  342.    
  343.     public function CreateBolt( boltData : Dynamic )
  344.     {
  345.         if ( level == null ) return;
  346.        
  347.         bolt = new GameObject( level, boltData );
  348.         bolt.set_visible( false );
  349.         bolt.x = this.x + this.width;
  350.         bolt.y = this.y + ( ( this.height - bolt.height ) / 4 );       
  351.     }
  352.    
  353.     public override function Regen()
  354.     {
  355.         super.Regen();
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement