Add helper functions and set_x_movement in state functions

This commit is contained in:
Abdelrahman Said 2024-11-10 01:17:53 +00:00
parent 0852ca49db
commit 434b73226e

View File

@ -12,6 +12,12 @@
#define SPRITE_HEIGHT 64 #define SPRITE_HEIGHT 64
void get_player_controls(Player *player); void get_player_controls(Player *player);
void set_x_movement (Player *player);
void player_dash (Player *player);
void player_jump (Player *player);
void player_fall (Player *player);
int32_t player_gravity (Player *player);
bool player_on_ground (Player *player);
State *update_state (Player *player, uint32_t state); State *update_state (Player *player, uint32_t state);
State *idle_state (StateMachine *sm, Player *player); State *idle_state (StateMachine *sm, Player *player);
State *walk_state (StateMachine *sm, Player *player); State *walk_state (StateMachine *sm, Player *player);
@ -50,17 +56,6 @@ void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
void player_update(Player *player, uint32_t ticks) { void player_update(Player *player, uint32_t ticks) {
get_player_controls(player); get_player_controls(player);
if (player->controls.walk_left) {
player->movement.x = PLAYER_DIRECTION_LEFT * player->velocity;
player->x_direction = PLAYER_DIRECTION_LEFT;
} else if (player->controls.walk_right) {
player->movement.x = PLAYER_DIRECTION_RIGHT * player->velocity;
player->x_direction = PLAYER_DIRECTION_RIGHT;
} else if (!(player->controls.walk_left) && !(player->controls.walk_right)) {
player->movement.x = 0;
}
sm_run(&(player->state_machine), (void *)player); sm_run(&(player->state_machine), (void *)player);
player->position.x += player->movement.x; player->position.x += player->movement.x;
@ -68,6 +63,7 @@ void player_update(Player *player, uint32_t ticks) {
if (player->position.y > player->base_y) { if (player->position.y > player->base_y) {
player->position.y = player->base_y; player->position.y = player->base_y;
} }
ap_update(&(player->animations[player->current_state]), ticks); ap_update(&(player->animations[player->current_state]), ticks);
} }
@ -90,6 +86,38 @@ void get_player_controls(Player *player) {
player->controls.dash = player->controls.lctrl_last && !(player->controls.lctrl_pressed); player->controls.dash = player->controls.lctrl_last && !(player->controls.lctrl_pressed);
} }
void set_x_movement(Player *player) {
if (player->controls.walk_left) {
player->movement.x = PLAYER_DIRECTION_LEFT * player->velocity;
player->x_direction = PLAYER_DIRECTION_LEFT;
} else if (player->controls.walk_right) {
player->movement.x = PLAYER_DIRECTION_RIGHT * player->velocity;
player->x_direction = PLAYER_DIRECTION_RIGHT;
} else if (!(player->controls.walk_left) && !(player->controls.walk_right)) {
player->movement.x = 0;
}
}
void player_dash(Player *player) {
player->movement.x = player->x_direction * player->velocity * 2;
}
void player_jump(Player *player) {
player->movement.y = player->velocity * 6 * -1;
}
void player_fall(Player *player) {
player->movement.y = player->velocity * 6;
}
int32_t player_gravity(Player *player) {
return player->velocity * 0.5;
}
bool player_on_ground(Player *player) {
return player->position.y == player->base_y;
}
State *update_state(Player *player, uint32_t state) { State *update_state(Player *player, uint32_t state) {
if (state >= COUNT_PLAYER_STATES) { if (state >= COUNT_PLAYER_STATES) {
return &(player->states[player->current_state]); return &(player->states[player->current_state]);
@ -103,7 +131,7 @@ State *idle_state(StateMachine *sm, Player *player) {
uint32_t state = PLAYER_STATE_IDLE; uint32_t state = PLAYER_STATE_IDLE;
if (player->controls.jump) { if (player->controls.jump) {
player->movement.y = player->velocity * 6 * -1; player_jump(player);
state = PLAYER_STATE_JUMP; state = PLAYER_STATE_JUMP;
} else if (player->controls.walk_left) { } else if (player->controls.walk_left) {
state = PLAYER_STATE_WALK; state = PLAYER_STATE_WALK;
@ -111,6 +139,8 @@ State *idle_state(StateMachine *sm, Player *player) {
state = PLAYER_STATE_WALK; state = PLAYER_STATE_WALK;
} }
set_x_movement(player);
return update_state(player, state); return update_state(player, state);
} }
@ -118,11 +148,11 @@ State *walk_state(StateMachine *sm, Player *player) {
uint32_t state = PLAYER_STATE_WALK; uint32_t state = PLAYER_STATE_WALK;
if (player->controls.jump) { if (player->controls.jump) {
player->movement.y = player->velocity * 6 * -1; player_jump(player);
state = PLAYER_STATE_JUMP; state = PLAYER_STATE_JUMP;
} else if (player->controls.dash) { } else if (player->controls.dash) {
player->controls.dash_in_air = false; player_dash(player);
player->movement.x = player->x_direction * player->velocity * 2; player->controls.jump_dash = false;
state = PLAYER_STATE_DASH; state = PLAYER_STATE_DASH;
} }
@ -130,6 +160,8 @@ State *walk_state(StateMachine *sm, Player *player) {
state = PLAYER_STATE_IDLE; state = PLAYER_STATE_IDLE;
} }
set_x_movement(player);
return update_state(player, state); return update_state(player, state);
} }
@ -137,11 +169,11 @@ State *dash_state(StateMachine *sm, Player *player) {
bool reset = false; bool reset = false;
uint32_t state = PLAYER_STATE_DASH; uint32_t state = PLAYER_STATE_DASH;
player->movement.x += player->x_direction * 20; player->movement.x += player->x_direction * (int32_t)player->velocity * 0.5;
if (player->animations[PLAYER_STATE_DASH].finished) { if (player->animations[PLAYER_STATE_DASH].finished) {
if (player->controls.dash_in_air) { if (player->controls.jump_dash) {
player->movement.y = player->velocity * 6; player_fall(player);
state = PLAYER_STATE_FALL; state = PLAYER_STATE_FALL;
} else { } else {
player->movement.x = 0; player->movement.x = 0;
@ -162,16 +194,16 @@ State *jump_state(StateMachine *sm, Player *player) {
bool reset = false; bool reset = false;
uint32_t state = PLAYER_STATE_JUMP; uint32_t state = PLAYER_STATE_JUMP;
player->movement.y += player->velocity * 0.5; player->movement.y += player_gravity(player);
if (player->controls.dash) { if (player->controls.dash) {
player->controls.dash_in_air = true; player_dash(player);
player->movement.x = player->x_direction * player->velocity * 2; player->controls.jump_dash = true;
player->movement.y = player->velocity * 0.5; player->movement.y = player_gravity(player);
reset = true; reset = true;
state = PLAYER_STATE_DASH; state = PLAYER_STATE_DASH;
} else if (player->animations[PLAYER_STATE_JUMP].finished) { } else if (player->animations[PLAYER_STATE_JUMP].finished) {
player->movement.y = player->velocity * 6; player_fall(player);
reset = true; reset = true;
state = PLAYER_STATE_FALL; state = PLAYER_STATE_FALL;
} }
@ -180,6 +212,8 @@ State *jump_state(StateMachine *sm, Player *player) {
ap_reset(&(player->animations[PLAYER_STATE_JUMP])); ap_reset(&(player->animations[PLAYER_STATE_JUMP]));
} }
set_x_movement(player);
return update_state(player, state); return update_state(player, state);
} }
@ -187,7 +221,7 @@ State *fall_state(StateMachine *sm, Player *player) {
bool reset = false; bool reset = false;
uint32_t state = PLAYER_STATE_FALL; uint32_t state = PLAYER_STATE_FALL;
if (player->animations[PLAYER_STATE_FALL].finished) { if (player->animations[PLAYER_STATE_FALL].finished || player_on_ground(player)) {
player->movement.y = 0; player->movement.y = 0;
reset = true; reset = true;
state = PLAYER_STATE_IDLE; state = PLAYER_STATE_IDLE;
@ -197,5 +231,7 @@ State *fall_state(StateMachine *sm, Player *player) {
ap_reset(&(player->animations[PLAYER_STATE_FALL])); ap_reset(&(player->animations[PLAYER_STATE_FALL]));
} }
set_x_movement(player);
return update_state(player, state); return update_state(player, state);
} }