Compare commits

..

4 Commits

2 changed files with 90 additions and 66 deletions

124
player.c
View File

@@ -11,26 +11,14 @@
#define SPRITE_WIDTH 100
#define SPRITE_HEIGHT 64
void print_state (Player *player) {
switch(player->current_state) {
case PLAYER_STATE_IDLE:
printf("IDLE\n");
break;
case PLAYER_STATE_WALK:
printf("WALK\n");
break;
case PLAYER_STATE_DASH:
printf("DASH\n");
break;
case PLAYER_STATE_JUMP:
printf("JUMP\n");
break;
case PLAYER_STATE_FALL:
printf("FALL\n");
break;
}
}
void get_player_controls(Player *player);
void set_x_movement (Player *player);
void player_move (Player *player);
void player_dash (Player *player);
void player_jump (Player *player);
void player_fall (Player *player);
bool player_on_ground (Player *player);
int32_t player_gravity (Player *player);
State *update_state (Player *player, uint32_t state);
State *idle_state (StateMachine *sm, Player *player);
State *walk_state (StateMachine *sm, Player *player);
@@ -54,7 +42,7 @@ void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
player->animations[PLAYER_STATE_JUMP] =
ap_init(renderer, "knight_player/Jump_KG_2.png", 100, SPRITE_WIDTH, SPRITE_HEIGHT, false);
player->animations[PLAYER_STATE_FALL] =
ap_init(renderer, "knight_player/Fall_KG_2.png", 80, SPRITE_WIDTH, SPRITE_HEIGHT, false);
ap_init(renderer, "knight_player/Fall_KG_2.png", 50, SPRITE_WIDTH, SPRITE_HEIGHT, false);
player->states[PLAYER_STATE_IDLE] = (State){ .state_func = (StateFunc *)idle_state };
player->states[PLAYER_STATE_WALK] = (State){ .state_func = (StateFunc *)walk_state };
@@ -69,24 +57,8 @@ void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
void player_update(Player *player, uint32_t ticks) {
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);
player->position.x += player->movement.x;
player->position.y += player->movement.y;
if (player->position.y > player->base_y) {
player->position.y = player->base_y;
}
player_move(player);
ap_update(&(player->animations[player->current_state]), ticks);
}
@@ -109,6 +81,50 @@ void get_player_controls(Player *player) {
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_move(Player *player) {
player->position.x += player->movement.x;
player->position.y += player->movement.y;
if (player->position.y > player->base_y) {
player->position.y = player->base_y;
}
}
void player_dash(Player *player) {
player->movement.x = player->x_direction * player->velocity * 2;
}
void player_jump(Player *player) {
player->movement.y = player->velocity * 7 * -1;
}
void player_fall(Player *player) {
player->movement.y = player->velocity * 6;
}
bool player_on_ground(Player *player) {
return player->position.y == player->base_y;
}
int32_t player_gravity(Player *player) {
return player->velocity * 0.5;
}
bool player_falling(Player *player) {
return player->movement.y > 0;
}
State *update_state(Player *player, uint32_t state) {
if (state >= COUNT_PLAYER_STATES) {
return &(player->states[player->current_state]);
@@ -122,7 +138,7 @@ State *idle_state(StateMachine *sm, Player *player) {
uint32_t state = PLAYER_STATE_IDLE;
if (player->controls.jump) {
player->movement.y = player->velocity * 6 * -1;
player_jump(player);
state = PLAYER_STATE_JUMP;
} else if (player->controls.walk_left) {
state = PLAYER_STATE_WALK;
@@ -130,6 +146,8 @@ State *idle_state(StateMachine *sm, Player *player) {
state = PLAYER_STATE_WALK;
}
set_x_movement(player);
return update_state(player, state);
}
@@ -137,11 +155,11 @@ State *walk_state(StateMachine *sm, Player *player) {
uint32_t state = PLAYER_STATE_WALK;
if (player->controls.jump) {
player->movement.y = player->velocity * 6 * -1;
player_jump(player);
state = PLAYER_STATE_JUMP;
} else if (player->controls.dash) {
player->controls.dash_in_air = false;
player->movement.x = player->x_direction * player->velocity * 2;
player_dash(player);
player->controls.jump_dash = false;
state = PLAYER_STATE_DASH;
}
@@ -149,6 +167,8 @@ State *walk_state(StateMachine *sm, Player *player) {
state = PLAYER_STATE_IDLE;
}
set_x_movement(player);
return update_state(player, state);
}
@@ -156,11 +176,11 @@ State *dash_state(StateMachine *sm, Player *player) {
bool reset = false;
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->controls.dash_in_air) {
player->movement.y = player->velocity * 6;
if (player->controls.jump_dash) {
player_fall(player);
state = PLAYER_STATE_FALL;
} else {
player->movement.x = 0;
@@ -181,16 +201,16 @@ State *jump_state(StateMachine *sm, Player *player) {
bool reset = false;
uint32_t state = PLAYER_STATE_JUMP;
player->movement.y += player->velocity * 0.5;
player->movement.y += player_gravity(player);
if (player->controls.dash) {
player->controls.dash_in_air = true;
player->movement.x = player->x_direction * player->velocity * 2;
player->movement.y = player->velocity * 0.5;
player_dash(player);
player->controls.jump_dash = true;
player->movement.y = player_gravity(player);
reset = true;
state = PLAYER_STATE_DASH;
} else if (player->animations[PLAYER_STATE_JUMP].finished) {
player->movement.y = player->velocity * 6;
player_fall(player);
reset = true;
state = PLAYER_STATE_FALL;
}
@@ -199,6 +219,8 @@ State *jump_state(StateMachine *sm, Player *player) {
ap_reset(&(player->animations[PLAYER_STATE_JUMP]));
}
set_x_movement(player);
return update_state(player, state);
}
@@ -206,7 +228,7 @@ State *fall_state(StateMachine *sm, Player *player) {
bool reset = false;
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;
reset = true;
state = PLAYER_STATE_IDLE;
@@ -216,5 +238,7 @@ State *fall_state(StateMachine *sm, Player *player) {
ap_reset(&(player->animations[PLAYER_STATE_FALL]));
}
set_x_movement(player);
return update_state(player, state);
}

View File

@@ -35,7 +35,7 @@ struct player_control {
bool lctrl_last;
bool lctrl_pressed;
bool dash;
bool dash_in_air;
bool jump_dash;
};
typedef struct movement Movement;