Advertisement
SEnergy

2D Platform Movement

Sep 29th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///Handle - Movement & Collisions
  2.  
  3. x = round(x);
  4. y = round(y);
  5.  
  6. //Handle - Movement
  7. hmove = keyboard_check(vk_right) - keyboard_check(vk_left);
  8.  
  9. if(place_free(x+hmove, y))
  10.     hspd = speed_max*hmove;
  11. else
  12.     hspd = 0;
  13.  
  14. //Handle - Falling & Jumping
  15. if(place_free(x, y+vspd+1))
  16.     vspd += speed_fall;
  17.    
  18. if(vspd == 0)
  19. {
  20.     if(place_free(x+hspd, y-1) && !place_free(x, y+1))
  21.         if(keyboard_check(vk_up))
  22.             vspd = -speed_jump;
  23. }
  24.  
  25. //Collision
  26. if(!place_free(x+hspd, y+vspd))
  27. {
  28.     if(!place_free(x+hspd, y))
  29.         while(place_free(x+sign(hspd), y))
  30.             x += sign(hspd);
  31.            
  32.     if(!place_free(x, y+vspd))
  33.         while(place_free(x, y+sign(vspd)))
  34.             y += sign(vspd);
  35.    
  36.     if(!place_free(x+hspd, y))
  37.         hspd = 0;
  38.     if(!place_free(x, y+vspd))
  39.         vspd = 0;
  40.        
  41.     if(vspd != 0 && hspd != 0)
  42.     {
  43.         if(place_free(x+hspd, y))
  44.             if(place_free(x, y+vspd))
  45.                 while(!place_free(x+hspd, y+vspd) && (hspd*vspd) != 0)
  46.                 {
  47.                     y++;
  48.                     if(!place_free(x+hspd, y))
  49.                         hspd = 0;
  50.                     if(!place_free(x, y+vspd))
  51.                         vspd = 0;
  52.                 }
  53.     }
  54. }
  55.  
  56. x += hspd;
  57. y += vspd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement