Add key to toggle player movement

This key will be used for looking ahead
main
En Yi 2024-08-17 13:58:06 +08:00
parent e762f62f40
commit 9fdb8296ff
4 changed files with 108 additions and 84 deletions

View File

@ -91,6 +91,7 @@ typedef struct _CPlayerState_t {
uint8_t jump_pressed;
uint8_t is_crouch;
bool ladder_state;
bool locked; // Whether to respond to inputs
} CPlayerState_t;
typedef enum ContainerItem {

View File

@ -23,5 +23,6 @@ typedef enum ActionType
ACTION_SPAWN_TILE,
ACTION_REMOVE_TILE,
ACTION_SWITCH_TILESET,
ACTION_LOOKAHEAD,
}ActionType_t;
#endif // __ACTIONS_H

View File

@ -1022,7 +1022,6 @@ static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
case ACTION_REMOVE_TILE:
toggle_block_system(scene, action, pressed);
update_entity_manager(&scene->ent_manager);
default:
break;
case ACTION_SWITCH_TILESET:
if (!pressed)
@ -1033,6 +1032,10 @@ static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
data->solid_tile_sprites = get_sprite(&scene->engine->assets, SOLID_TILE_SELECTIONS[data->selected_solid_tilemap]);
}
break;
case ACTION_LOOKAHEAD:
p_playerstate->locked = pressed;
default:
break;
}
}
}
@ -1321,10 +1324,11 @@ void init_sandbox_scene(LevelScene_t* scene)
sc_map_put_64(&scene->scene.action_map, KEY_T, ACTION_CRATE_ACTIVATION);
sc_map_put_64(&scene->scene.action_map, KEY_L, ACTION_EXIT);
sc_map_put_64(&scene->scene.action_map, KEY_R, ACTION_RESTART);
sc_map_put_64(&scene->scene.action_map, KEY_B, ACTION_TOGGLE_GRID);
sc_map_put_64(&scene->scene.action_map, KEY_Z, ACTION_SWITCH_TILESET);
sc_map_put_64(&scene->scene.action_map, KEY_V, ACTION_SET_SPAWNPOINT);
sc_map_put_64(&scene->scene.action_map, KEY_X, ACTION_TOGGLE_GRID);
sc_map_put_64(&scene->scene.action_map, KEY_C, ACTION_SET_SPAWNPOINT);
sc_map_put_64(&scene->scene.action_map, KEY_U, ACTION_TOGGLE_TIMESLOW);
sc_map_put_64(&scene->scene.action_map, KEY_V, ACTION_LOOKAHEAD);
sc_map_put_64(&scene->scene.action_map, MOUSE_LEFT_BUTTON, ACTION_SPAWN_TILE);
sc_map_put_64(&scene->scene.action_map, MOUSE_RIGHT_BUTTON, ACTION_REMOVE_TILE);

View File

@ -298,43 +298,47 @@ void player_movement_input_system(Scene_t* scene)
// Ladder handling
if (!p_pstate->ladder_state)
{
if (p_pstate->player_dir.y < 0)
// Transit into ladder state if possible
if (!p_pstate->locked)
{
unsigned int tile_idx = get_tile_idx(
p_player->position.x + p_bbox->half_size.x,
p_player->position.y + p_bbox->half_size.y,
data->tilemap
);
if (tilemap.tiles[tile_idx].tile_type == LADDER && p_ctransform->velocity.y >= 0)
if (p_pstate->player_dir.y < 0)
{
p_pstate->ladder_state = true;
p_player->position.y--;
}
}
else if (p_pstate->player_dir.y > 0)
{
unsigned int tile_idx;
if (p_mstate->ground_state & 1)
{
tile_idx = get_tile_idx(
p_player->position.x + p_bbox->half_size.x,
p_player->position.y + p_bbox->size.y,
data->tilemap
);
}
else
{
tile_idx = get_tile_idx(
unsigned int tile_idx = get_tile_idx(
p_player->position.x + p_bbox->half_size.x,
p_player->position.y + p_bbox->half_size.y,
data->tilemap
);
if (tilemap.tiles[tile_idx].tile_type == LADDER && p_ctransform->velocity.y >= 0)
{
p_pstate->ladder_state = true;
p_player->position.y--;
}
}
if (tile_idx < tilemap.n_tiles && tilemap.tiles[tile_idx].tile_type == LADDER)
else if (p_pstate->player_dir.y > 0)
{
p_pstate->ladder_state = true;
p_player->position.y++;
unsigned int tile_idx;
if (p_mstate->ground_state & 1)
{
tile_idx = get_tile_idx(
p_player->position.x + p_bbox->half_size.x,
p_player->position.y + p_bbox->size.y,
data->tilemap
);
}
else
{
tile_idx = get_tile_idx(
p_player->position.x + p_bbox->half_size.x,
p_player->position.y + p_bbox->half_size.y,
data->tilemap
);
}
if (tile_idx < tilemap.n_tiles && tilemap.tiles[tile_idx].tile_type == LADDER)
{
p_pstate->ladder_state = true;
p_player->position.y++;
}
}
}
}
@ -353,10 +357,11 @@ void player_movement_input_system(Scene_t* scene)
p_pstate->ladder_state |= tilemap.tiles[tile_idx].tile_type == LADDER;
}
}
if (p_pstate->ladder_state)
{
p_ctransform->velocity.y = p_pstate->player_dir.y * 150;
p_ctransform->velocity.x = p_pstate->player_dir.x * 40;
p_ctransform->velocity.y = p_pstate->player_dir.y * 150 * (p_pstate->locked ? 0 : 1);
p_ctransform->velocity.x = p_pstate->player_dir.x * 40 * (p_pstate->locked ? 0 : 1);
if (p_pstate->player_dir.y != 0)
{
p_player->position.x = tile_x * TILE_SIZE + 1;
@ -365,30 +370,50 @@ void player_movement_input_system(Scene_t* scene)
}
bool in_water = (p_mstate->water_state & 1);
p_pstate->is_crouch |= (p_pstate->player_dir.y > 0)? 0b10 : 0;
if (!in_water)
if (p_pstate->locked)
{
p_pstate->player_dir.y = 0;
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL);
p_pstate->is_crouch |= (p_pstate->is_crouch & 1)? 0b10 : 0;
}
else
{
// Although this can be achieved via higher friction, i'll explain away as the player is not
// good with swimming, resulting in lower movement acceleration
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL/1.2);
p_pstate->is_crouch |= (p_pstate->player_dir.y > 0)? 0b10 : 0;
Vector2 point_to_check = Vector2Add(
p_player->position,
(Vector2){0, p_bbox->size.y - PLAYER_HEIGHT}
);
uint8_t collide_type = check_collision_at(
p_player, point_to_check, p_bbox->size, &tilemap
);
if (collide_type == 1)
{
p_pstate->is_crouch |= 0b10;
}
if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01;
}
if (p_pstate->is_crouch & 1)
if (!in_water)
{
p_ctransform->accel = Vector2Scale(p_ctransform->accel, 0.5);
p_pstate->player_dir.y = 0;
}
if (!p_pstate->locked)
{
// Although this can be achieved via higher friction, i'll explain away as the player is not
// good with swimming, resulting in lower movement acceleration
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL / (in_water ? 1.2f : 1.0f));
if (p_pstate->is_crouch & 1)
{
p_ctransform->accel = Vector2Scale(p_ctransform->accel, 0.5);
}
}
// Short Hop
// Jumped check is needed to make sure it is applied on jumps, not generally
// One issue caused is lower velocity in water
if (p_cjump->jumped)
{
if (!p_pstate->jump_pressed)
if (!p_pstate->jump_pressed || p_pstate->locked)
{
p_cjump->jump_released = true;
if (!p_cjump->short_hop && p_ctransform->velocity.y < 0)
@ -399,57 +424,50 @@ void player_movement_input_system(Scene_t* scene)
}
}
Vector2 point_to_check = Vector2Add(
p_player->position,
(Vector2){0, p_bbox->size.y - PLAYER_HEIGHT}
);
uint8_t collide_type = check_collision_at(
p_player, point_to_check, p_bbox->size, &tilemap
);
if (collide_type == 1)
{
p_pstate->is_crouch |= 0b10;
}
if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01;
p_pstate->is_crouch >>= 1;
// Jumps is possible as long as you have a jump
// Check if possible to jump when jump is pressed
if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
if (!p_pstate->locked)
{
play_sfx(scene->engine, PLAYER_JMP_SFX);
p_cjump->jumps--;
if (!in_water)
if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
{
if (p_mstate->ground_state & 1 || p_cjump->coyote_timer > 0)
play_sfx(scene->engine, PLAYER_JMP_SFX);
p_cjump->jumps--;
if (!in_water)
{
p_ctransform->velocity.y = -p_cjump->jump_speed;
if (p_mstate->ground_state & 1 || p_cjump->coyote_timer > 0)
{
p_ctransform->velocity.y = -p_cjump->jump_speed;
}
else if (p_pstate->ladder_state)
{
p_ctransform->velocity.y = -p_cjump->jump_speed / 1.4;
}
}
else if (p_pstate->ladder_state)
else
{
p_ctransform->velocity.y = -p_cjump->jump_speed / 1.4;
p_ctransform->velocity.y = -p_cjump->jump_speed / 1.75;
}
}
else
{
p_ctransform->velocity.y = -p_cjump->jump_speed / 1.75;
}
p_pstate->ladder_state = false;
p_cjump->coyote_timer = 0;
p_cjump->jumped = true;
p_cjump->jump_ready = false;
p_cjump->jump_released = false;
}
else if (p_mstate->ground_state == 0b01)
{
// the else if check is to prevent playing the landing sfx
// on first frame jumps
// This is also why this is done here instead of in the
// state transition function
play_sfx_pitched(scene->engine, PLAYER_LAND_SFX, 0.8f + rand() * 0.4f / (float)RAND_MAX);
p_pstate->ladder_state = false;
p_cjump->coyote_timer = 0;
p_cjump->jumped = true;
p_cjump->jump_ready = false;
p_cjump->jump_released = false;
}
else if (p_mstate->ground_state == 0b01)
{
// the else if check is to prevent playing the landing sfx
// on first frame jumps
// This is also why this is done here instead of in the
// state transition function
play_sfx_pitched(scene->engine, PLAYER_LAND_SFX, 0.8f + rand() * 0.4f / (float)RAND_MAX);
}
}
// Post movement state update
p_pstate->is_crouch >>= 1;
}
}