Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Player input
- key_left = keyboard_check(vk_left);
- key_right = keyboard_check(vk_right);
- key_jump = keyboard_check_pressed(vk_space);
- // Track last direction pressed
- if (key_left) {
- last_direction = -1; // Left
- } else if (key_right) {
- last_direction = 1; // Right
- }
- // Calculate movement
- var move = key_right - key_left;
- hsp = move * walksp;
- vsp += grv;
- // Flip the player based on movement direction
- if (move != 0) {
- image_xscale = last_direction * 2; // Flip while maintaining 2x scale
- }
- // Jumping logic
- if (place_meeting(x, y + 1, obj_Wall) && key_jump) {
- vsp = -13;
- sprite_index = spr_Player_Jump;
- image_speed = 1;
- image_index = 0;
- }
- // Horizontal Movement and Collision
- if (hsp != 0) {
- if (!place_meeting(x + hsp, y, obj_Wall)) {
- x += hsp; // Move if no collision
- } else {
- while (!place_meeting(x + sign(hsp), y, obj_Wall)) {
- x += sign(hsp); // Move close to wall
- }
- hsp = 0; // Stop on collision
- }
- }
- // Vertical Movement and Collision
- if (vsp != 0) {
- if (!place_meeting(x, y + vsp, obj_Wall)) {
- y += vsp; // Move if no collision
- } else {
- while (!place_meeting(x, y + sign(vsp), obj_Wall)) {
- y += sign(vsp); // Move close to floor/ceiling
- }
- vsp = 0; // Stop on collision
- }
- }
- // Animations
- if (!place_meeting(x, y + 1, obj_Wall)) {
- // Player in air
- if (vsp < 0) {
- // Jumping animation
- if (sprite_index != spr_Player_Jump || image_index < 2) {
- sprite_index = spr_Player_Jump;
- image_index = (image_index < 2) ? image_index : 2;
- }
- } else {
- // Falling animation
- sprite_index = spr_Player_Jump;
- if (image_index < 3 || image_index > 5) {
- image_index = 3;
- }
- }
- } else if (vsp == 0) {
- // Player grounded
- if (move != 0) {
- // Running animation
- sprite_index = spr_Player_Run;
- } else {
- // Idle animation after landing from jump
- if (sprite_index == spr_Player_Jump && image_index >= 5) {
- if (image_index < 6) {
- image_index = 6;
- } else if (image_index >= 8) {
- sprite_index = spr_Player_Idle;
- }
- } else {
- sprite_index = spr_Player_Idle;
- }
- }
- }
- image_speed = 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement