rooksword

Delta Time Platformer

May 6th, 2020 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///Movement
  2.  
  3. global.delta = delta_time / ((1/room_speed)*1000000);
  4.  
  5. // Get input
  6. keyboard_set_map(ord("W"), vk_up);
  7. keyboard_set_map(ord("A"), vk_left);
  8. keyboard_set_map(ord("D"), vk_right);
  9.  
  10. key_right = keyboard_check(vk_right);
  11. key_left = -keyboard_check(vk_left);
  12. key_jump += buffer*keyboard_check_pressed(vk_up);
  13. if key_jump > 0 key_jump--;
  14.  
  15. move = key_left + key_right;
  16. if move != 0 //Accelerating
  17. {
  18.     dir = move;
  19.     hsp = (lerp(hsp,move * spd, 0.2)*global.delta);
  20. }
  21. else hsp = (lerp(hsp,move * spd,0.1)*global.delta); //Decelerating
  22.  
  23.  
  24. if vsp < 10 vsp += grav*global.delta;
  25.  
  26. if place_meeting(x, y + 1, obj_wall) //If on ground
  27. {
  28.     if key_jump > 0
  29.     {
  30.         vsp = sign(key_jump) * -jspd * global.delta;
  31.         key_jump = 0;
  32.     }
  33. }
  34.  
  35. //Collisions
  36. if place_meeting(x + hsp, y, obj_wall)
  37. {
  38.     while (!place_meeting(x+sign(hsp),y, obj_wall))
  39.     {
  40.         x += sign(hsp);
  41.     }
  42.     hsp = 0;
  43. }
  44. x += hsp;
  45.  
  46. if place_meeting(x, y + vsp, obj_wall)
  47. {
  48.     while !place_meeting(x, y + sign(vsp), obj_wall)
  49.     {
  50.         y += sign(vsp);
  51.     }
  52.     vsp = 0;
  53. }
  54. y += vsp;
  55.  
  56. //Clamp in room
  57. x = clamp(x,sprite_width/2,room_width-(sprite_width/2));
  58. y = clamp(y,sprite_height/2,room_height-(sprite_height/2));
Add Comment
Please, Sign In to add comment