Advertisement
MRDANEEYUL

The Waking Cloak - Player Movement

Feb 18th, 2017
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///-------------------------------------------------------------
  2. /// This is not the complete code--just some relevant bits since Tumblr code formatting sucks.
  3. ///-------------------------------------------------------------
  4.  
  5.  
  6. ///-------------------------------------------------------------
  7. /// @description Player Object - Create
  8. ///-------------------------------------------------------------
  9. xSpeed = 0;
  10. ySpeed = 0;
  11. xSpeedRemainder = 0;
  12. ySpeedRemainder = 0;
  13.  
  14. ///-------------------------------------------------------------
  15. /// @description  Player Object - Begin Step
  16. ///-------------------------------------------------------------
  17. xSpeed = 0;
  18. ySpeed = 0;
  19.  
  20. ///-------------------------------------------------------------
  21. /// @description PlayerWalkState script
  22. ///-------------------------------------------------------------
  23. if(state_new)
  24. {
  25.     playerAnimationSpeed = 1;
  26.     image_index = 1;
  27.     canTransitionCamera = true;
  28.     isInputBufferOpen = true;
  29. }
  30.  
  31. if(isCameraTransitioning)
  32. {
  33.     StateSwitch("CameraTransition");
  34.     return;
  35. }
  36.  
  37. if(!isUpKeyHeld && !isDownKeyHeld && !isRightKeyHeld && !isLeftKeyHeld)
  38. {
  39.     StateSwitch("Stand");
  40.     return;
  41. }
  42. CheckItemState();
  43.  
  44. image_speed = playerAnimationSpeed;  
  45. playerSpeed = 1;
  46. playerSpeedDiagonal = playerSpeed * 0.707; //We could calculate exact speed, but this constant is close enough.
  47.  
  48. CheckCameraTransitionState();
  49.  
  50. xSpeed = 0;
  51. ySpeed = 0;
  52.  
  53. //Northwest
  54. if(isUpKeyHeld && isLeftKeyHeld && !isDownKeyHeld && !isRightKeyHeld)
  55. {
  56.     sprite_index = player_walk_n_sp;
  57.     rotation = compass.northwest;
  58.     xSpeed = -playerSpeedDiagonal;
  59.     ySpeed = -playerSpeedDiagonal;
  60. }
  61. //Northeast
  62. else if(isUpKeyHeld && isRightKeyHeld && !isDownKeyHeld && !isLeftKeyHeld)
  63. {
  64.     sprite_index = player_walk_n_sp;
  65.     rotation = compass.northeast;
  66.     xSpeed = playerSpeedDiagonal;
  67.     ySpeed = -playerSpeedDiagonal;
  68. }
  69. //Southwest
  70. else if(isDownKeyHeld && isLeftKeyHeld && !isUpKeyHeld && !isRightKeyHeld)
  71. {
  72.     sprite_index = player_walk_s_sp;
  73.     rotation = compass.southwest;
  74.     xSpeed = -playerSpeedDiagonal;
  75.     ySpeed = playerSpeedDiagonal;
  76. }
  77. //Southeast
  78. else if(isDownKeyHeld && isRightKeyHeld && !isUpKeyHeld && !isLeftKeyHeld)
  79. {
  80.     sprite_index = player_walk_s_sp;
  81.     rotation = compass.southeast;
  82.     xSpeed = playerSpeedDiagonal;
  83.     ySpeed = playerSpeedDiagonal;
  84. }
  85. //North
  86. else if(isUpKeyHeld)
  87. {
  88.     sprite_index = player_walk_n_sp;
  89.     rotation = compass.north;
  90.     ySpeed = -playerSpeed;
  91. }
  92. //South
  93. else if(isDownKeyHeld)
  94. {
  95.     sprite_index = player_walk_s_sp;
  96.     rotation = compass.south;
  97.     ySpeed = playerSpeed;
  98. }
  99. //West
  100. else if(isLeftKeyHeld)
  101. {
  102.     sprite_index = player_walk_w_sp;
  103.     rotation = compass.west;    
  104.     xSpeed = -playerSpeed;
  105. }
  106. //East
  107. else if(isRightKeyHeld)
  108. {
  109.     sprite_index = player_walk_e_sp;
  110.     rotation = compass.east;    
  111.     xSpeed = playerSpeed;
  112. }
  113.  
  114. ///-------------------------------------------------------------
  115. /// @description Player Object - End Step
  116. ///-------------------------------------------------------------
  117. StateUpdate();
  118.  
  119. //Add the speed to the respective remainders.
  120. xSpeedRemainder += xSpeed;
  121. ySpeedRemainder += ySpeed;
  122.  
  123. //Sync up remainders for diagonals to avoid stair-step effect (abs() prevents sync on knockback).
  124. if(xSpeed != 0 && ySpeed != 0 && abs(xSpeed) == abs(ySpeed))
  125. {
  126.     if (ySpeedRemainder < xSpeedRemainder)
  127.         ySpeedRemainder = sign(ySpeedRemainder) * abs(xSpeedRemainder);
  128.     if (xSpeedRemainder < ySpeedRemainder)
  129.         xSpeedRemainder = sign(xSpeedRemainder) * abs(ySpeedRemainder);
  130. }
  131.  
  132. //If the remainder is >= 1 or <= -1, set the whole number as the speed.
  133. var xSpeedWholeNum = xSpeedRemainder & ~0;
  134. var ySpeedWholeNum = ySpeedRemainder & ~0;
  135.  
  136. //Move x, process x collision.
  137. x += xSpeedWholeNum;
  138. ProcessCollision(id, xSpeedWholeNum, 0);
  139.  
  140. //Move y, process y collision.
  141. y += ySpeedWholeNum;
  142. ProcessCollision(id, 0, ySpeedWholeNum);
  143.  
  144. //Hitbox should follow player.
  145. player_hitbox_obj.x = x;
  146. player_hitbox_obj.y = y;
  147.  
  148. //Subtract the whole number from the respective remainder (so just a fraction is left).
  149. xSpeedRemainder -= xSpeedWholeNum;
  150. ySpeedRemainder -= ySpeedWholeNum;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement