Advertisement
Humphryy

Untitled

Sep 7th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Player input
  2. key_left = keyboard_check(vk_left);
  3. key_right = keyboard_check(vk_right);
  4. key_jump = keyboard_check_pressed(vk_space);
  5.  
  6. // Track last direction pressed
  7. if (key_left) {
  8.     last_direction = -1; // Left
  9. } else if (key_right) {
  10.     last_direction = 1; // Right
  11. }
  12.  
  13. // Calculate movement
  14. var move = key_right - key_left;
  15.  
  16. hsp = move * walksp;
  17. vsp += grv;
  18.  
  19. // Flip the player based on movement direction
  20. if (move != 0) {
  21.     image_xscale = last_direction * 2; // Flip while maintaining 2x scale
  22. }
  23.  
  24. // Jumping logic
  25. if (place_meeting(x, y + 1, obj_Wall) && key_jump) {
  26.     vsp = -13;
  27.     sprite_index = spr_Player_Jump;
  28.     image_speed = 1;
  29.     image_index = 0;
  30. }
  31.  
  32. // Horizontal Movement and Collision
  33. if (hsp != 0) {
  34.     if (!place_meeting(x + hsp, y, obj_Wall)) {
  35.         x += hsp; // Move if no collision
  36.     } else {
  37.         while (!place_meeting(x + sign(hsp), y, obj_Wall)) {
  38.             x += sign(hsp); // Move close to wall
  39.         }
  40.         hsp = 0; // Stop on collision
  41.     }
  42. }
  43.  
  44. // Vertical Movement and Collision
  45. if (vsp != 0) {
  46.     if (!place_meeting(x, y + vsp, obj_Wall)) {
  47.         y += vsp; // Move if no collision
  48.     } else {
  49.         while (!place_meeting(x, y + sign(vsp), obj_Wall)) {
  50.             y += sign(vsp); // Move close to floor/ceiling
  51.         }
  52.         vsp = 0; // Stop on collision
  53.     }
  54. }
  55.  
  56. // Animations
  57. if (!place_meeting(x, y + 1, obj_Wall)) {
  58.     // Player in air
  59.     if (vsp < 0) {
  60.         // Jumping animation
  61.         if (sprite_index != spr_Player_Jump || image_index < 2) {
  62.             sprite_index = spr_Player_Jump;
  63.             image_index = (image_index < 2) ? image_index : 2;
  64.         }
  65.     } else {
  66.         // Falling animation
  67.         sprite_index = spr_Player_Jump;
  68.         if (image_index < 3 || image_index > 5) {
  69.             image_index = 3;
  70.         }
  71.     }
  72. } else if (vsp == 0) {
  73.     // Player grounded
  74.     if (move != 0) {
  75.         // Running animation
  76.         sprite_index = spr_Player_Run;
  77.     } else {
  78.         // Idle animation after landing from jump
  79.         if (sprite_index == spr_Player_Jump && image_index >= 5) {
  80.             if (image_index < 6) {
  81.                 image_index = 6;
  82.             } else if (image_index >= 8) {
  83.                 sprite_index = spr_Player_Idle;
  84.             }
  85.         } else {
  86.             sprite_index = spr_Player_Idle;
  87.         }
  88.     }
  89. }
  90. image_speed = 1;
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement