Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ON LOAD ///////////////////////////////////////////////////////////
- function playerClass::onLevelLoaded(%this, %scenegraph)
- {
- $pGuy = %this;
- moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
- moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
- moveMap.bindCmd(keyboard, "space", "playerJump();", "");
- $pGuy.setCollisionMaxIterations(2);
- $pGuy.airborne = false;
- $pGuy.runSpeed = 15;
- //Bind player to center
- //%force = 20;
- //sceneWindow2D.mount($pGuy, "0 0", %force, true);
- }
- // PLAYER CONTROLS////////////////////////////////////////////////////
- function playerLeft()
- {
- $pGuy.moveLeft = true;
- $pGuy.setFlip(true, false);
- }
- function playerLeftStop()
- {
- $pGuy.moveLeft = false;
- }
- function playerRight()
- {
- $pGuy.moveRight = true;
- $pGuy.setFlip(false, false);
- }
- function playerRightStop()
- {
- $pGuy.moveRight = false;
- }
- function playerJump()
- {
- %yVelocity = $pGuy.getLinearVelocityY();
- %xVelocity = $pGuy.getLinearVelocityX();
- $pGuy.setLinearVelocityY(10);
- $pGuy.setLinearVelocityX(0);
- %collision = $pGuy.castCollision(0.005);
- if(!(%collision $= ""))
- {
- $pGuy.setLinearVelocityY(-55); // Jump Height
- $pGuy.airborne = true;
- }
- else
- {
- $pGuy.setLinearVelocityY(%yVelocity);
- }
- $pGuy.setLinearVelocityX(%xVelocity);
- }
- //UPDATE FUNCTION/////////////////////////////////////////////////////
- function playerClass::updateMovement(%this)
- {
- if(%this.moveLeft)
- {
- %this.setLinearVelocityX(-%this.runSpeed);
- }
- if(%this.moveRight)
- {
- %this.setLinearVelocityX(%this.runSpeed);
- }
- if(!%this.moveLeft && !%this.moveRight)
- {
- %this.setLinearVelocityX(0);
- if(!%this.airborne)
- %this.setLinearVelocityY(0);
- }
- if(%this.moveLeft && %this.moveRight)
- {
- %this.setLinearVelocityX(0);
- }
- // Begin checking for objects around the player
- %yVelocity = $pGuy.getLinearVelocityY();
- $pGuy.setCurrentAnimation(%yVelocity);
- $pGuy.setLinearVelocityY(100);
- %collision = $pGuy.castCollision(0.005);
- // WALL CLIMBING FIX HACK.
- if(%collision !$= "")
- {
- if(%this.airborne) // NEW: Only kill the X-Velocity if we're flyin'.
- %this.setLinearVelocityX(0);
- }
- // Check for ground beneath the feet.
- %this.setLinearVelocityY(10);
- %collision = %this.castCollision(0.005);
- // Before we move further, restore the previous velocity.
- %this.setLinearVelocityY(%yVelocity);
- if(%collision $= "")
- {
- %this.airborne = true; // NEW: Case that ensures falling == airborne.
- %this.setConstantForceY(100);
- }
- // NEW: Fixes the following:
- // A) Case where you're moving into a wall while you jump. Without this, a player can
- // bound over any wall around simply by holding the arrow key. A special case of
- // wall-climbing.
- // B) Case for moving UP ramps / sloped platforms. Does not really fix this issue,
- // just allows for the case to be handled separately. (This is actually handled in the
- // final else-statement below).
- else if(%yVelocity < 0 && %this.airborne)
- {
- // We are in the special case where we are jumping while colliding against a wall.
- %this.setConstantForceY(100);
- }
- else
- {
- %this.airborne = false; // NEW: Default for being on the ground!
- %this.setConstantForceY(0);
- }
- // Make sure we're animating properly
- %this.setCurrentAnimation();
- }
- // CURRENT ANIMATION ///////////////////////////////////////////////////////////
- function playerClass::setCurrentAnimation(%this)
- {
- if(%yVelocity < 0 || %this.airborne)
- {
- %this.playAnimation(playerJumpUp);
- }
- else if(%yVelocity > 0 || %this.airborne)
- {
- %this.playAnimation(playerJumpDown);
- }
- else
- {
- if(%this.moveLeft || %this.moveRight)
- {
- if(%this.getAnimationName() $= "playerRun")
- {
- if(%this.getIsAnimationFinished())
- {
- %this.playAnimation(playerRun);
- }
- }
- else
- {
- %this.playAnimation(playerRun);
- }
- }
- else
- {
- %this.playAnimation(playerStand);
- }
- }
- if(%this.moveLeft && %this.moveRight)
- {
- %this.playAnimation(playerStand);
- }
- }
- // UPDATE //////////////////////////////////////////////////////////////////
- function t2dSceneGraph::onUpdateScene()
- {
- $pGuy.updateMovement();
- }
Advertisement
RAW Paste Data
Copied
Advertisement