Advertisement
Guest User

Movement

a guest
Apr 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. //This is used to get input from the user based on the game state
  2. switch(gameState){
  3.  
  4. case gStates.playing:
  5. /*
  6. If the player isn't in a menu then this will get all the input needed
  7. and determine what state the player should be in
  8. */
  9. key_Up = -keyboard_check(ord("W"));
  10. key_Down = keyboard_check(ord("S"));
  11. key_Left = -keyboard_check(ord("A"));
  12. key_Right = keyboard_check(ord("D"));
  13. key_Dodge = keyboard_check_pressed(vk_space);
  14.  
  15. /*
  16. If the player hits the dodge button (space) and dodge is off cooldown
  17. then this will make all the necessary calculations for the dodge movement
  18. and then change the player state to dodging.
  19. */
  20. if(key_Dodge && dodgeCount >= 30){
  21. playerState = states.dodge;
  22.  
  23. targetX = mouse_x - xPos;
  24. targetY = mouse_y - yPos;
  25.  
  26. //Get the unit vector magnitude
  27. unitVectorMag = sqrt(sqr(targetX) + sqr(targetY));
  28.  
  29. //Check to see if the target for the dodge is greater then maxDodgeDist
  30. sizeDif = max(1,abs(unitVectorMag/maxDodgeDist));
  31.  
  32. //If the size dif is > 1 then we use the unit vector, otherwise just use division
  33. if(sizeDif > 1){
  34. xVel = abs((targetX/sizeDif) / dodgeInstances) * (targetX/unitVectorMag);
  35. yVel = abs((targetY/sizeDif) / dodgeInstances) * (targetY/unitVectorMag);
  36. } else {
  37. xVel = targetX/dodgeInstances;
  38. yVel = targetY/dodgeInstances;
  39. }
  40.  
  41. dodgeCount = 0;
  42.  
  43. /*
  44. We break from the switch here since there is nothing else that needs to be done
  45. after this. The player is now dodging and cannot walk, attack or do anything else
  46. */
  47. break;
  48. }
  49.  
  50. hMove = key_Left + key_Right;
  51. vMove = key_Up + key_Down;
  52.  
  53. if(hMove != 0 && vMove != 0){
  54. unitVectorMag = sqrt(2);
  55. } else {
  56. unitVectorMag = 1;
  57. }
  58.  
  59. break;
  60.  
  61. case gStates.inventory:
  62.  
  63. break;
  64.  
  65. default: //This is standing
  66. break;
  67. }
  68.  
  69. //This is used to control the player based on the players state
  70. switch(playerState){
  71.  
  72. case states.walk:
  73.  
  74. //We use the same variable to count dodgeinstances and its cool down. "EFFICIENCY"
  75. dodgeCount += 1;
  76.  
  77. /*
  78. Movespeed is calculated in this moment as the player might randomly gain or loose
  79. agi from an item or buff. We use large numbers that are kater scaled down to allow
  80. for more variation in movement speeds between enemies and players.
  81. */
  82. maxMoveSpeed = 200 + (2*totalAgi);
  83. currentMoveSpeed = maxMoveSpeed;
  84.  
  85. /*
  86. What the fuck is going on here?
  87. Well its really quite simple, if the player is inputting that they want to move in
  88. the x axis (hMove != 0), then we check to see if they were previously moving in that
  89. direction or at all (sign(hMove) == sign(xVel) || round(xVel) == 0), otherwise
  90. we just simply deccelerate the players movement in the x axis
  91. */
  92. if(hMove != 0 && (sign(hMove) == sign(xVel) || round(xVel) == 0)){
  93. xVel += (currentMoveSpeed / 600) * hMove;
  94. } else {
  95. xVel = xVel - (xVel/3);
  96. }
  97. //Same thing as above boi
  98. if(vMove != 0 && (sign(vMove) == sign(yVel) || round(yVel) == 0)){
  99. yVel += (currentMoveSpeed / 600) * vMove;
  100. } else {
  101. yVel = yVel - (yVel/3);
  102. }
  103.  
  104.  
  105. /*
  106. This just simple checks to see if the player is to much of a speedy boi or not..
  107. If vel is greater then the movement speed then we make the proper adjustments
  108. */
  109. if(abs(xVel) > (currentMoveSpeed / 100)){
  110. xVel = (currentMoveSpeed / 100) * hMove;
  111. }
  112. if(abs(yVel) > (currentMoveSpeed / 100)){
  113. yVel = (currentMoveSpeed / 100) * vMove;
  114. }
  115.  
  116. /*
  117. WHY WE USE 2 VARS FOR POSITION? REEE? One gets track of the real values of
  118. the player position (xPos and yPos) which allows for small difference in movement
  119. speed to actually make a difference and the other one is the int value of the
  120. players position to makes sure the image isnt have a seizure on the screen
  121. */
  122. xPos += xVel * (1/unitVectorMag);
  123. yPos += yVel * (1/unitVectorMag);
  124.  
  125. phy_position_x = round(xPos);
  126. phy_position_y = round(yPos);
  127.  
  128. break;
  129.  
  130. case states.attack:
  131. break;
  132.  
  133. case states.dodge:
  134.  
  135. /*
  136. 2 VARS AGAIN? just check the same thing int the walk section pls
  137. */
  138. xPos += xVel;
  139. yPos += yVel;
  140.  
  141. phy_position_x = round(xPos);
  142. phy_position_y = round(yPos);
  143.  
  144. //If boi over num of dodges allowed. halt him from doing the dodgy doo
  145. dodgeCount += 1;
  146. if(dodgeCount == dodgeInstances){
  147. playerState = states.walk;
  148.  
  149. xVel = 0;
  150. yVel = 0;
  151. }
  152.  
  153. break;
  154.  
  155. default: //This is standing
  156. break;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement