Compare commits
4 Commits
6182a2db85
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e2c781098c | |||
| 434b73226e | |||
| 0852ca49db | |||
| d049ae7933 |
154
player.c
154
player.c
@@ -11,32 +11,20 @@
|
|||||||
#define SPRITE_WIDTH 100
|
#define SPRITE_WIDTH 100
|
||||||
#define SPRITE_HEIGHT 64
|
#define SPRITE_HEIGHT 64
|
||||||
|
|
||||||
void print_state (Player *player) {
|
void get_player_controls(Player *player);
|
||||||
switch(player->current_state) {
|
void set_x_movement (Player *player);
|
||||||
case PLAYER_STATE_IDLE:
|
void player_move (Player *player);
|
||||||
printf("IDLE\n");
|
void player_dash (Player *player);
|
||||||
break;
|
void player_jump (Player *player);
|
||||||
case PLAYER_STATE_WALK:
|
void player_fall (Player *player);
|
||||||
printf("WALK\n");
|
bool player_on_ground (Player *player);
|
||||||
break;
|
int32_t player_gravity (Player *player);
|
||||||
case PLAYER_STATE_DASH:
|
State *update_state (Player *player, uint32_t state);
|
||||||
printf("DASH\n");
|
State *idle_state (StateMachine *sm, Player *player);
|
||||||
break;
|
State *walk_state (StateMachine *sm, Player *player);
|
||||||
case PLAYER_STATE_JUMP:
|
State *dash_state (StateMachine *sm, Player *player);
|
||||||
printf("JUMP\n");
|
State *jump_state (StateMachine *sm, Player *player);
|
||||||
break;
|
State *fall_state (StateMachine *sm, Player *player);
|
||||||
case PLAYER_STATE_FALL:
|
|
||||||
printf("FALL\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void get_player_controls(Player *player);
|
|
||||||
State *update_state (Player *player, uint32_t state);
|
|
||||||
State *idle_state (StateMachine *sm, Player *player);
|
|
||||||
State *walk_state (StateMachine *sm, Player *player);
|
|
||||||
State *dash_state (StateMachine *sm, Player *player);
|
|
||||||
State *jump_state (StateMachine *sm, Player *player);
|
|
||||||
State *fall_state (StateMachine *sm, Player *player);
|
|
||||||
|
|
||||||
void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
|
void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
|
||||||
player->base_y = position.y;
|
player->base_y = position.y;
|
||||||
@@ -54,7 +42,7 @@ void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
|
|||||||
player->animations[PLAYER_STATE_JUMP] =
|
player->animations[PLAYER_STATE_JUMP] =
|
||||||
ap_init(renderer, "knight_player/Jump_KG_2.png", 100, SPRITE_WIDTH, SPRITE_HEIGHT, false);
|
ap_init(renderer, "knight_player/Jump_KG_2.png", 100, SPRITE_WIDTH, SPRITE_HEIGHT, false);
|
||||||
player->animations[PLAYER_STATE_FALL] =
|
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_IDLE] = (State){ .state_func = (StateFunc *)idle_state };
|
||||||
player->states[PLAYER_STATE_WALK] = (State){ .state_func = (StateFunc *)walk_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) {
|
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_move(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;
|
|
||||||
}
|
|
||||||
ap_update(&(player->animations[player->current_state]), ticks);
|
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);
|
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) {
|
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]);
|
||||||
@@ -122,14 +138,16 @@ 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;
|
||||||
} else if (player->controls.walk_right) {
|
} else if (player->controls.walk_right) {
|
||||||
state = PLAYER_STATE_WALK;
|
state = PLAYER_STATE_WALK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_x_movement(player);
|
||||||
|
|
||||||
return update_state(player, state);
|
return update_state(player, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,18 +155,20 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(player->controls.walk_left) && !(player->controls.walk_right)) {
|
if (!(player->controls.walk_left) && !(player->controls.walk_right)) {
|
||||||
state = PLAYER_STATE_IDLE;
|
state = PLAYER_STATE_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_x_movement(player);
|
||||||
|
|
||||||
return update_state(player, state);
|
return update_state(player, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,12 +176,12 @@ 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;
|
||||||
state = PLAYER_STATE_IDLE;
|
state = PLAYER_STATE_IDLE;
|
||||||
@@ -181,24 +201,26 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reset) {
|
if (reset) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +228,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;
|
||||||
@@ -216,5 +238,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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user