Advertisement
davehale23

ladderHero.as

Jun 24th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import Box2D.Collision.b2Manifold;
  4.     import Box2D.Common.Math.b2Vec2;
  5.     import Box2D.Dynamics.b2Body;
  6.     import Box2D.Dynamics.Contacts.b2Contact;
  7.     import citrus.input.controllers.Keyboard;
  8.     import citrus.objects.platformer.box2d.Hero;
  9.     import citrus.objects.platformer.box2d.Platform;
  10.     import citrus.objects.platformer.box2d.Sensor;
  11.    
  12.     /**
  13.      * ...
  14.      * @author david.hale
  15.      */
  16.     public class ladderHero extends Hero
  17.     {
  18.                
  19.         public var canClimb:Boolean = false;
  20.         public var climbVelocity:Number = 7;
  21.         public var ladder:Sensor;
  22.         protected var _onLadder:Boolean = false;
  23.         protected var _climbing:Boolean = false;
  24.         protected var _climbAnimation:String = "ladderIdle";
  25.        
  26.         private var heroCanClimbLadders:Boolean = false;
  27.        
  28.         public function ladderHero(name:String, params:Object=null)
  29.         {
  30.             super(name, params);           
  31.         }
  32.            
  33.         override public function update(timeDelta:Number):void
  34.         {
  35.            
  36.             super.update(timeDelta);
  37.            
  38.             // we get a reference to the actual velocity vector
  39.             var velocity:b2Vec2 = _body.GetLinearVelocity();
  40.            
  41.             if (controlsEnabled)
  42.             {
  43.                 var moveKeyPressed:Boolean = false;
  44.                
  45.                 _ducking = (_ce.input.isDoing("duck", inputChannel) && _onGround && canDuck);
  46.                
  47.                 if (_ce.input.isDoing("right", inputChannel) && !_ducking)
  48.                 {                  
  49.                     velocity.Add(getSlopeBasedMoveAngle());
  50.                     moveKeyPressed = true;
  51.                 }
  52.                
  53.                 if (_ce.input.isDoing("left", inputChannel) && !_ducking)
  54.                 {                  
  55.                     velocity.Subtract(getSlopeBasedMoveAngle());
  56.                     moveKeyPressed = true;
  57.                 }
  58.                
  59.                
  60.                 if(heroCanClimbLadders){
  61.                     //ladder stuff             
  62.                     _onLadder = (canClimb);
  63.      
  64.                     if (_onLadder && !_onGround) {
  65.                         if (x < ladder.x)
  66.                             x++;
  67.                         if (x > ladder.x)
  68.                             x--;
  69.                     }
  70.                                        
  71.                     if (_ce.input.isDoing("up", inputChannel) && canClimb) {                       
  72.                         _onLadder = _climbing = true;
  73.                         _climbAnimation = "ladderClimbUp";                     
  74.                         velocity.y = -climbVelocity;
  75.                         velocity.x = 0;
  76.                         moveKeyPressed = true;
  77.                     }else if (_ce.input.isDoing("down", inputChannel) && canClimb) {                       
  78.                         _onLadder = _climbing = true;
  79.                         _climbAnimation = "ladderClimbDown";
  80.                         velocity.y = climbVelocity;
  81.                         velocity.x = 0;
  82.                         moveKeyPressed = true;
  83.                     }else if (_climbing && !_onGround) {                       
  84.                         velocity.y = -_friction;        //if you don't counteract the _friction, the Hero will slowly fall down the ladder.
  85.                     }
  86.      
  87.                     // Remove velocity if just stop climbing
  88.                     if ((_ce.input.justDid("up", inputChannel) || _ce.input.justDid("down", inputChannel)) && canClimb) {                      
  89.                         _climbAnimation = "ladderIdle";
  90.                         velocity.y = 0;
  91.                     }
  92.                    
  93.                     if (canClimb && _ce.input.justDid("jump", inputChannel)) {                     
  94.                         _climbing = false;
  95.                         velocity.y = -jumpHeight;
  96.                         onJump.dispatch();
  97.                     }
  98.                     //end ladder
  99.                 }
  100.                
  101.                 //If player just started moving the hero this tick.
  102.                 if (moveKeyPressed && !_playerMovingHero)
  103.                 {
  104.                     _playerMovingHero = true;
  105.                     _fixture.SetFriction(0); //Take away friction so he can accelerate.
  106.                 }
  107.                 //Player just stopped moving the hero this tick.
  108.                 else if (!moveKeyPressed && _playerMovingHero)
  109.                 {
  110.                     _playerMovingHero = false;
  111.                     _fixture.SetFriction(_friction); //Add friction so that he stops running
  112.                 }
  113.                
  114.                 if (_onGround && _ce.input.justDid("jump", inputChannel) && !_ducking)
  115.                 {
  116.                     velocity.y = -jumpHeight;
  117.                     onJump.dispatch();
  118.                 }
  119.                
  120.                 if (_ce.input.isDoing("jump", inputChannel) && !_onGround && velocity.y < 0)
  121.                 {
  122.                     velocity.y -= jumpAcceleration;
  123.                 }
  124.                
  125.                 if (_springOffEnemy != -1)
  126.                 {
  127.                     if (_ce.input.isDoing("jump", inputChannel))
  128.                         velocity.y = -enemySpringJumpHeight;
  129.                     else
  130.                         velocity.y = -enemySpringHeight;
  131.                     _springOffEnemy = -1;
  132.                 }
  133.                
  134.                 //Cap running velocities
  135.                 if (velocity.x > (maxVelocity))
  136.                     velocity.x = maxVelocity;
  137.                 else if (velocity.x < (-maxVelocity))
  138.                     velocity.x = -maxVelocity;
  139.                
  140.                 //set max ladder velocity
  141.                 if (velocity.y > (climbVelocity))
  142.                     velocity.y = climbVelocity;
  143.                 else if (velocity.y < (-climbVelocity))
  144.                     velocity.y = -climbVelocity;
  145.                
  146.             }
  147.            
  148.             updateAnimation();
  149.            
  150.         }
  151.        
  152.        
  153.         public function setHeroCanClimbLadders(_heroClimbs:Boolean):void
  154.         {          
  155.             heroCanClimbLadders = _heroClimbs;         
  156.         }
  157.        
  158.         override public function handlePreSolve(contact:b2Contact, oldManifold:b2Manifold):void
  159.         {
  160.             if(heroCanClimbLadders){
  161.                 //ladder handling
  162.                 var colliderBody:b2Body;
  163.                 //get which one is the Platform
  164.                 if (contact.GetFixtureA().GetBody().GetUserData() is Platform)
  165.                     colliderBody = contact.GetFixtureA().GetBody();
  166.                 else
  167.                     colliderBody = contact.GetFixtureB().GetBody();
  168.                    
  169.                 //this disables contact with any platform that the hero comes in contact with so that he can get up the ladder and onto the platform   
  170.                 if (colliderBody.GetUserData() is Platform && canClimb && (_climbing || !_onGround)) {
  171.                     if (((Platform)(colliderBody.GetUserData()).y + (Platform)(colliderBody.GetUserData()).height / 2) < (ladder.y + ladder.height / 2)) {
  172.                                        
  173.                         contact.SetEnabled(false);      //this turns off the collision for the platform when we are crossing it vertically on the ladder
  174.                     }
  175.                 }
  176.             }          
  177.            
  178.             super.handlePreSolve(contact, oldManifold);
  179.         }
  180.        
  181.         override public function handleBeginContact(contact:b2Contact):void
  182.         {          
  183.             if(heroCanClimbLadders){
  184.                 var ladderBody:b2Body = returnBodyIfLadder(contact);
  185.                    
  186.                 //yes, one of the two fixtures was indeed a ladder
  187.                 if (ladderBody != null) {
  188.                     canClimb = true;
  189.                     canDuck = false;
  190.                     ladder = ladderBody.GetUserData();
  191.                 }
  192.             }
  193.  
  194.             super.handleBeginContact(contact);
  195.         }
  196.                
  197.         override public function handleEndContact(contact:b2Contact):void
  198.         {
  199.             super.handleEndContact(contact);
  200.            
  201.             if(heroCanClimbLadders){
  202.                 var ladderBody:b2Body = returnBodyIfLadder(contact);
  203.                    
  204.                 if(ladderBody != null){
  205.                     _climbing = canClimb = false;
  206.                     canDuck = true;
  207.                 }
  208.             }
  209.            
  210.         }
  211.        
  212.         override protected function updateAnimation():void
  213.         {
  214.            
  215.             var prevAnimation:String = _animation;
  216.            
  217.             var walkingSpeed:Number = getWalkingSpeed();
  218.            
  219.             if (_hurt)
  220.                 _animation = "hurt";
  221.                
  222.             else if (heroCanClimbLadders && _climbing) {    //ladder handling
  223.  
  224.                 _animation = _climbAnimation;
  225.             }  
  226.             else if (!_onGround) {
  227.                
  228.                 _animation = "jump";
  229.                
  230.                 if (walkingSpeed < -acceleration)
  231.                     _inverted = true;
  232.                 else if (walkingSpeed > acceleration)
  233.                     _inverted = false;
  234.                
  235.             } else if (_ducking)
  236.                 _animation = "duck";
  237.                
  238.             else {
  239.                
  240.                 if (walkingSpeed < -acceleration) {
  241.                     _inverted = true;
  242.                     _animation = "walk";
  243.                    
  244.                 } else if (walkingSpeed > acceleration) {
  245.                    
  246.                     _inverted = false;
  247.                     _animation = "walk";
  248.                    
  249.                 } else
  250.                     _animation = "idle";
  251.             }
  252.            
  253.             if (prevAnimation != _animation)
  254.                 onAnimationChange.dispatch();
  255.                
  256.         }
  257.        
  258.        
  259.         ///when given a b2Contact, this returns the ladder, or null
  260.         private function returnBodyIfLadder(contact:b2Contact):b2Body
  261.         {
  262.             if (contact.GetFixtureA().GetBody().GetUserData() is Sensor && (Sensor)(contact.GetFixtureA().GetBody().GetUserData()).isLadder)
  263.                 return contact.GetFixtureA().GetBody();
  264.             else if (contact.GetFixtureB().GetBody().GetUserData() is Sensor && (Sensor)(contact.GetFixtureB().GetBody().GetUserData()).isLadder)
  265.                 return contact.GetFixtureB().GetBody();
  266.             else
  267.                 return null;
  268.         }
  269.        
  270.     }
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement