Advertisement
Guest User

Untitled

a guest
Jul 5th, 2012
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ON LOAD ///////////////////////////////////////////////////////////
  2. function playerClass::onLevelLoaded(%this, %scenegraph)  
  3. {
  4.     $pGuy = %this;  
  5.     moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");  
  6.     moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");  
  7.     moveMap.bindCmd(keyboard, "space", "playerJump();", "");  
  8.     $pGuy.setCollisionMaxIterations(2);
  9.     $pGuy.airborne = false;
  10.     $pGuy.runSpeed = 15;
  11.    
  12.     //Bind player to center  
  13.     //%force = 20;
  14.     //sceneWindow2D.mount($pGuy, "0 0", %force, true);  
  15. }  
  16.  
  17. // PLAYER CONTROLS////////////////////////////////////////////////////
  18. function playerLeft()  
  19. {
  20.     $pGuy.moveLeft = true;  
  21.     $pGuy.setFlip(true, false);  
  22. }  
  23.  
  24. function playerLeftStop()  
  25. {  
  26.     $pGuy.moveLeft = false;      
  27. }  
  28.  
  29. function playerRight()  
  30. {  
  31.     $pGuy.moveRight = true;  
  32.     $pGuy.setFlip(false, false);  
  33. }  
  34.  
  35. function playerRightStop()  
  36. {  
  37.     $pGuy.moveRight = false;  
  38. }  
  39.  
  40. function playerJump()  
  41. {  
  42.     %yVelocity = $pGuy.getLinearVelocityY();  
  43.     %xVelocity = $pGuy.getLinearVelocityX();  
  44.     $pGuy.setLinearVelocityY(10);  
  45.     $pGuy.setLinearVelocityX(0);  
  46.     %collision = $pGuy.castCollision(0.005);  
  47.     if(!(%collision $= ""))  
  48.     {  
  49.         $pGuy.setLinearVelocityY(-55); // Jump Height
  50.         $pGuy.airborne = true;
  51.     }  
  52.     else  
  53.     {  
  54.         $pGuy.setLinearVelocityY(%yVelocity);  
  55.     }  
  56.     $pGuy.setLinearVelocityX(%xVelocity);  
  57. }  
  58.  
  59. //UPDATE FUNCTION/////////////////////////////////////////////////////
  60. function playerClass::updateMovement(%this)  
  61. {  
  62.     if(%this.moveLeft)  
  63.     {  
  64.         %this.setLinearVelocityX(-%this.runSpeed);
  65.     }  
  66.      
  67.     if(%this.moveRight)  
  68.     {  
  69.         %this.setLinearVelocityX(%this.runSpeed);
  70.     }  
  71.      
  72.     if(!%this.moveLeft && !%this.moveRight)  
  73.     {  
  74.         %this.setLinearVelocityX(0);
  75.        
  76.         if(!%this.airborne)  
  77.             %this.setLinearVelocityY(0);
  78.     }
  79.     if(%this.moveLeft && %this.moveRight)  
  80.     {
  81.         %this.setLinearVelocityX(0);
  82.     }
  83.  
  84.     // Begin checking for objects around the player  
  85.     %yVelocity = $pGuy.getLinearVelocityY();
  86.     $pGuy.setCurrentAnimation(%yVelocity);              
  87.     $pGuy.setLinearVelocityY(100);  
  88.     %collision = $pGuy.castCollision(0.005);  
  89.    
  90.     // WALL CLIMBING FIX HACK.  
  91.     if(%collision !$= "")  
  92.     {  
  93.         if(%this.airborne) // NEW: Only kill the X-Velocity if we're flyin'.  
  94.             %this.setLinearVelocityX(0);  
  95.     }
  96.     // Check for ground beneath the feet.  
  97.     %this.setLinearVelocityY(10);  
  98.     %collision = %this.castCollision(0.005);
  99.    
  100.     // Before we move further, restore the previous velocity.  
  101.     %this.setLinearVelocityY(%yVelocity);  
  102.    
  103.     if(%collision $= "")  
  104.     {  
  105.         %this.airborne = true;        // NEW: Case that ensures falling == airborne.  
  106.         %this.setConstantForceY(100);  
  107.     }  
  108.    
  109.     // NEW: Fixes the following:  
  110.     //  A) Case where you're moving into a wall while you jump.  Without this, a player can  
  111.     //    bound over any wall around simply by holding the arrow key.  A special case of  
  112.     //    wall-climbing.  
  113.     //  B) Case for moving UP ramps / sloped platforms.  Does not really fix this issue,  
  114.     //    just allows for the case to be handled separately.  (This is actually handled in the  
  115.     //    final else-statement below).  
  116.     else if(%yVelocity < 0 && %this.airborne)  
  117.     {  
  118.         // We are in the special case where we are jumping while colliding against a wall.  
  119.         %this.setConstantForceY(100);  
  120.     }  
  121.     else  
  122.     {  
  123.         %this.airborne = false;        // NEW: Default for being on the ground!  
  124.         %this.setConstantForceY(0);  
  125.     }  
  126.  
  127.     // Make sure we're animating properly  
  128.     %this.setCurrentAnimation();
  129.    
  130. }  
  131.  
  132. // CURRENT ANIMATION ///////////////////////////////////////////////////////////
  133. function playerClass::setCurrentAnimation(%this)
  134. {
  135.     if(%yVelocity < 0 || %this.airborne)  
  136.     {  
  137.         %this.playAnimation(playerJumpUp);      
  138.     }  
  139.     else if(%yVelocity > 0 || %this.airborne)  
  140.     {  
  141.         %this.playAnimation(playerJumpDown);  
  142.     }
  143.  
  144.     else
  145.     {
  146.         if(%this.moveLeft || %this.moveRight)
  147.             {
  148.                 if(%this.getAnimationName() $= "playerRun")
  149.                     {
  150.                         if(%this.getIsAnimationFinished())
  151.                             {
  152.                                 %this.playAnimation(playerRun);
  153.                             }
  154.                     }
  155.                         else
  156.                     {
  157.                     %this.playAnimation(playerRun);
  158.                 }
  159.             }  
  160.         else
  161.             {
  162.                 %this.playAnimation(playerStand);
  163.             }
  164.      }      
  165.     if(%this.moveLeft && %this.moveRight)  
  166.     {  
  167.         %this.playAnimation(playerStand);  
  168.     }
  169. }
  170.  
  171. // UPDATE //////////////////////////////////////////////////////////////////
  172. function t2dSceneGraph::onUpdateScene()  
  173. {  
  174.        $pGuy.updateMovement();  
  175. }
Advertisement
RAW Paste Data Copied
Advertisement