Start fixing controls
This commit is contained in:
parent
f3b18b8213
commit
ac3b277d05
150
player.c
150
player.c
@ -13,8 +13,6 @@
|
|||||||
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);
|
||||||
State *jump_state(StateMachine *sm, Player *player);
|
|
||||||
State *fall_state(StateMachine *sm, Player *player);
|
|
||||||
State *dash_state(StateMachine *sm, Player *player);
|
State *dash_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) {
|
||||||
@ -27,17 +25,11 @@ void player_init(Player *player, SDL_Renderer *renderer, SDL_Rect position) {
|
|||||||
ap_init(renderer, "knight_player/Idle_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, true);
|
ap_init(renderer, "knight_player/Idle_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, true);
|
||||||
player->animations[PLAYER_STATE_WALK] =
|
player->animations[PLAYER_STATE_WALK] =
|
||||||
ap_init(renderer, "knight_player/Walking_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, true);
|
ap_init(renderer, "knight_player/Walking_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, true);
|
||||||
player->animations[PLAYER_STATE_JUMP] =
|
|
||||||
ap_init(renderer, "knight_player/Jump_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, false);
|
|
||||||
player->animations[PLAYER_STATE_FALL] =
|
|
||||||
ap_init(renderer, "knight_player/Fall_KG_2.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, false);
|
|
||||||
player->animations[PLAYER_STATE_DASH] =
|
player->animations[PLAYER_STATE_DASH] =
|
||||||
ap_init(renderer, "knight_player/Dashing_KG_1.png", 125, SPRITE_WIDTH, SPRITE_HEIGHT, false);
|
ap_init(renderer, "knight_player/Dashing_KG_1.png", 125, 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 };
|
||||||
player->states[PLAYER_STATE_JUMP] = (State){ .state_func = (StateFunc *)jump_state };
|
|
||||||
player->states[PLAYER_STATE_FALL] = (State){ .state_func = (StateFunc *)fall_state };
|
|
||||||
player->states[PLAYER_STATE_DASH] = (State){ .state_func = (StateFunc *)dash_state };
|
player->states[PLAYER_STATE_DASH] = (State){ .state_func = (StateFunc *)dash_state };
|
||||||
|
|
||||||
player->event_data = (PlayerEventData){0};
|
player->event_data = (PlayerEventData){0};
|
||||||
@ -50,55 +42,13 @@ void player_events(Player *player, const SDL_Event *event) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (event->type) {
|
player->event_data.event = event;
|
||||||
case SDL_KEYDOWN:
|
sm_run(&(player->state_machine), (void *)player);
|
||||||
switch (event->key.keysym.sym) {
|
|
||||||
case SDLK_LEFT:
|
|
||||||
player->movement.x = PLAYER_DIRECTION_LEFT * player->velocity;
|
|
||||||
player->x_direction = PLAYER_DIRECTION_LEFT;
|
|
||||||
player->event_data.key = PLAYER_CONTROL_LEFT_DOWN;
|
|
||||||
break;
|
|
||||||
case SDLK_RIGHT:
|
|
||||||
player->movement.x = PLAYER_DIRECTION_RIGHT * player->velocity;
|
|
||||||
player->x_direction = PLAYER_DIRECTION_RIGHT;
|
|
||||||
player->event_data.key = PLAYER_CONTROL_RIGHT_DOWN;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SDL_KEYUP:
|
|
||||||
switch (event->key.keysym.sym) {
|
|
||||||
case SDLK_LEFT:
|
|
||||||
player->movement.x = 0;
|
|
||||||
player->event_data.key = PLAYER_CONTROL_LEFT_UP;
|
|
||||||
break;
|
|
||||||
case SDLK_RIGHT:
|
|
||||||
player->movement.x = 0;
|
|
||||||
player->event_data.key = PLAYER_CONTROL_RIGHT_UP;
|
|
||||||
break;
|
|
||||||
case SDLK_SPACE:
|
|
||||||
player->event_data.key = PLAYER_CONTROL_SPACE;
|
|
||||||
break;
|
|
||||||
case SDLK_LCTRL:
|
|
||||||
player->movement.x = player->x_direction * player->velocity * 3;
|
|
||||||
player->event_data.key = PLAYER_CONTROL_LCTRL;
|
|
||||||
|
|
||||||
if (player->current_state == PLAYER_STATE_JUMP) {
|
|
||||||
player->event_data.dash_in_air = true;
|
|
||||||
} else {
|
|
||||||
player->event_data.dash_in_air = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_update(Player *player, uint32_t ticks) {
|
void player_update(Player *player, uint32_t ticks) {
|
||||||
player->position.x += player->movement.x;
|
player->position.x += player->movement.x;
|
||||||
sm_run(&(player->state_machine), (void *)player);
|
|
||||||
ap_update(&(player->animations[player->current_state]), ticks);
|
ap_update(&(player->animations[player->current_state]), ticks);
|
||||||
player->event_data.key = PLAYER_CONTROL_NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_draw(const Player *player, SDL_Renderer *renderer) {
|
void player_draw(const Player *player, SDL_Renderer *renderer) {
|
||||||
@ -115,61 +65,50 @@ State *update_state(Player *player, uint32_t state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
State *idle_state(StateMachine *sm, Player *player) {
|
State *idle_state(StateMachine *sm, Player *player) {
|
||||||
switch (player->event_data.key) {
|
const SDL_Event *event = player->event_data.event;
|
||||||
case PLAYER_CONTROL_LEFT_DOWN:
|
uint32_t state = PLAYER_STATE_IDLE;
|
||||||
case PLAYER_CONTROL_RIGHT_DOWN:
|
|
||||||
return update_state(player, PLAYER_STATE_WALK);
|
|
||||||
case PLAYER_CONTROL_SPACE:
|
|
||||||
return update_state(player, PLAYER_STATE_JUMP);
|
|
||||||
}
|
|
||||||
|
|
||||||
return update_state(player, PLAYER_STATE_IDLE);
|
switch (event->type) {
|
||||||
}
|
case SDL_KEYDOWN:
|
||||||
|
switch (event->key.keysym.sym) {
|
||||||
State *walk_state(StateMachine *sm, Player *player) {
|
case SDLK_LEFT:
|
||||||
switch (player->event_data.key) {
|
player->movement.x = PLAYER_DIRECTION_LEFT * player->velocity;
|
||||||
case PLAYER_CONTROL_LEFT_UP:
|
player->x_direction = PLAYER_DIRECTION_LEFT;
|
||||||
case PLAYER_CONTROL_RIGHT_UP:
|
state = PLAYER_STATE_WALK;
|
||||||
return update_state(player, PLAYER_STATE_IDLE);
|
break;
|
||||||
case PLAYER_CONTROL_SPACE:
|
case SDLK_RIGHT:
|
||||||
return update_state(player, PLAYER_STATE_JUMP);
|
player->movement.x = PLAYER_DIRECTION_RIGHT * player->velocity;
|
||||||
case PLAYER_CONTROL_LCTRL:
|
player->x_direction = PLAYER_DIRECTION_RIGHT;
|
||||||
return update_state(player, PLAYER_STATE_DASH);
|
state = PLAYER_STATE_WALK;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
return update_state(player, PLAYER_STATE_WALK);
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
State *jump_state(StateMachine *sm, Player *player) {
|
|
||||||
bool reset = false;
|
|
||||||
uint32_t state = PLAYER_STATE_JUMP;
|
|
||||||
|
|
||||||
if (player->animations[PLAYER_STATE_JUMP].finished) {
|
|
||||||
reset = true;
|
|
||||||
state = PLAYER_STATE_FALL;
|
|
||||||
} else if (player->event_data.key == PLAYER_CONTROL_LCTRL) {
|
|
||||||
reset = true;
|
|
||||||
state = PLAYER_STATE_DASH;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reset) {
|
|
||||||
ap_reset(&(player->animations[PLAYER_STATE_JUMP]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return update_state(player, state);
|
return update_state(player, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
State *fall_state(StateMachine *sm, Player *player) {
|
State *walk_state(StateMachine *sm, Player *player) {
|
||||||
bool reset = false;
|
const SDL_Event *event = player->event_data.event;
|
||||||
uint32_t state = PLAYER_STATE_FALL;
|
uint32_t state = PLAYER_STATE_WALK;
|
||||||
|
|
||||||
if (player->animations[PLAYER_STATE_FALL].finished) {
|
switch (event->type) {
|
||||||
reset = true;
|
case SDL_KEYUP:
|
||||||
state = PLAYER_STATE_IDLE;
|
switch (event->key.keysym.sym) {
|
||||||
}
|
case SDLK_LCTRL:
|
||||||
|
player->movement.x = player->x_direction * player->velocity * 3;
|
||||||
if (reset) {
|
state = PLAYER_STATE_DASH;
|
||||||
ap_reset(&(player->animations[PLAYER_STATE_FALL]));
|
break;
|
||||||
|
case SDLK_LEFT:
|
||||||
|
player->movement.x = 0;
|
||||||
|
state = PLAYER_STATE_IDLE;
|
||||||
|
break;
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
player->movement.x = 0;
|
||||||
|
state = PLAYER_STATE_IDLE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return update_state(player, state);
|
return update_state(player, state);
|
||||||
@ -179,17 +118,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 * 10;
|
player->movement.x += player->x_direction * 3;
|
||||||
|
|
||||||
if (player->animations[PLAYER_STATE_DASH].finished) {
|
if (player->animations[PLAYER_STATE_DASH].finished) {
|
||||||
reset = true;
|
|
||||||
player->movement.x = 0;
|
player->movement.x = 0;
|
||||||
|
reset = true;
|
||||||
if (player->event_data.dash_in_air) {
|
state = PLAYER_STATE_IDLE;
|
||||||
state = PLAYER_STATE_FALL;
|
|
||||||
} else {
|
|
||||||
state = PLAYER_STATE_IDLE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reset) {
|
if (reset) {
|
||||||
|
17
player.h
17
player.h
@ -11,25 +11,11 @@
|
|||||||
enum player_states {
|
enum player_states {
|
||||||
PLAYER_STATE_IDLE,
|
PLAYER_STATE_IDLE,
|
||||||
PLAYER_STATE_WALK,
|
PLAYER_STATE_WALK,
|
||||||
PLAYER_STATE_JUMP,
|
|
||||||
PLAYER_STATE_FALL,
|
|
||||||
PLAYER_STATE_DASH,
|
PLAYER_STATE_DASH,
|
||||||
|
|
||||||
COUNT_PLAYER_STATES,
|
COUNT_PLAYER_STATES,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum player_controls {
|
|
||||||
PLAYER_CONTROL_NONE,
|
|
||||||
PLAYER_CONTROL_LEFT_DOWN,
|
|
||||||
PLAYER_CONTROL_RIGHT_DOWN,
|
|
||||||
PLAYER_CONTROL_LEFT_UP,
|
|
||||||
PLAYER_CONTROL_RIGHT_UP,
|
|
||||||
PLAYER_CONTROL_SPACE,
|
|
||||||
PLAYER_CONTROL_LCTRL,
|
|
||||||
|
|
||||||
COUNT_PLAYER_CONTROLS,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum player_direction {
|
enum player_direction {
|
||||||
PLAYER_DIRECTION_LEFT = -1,
|
PLAYER_DIRECTION_LEFT = -1,
|
||||||
PLAYER_DIRECTION_RIGHT = 1,
|
PLAYER_DIRECTION_RIGHT = 1,
|
||||||
@ -39,8 +25,7 @@ enum player_direction {
|
|||||||
|
|
||||||
typedef struct player_event_data PlayerEventData;
|
typedef struct player_event_data PlayerEventData;
|
||||||
struct player_event_data {
|
struct player_event_data {
|
||||||
uint32_t key;
|
const SDL_Event *event;
|
||||||
bool dash_in_air;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct movement Movement;
|
typedef struct movement Movement;
|
||||||
|
Loading…
Reference in New Issue
Block a user