Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import Box2D.Collision.b2Manifold;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Dynamics.b2Body;
- import Box2D.Dynamics.Contacts.b2Contact;
- import citrus.input.controllers.Keyboard;
- import citrus.objects.platformer.box2d.Hero;
- import citrus.objects.platformer.box2d.Platform;
- import citrus.objects.platformer.box2d.Sensor;
- /**
- * ...
- * @author david.hale
- */
- public class ladderHero extends Hero
- {
- public var canClimb:Boolean = false;
- public var climbVelocity:Number = 7;
- public var ladder:Sensor;
- protected var _onLadder:Boolean = false;
- protected var _climbing:Boolean = false;
- protected var _climbAnimation:String = "ladderIdle";
- private var heroCanClimbLadders:Boolean = false;
- public function ladderHero(name:String, params:Object=null)
- {
- super(name, params);
- }
- override public function update(timeDelta:Number):void
- {
- super.update(timeDelta);
- // we get a reference to the actual velocity vector
- var velocity:b2Vec2 = _body.GetLinearVelocity();
- if (controlsEnabled)
- {
- var moveKeyPressed:Boolean = false;
- _ducking = (_ce.input.isDoing("duck", inputChannel) && _onGround && canDuck);
- if (_ce.input.isDoing("right", inputChannel) && !_ducking)
- {
- velocity.Add(getSlopeBasedMoveAngle());
- moveKeyPressed = true;
- }
- if (_ce.input.isDoing("left", inputChannel) && !_ducking)
- {
- velocity.Subtract(getSlopeBasedMoveAngle());
- moveKeyPressed = true;
- }
- if(heroCanClimbLadders){
- //ladder stuff
- _onLadder = (canClimb);
- if (_onLadder && !_onGround) {
- if (x < ladder.x)
- x++;
- if (x > ladder.x)
- x--;
- }
- if (_ce.input.isDoing("up", inputChannel) && canClimb) {
- _onLadder = _climbing = true;
- _climbAnimation = "ladderClimbUp";
- velocity.y = -climbVelocity;
- velocity.x = 0;
- moveKeyPressed = true;
- }else if (_ce.input.isDoing("down", inputChannel) && canClimb) {
- _onLadder = _climbing = true;
- _climbAnimation = "ladderClimbDown";
- velocity.y = climbVelocity;
- velocity.x = 0;
- moveKeyPressed = true;
- }else if (_climbing && !_onGround) {
- velocity.y = -_friction; //if you don't counteract the _friction, the Hero will slowly fall down the ladder.
- }
- // Remove velocity if just stop climbing
- if ((_ce.input.justDid("up", inputChannel) || _ce.input.justDid("down", inputChannel)) && canClimb) {
- _climbAnimation = "ladderIdle";
- velocity.y = 0;
- }
- if (canClimb && _ce.input.justDid("jump", inputChannel)) {
- _climbing = false;
- velocity.y = -jumpHeight;
- onJump.dispatch();
- }
- //end ladder
- }
- //If player just started moving the hero this tick.
- if (moveKeyPressed && !_playerMovingHero)
- {
- _playerMovingHero = true;
- _fixture.SetFriction(0); //Take away friction so he can accelerate.
- }
- //Player just stopped moving the hero this tick.
- else if (!moveKeyPressed && _playerMovingHero)
- {
- _playerMovingHero = false;
- _fixture.SetFriction(_friction); //Add friction so that he stops running
- }
- if (_onGround && _ce.input.justDid("jump", inputChannel) && !_ducking)
- {
- velocity.y = -jumpHeight;
- onJump.dispatch();
- }
- if (_ce.input.isDoing("jump", inputChannel) && !_onGround && velocity.y < 0)
- {
- velocity.y -= jumpAcceleration;
- }
- if (_springOffEnemy != -1)
- {
- if (_ce.input.isDoing("jump", inputChannel))
- velocity.y = -enemySpringJumpHeight;
- else
- velocity.y = -enemySpringHeight;
- _springOffEnemy = -1;
- }
- //Cap running velocities
- if (velocity.x > (maxVelocity))
- velocity.x = maxVelocity;
- else if (velocity.x < (-maxVelocity))
- velocity.x = -maxVelocity;
- //set max ladder velocity
- if (velocity.y > (climbVelocity))
- velocity.y = climbVelocity;
- else if (velocity.y < (-climbVelocity))
- velocity.y = -climbVelocity;
- }
- updateAnimation();
- }
- public function setHeroCanClimbLadders(_heroClimbs:Boolean):void
- {
- heroCanClimbLadders = _heroClimbs;
- }
- override public function handlePreSolve(contact:b2Contact, oldManifold:b2Manifold):void
- {
- if(heroCanClimbLadders){
- //ladder handling
- var colliderBody:b2Body;
- //get which one is the Platform
- if (contact.GetFixtureA().GetBody().GetUserData() is Platform)
- colliderBody = contact.GetFixtureA().GetBody();
- else
- colliderBody = contact.GetFixtureB().GetBody();
- //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
- if (colliderBody.GetUserData() is Platform && canClimb && (_climbing || !_onGround)) {
- if (((Platform)(colliderBody.GetUserData()).y + (Platform)(colliderBody.GetUserData()).height / 2) < (ladder.y + ladder.height / 2)) {
- contact.SetEnabled(false); //this turns off the collision for the platform when we are crossing it vertically on the ladder
- }
- }
- }
- super.handlePreSolve(contact, oldManifold);
- }
- override public function handleBeginContact(contact:b2Contact):void
- {
- if(heroCanClimbLadders){
- var ladderBody:b2Body = returnBodyIfLadder(contact);
- //yes, one of the two fixtures was indeed a ladder
- if (ladderBody != null) {
- canClimb = true;
- canDuck = false;
- ladder = ladderBody.GetUserData();
- }
- }
- super.handleBeginContact(contact);
- }
- override public function handleEndContact(contact:b2Contact):void
- {
- super.handleEndContact(contact);
- if(heroCanClimbLadders){
- var ladderBody:b2Body = returnBodyIfLadder(contact);
- if(ladderBody != null){
- _climbing = canClimb = false;
- canDuck = true;
- }
- }
- }
- override protected function updateAnimation():void
- {
- var prevAnimation:String = _animation;
- var walkingSpeed:Number = getWalkingSpeed();
- if (_hurt)
- _animation = "hurt";
- else if (heroCanClimbLadders && _climbing) { //ladder handling
- _animation = _climbAnimation;
- }
- else if (!_onGround) {
- _animation = "jump";
- if (walkingSpeed < -acceleration)
- _inverted = true;
- else if (walkingSpeed > acceleration)
- _inverted = false;
- } else if (_ducking)
- _animation = "duck";
- else {
- if (walkingSpeed < -acceleration) {
- _inverted = true;
- _animation = "walk";
- } else if (walkingSpeed > acceleration) {
- _inverted = false;
- _animation = "walk";
- } else
- _animation = "idle";
- }
- if (prevAnimation != _animation)
- onAnimationChange.dispatch();
- }
- ///when given a b2Contact, this returns the ladder, or null
- private function returnBodyIfLadder(contact:b2Contact):b2Body
- {
- if (contact.GetFixtureA().GetBody().GetUserData() is Sensor && (Sensor)(contact.GetFixtureA().GetBody().GetUserData()).isLadder)
- return contact.GetFixtureA().GetBody();
- else if (contact.GetFixtureB().GetBody().GetUserData() is Sensor && (Sensor)(contact.GetFixtureB().GetBody().GetUserData()).isLadder)
- return contact.GetFixtureB().GetBody();
- else
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement