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 jump_pressed;
uint8_t is_crouch; uint8_t is_crouch;
bool ladder_state; bool ladder_state;
bool locked; // Whether to respond to inputs
} CPlayerState_t; } CPlayerState_t;
typedef enum ContainerItem { typedef enum ContainerItem {

View File

@ -23,5 +23,6 @@ typedef enum ActionType
ACTION_SPAWN_TILE, ACTION_SPAWN_TILE,
ACTION_REMOVE_TILE, ACTION_REMOVE_TILE,
ACTION_SWITCH_TILESET, ACTION_SWITCH_TILESET,
ACTION_LOOKAHEAD,
}ActionType_t; }ActionType_t;
#endif // __ACTIONS_H #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: case ACTION_REMOVE_TILE:
toggle_block_system(scene, action, pressed); toggle_block_system(scene, action, pressed);
update_entity_manager(&scene->ent_manager); update_entity_manager(&scene->ent_manager);
default:
break; break;
case ACTION_SWITCH_TILESET: case ACTION_SWITCH_TILESET:
if (!pressed) 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]); data->solid_tile_sprites = get_sprite(&scene->engine->assets, SOLID_TILE_SELECTIONS[data->selected_solid_tilemap]);
} }
break; 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_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_L, ACTION_EXIT);
sc_map_put_64(&scene->scene.action_map, KEY_R, ACTION_RESTART); 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_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_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_LEFT_BUTTON, ACTION_SPAWN_TILE);
sc_map_put_64(&scene->scene.action_map, MOUSE_RIGHT_BUTTON, ACTION_REMOVE_TILE); sc_map_put_64(&scene->scene.action_map, MOUSE_RIGHT_BUTTON, ACTION_REMOVE_TILE);

View File

@ -297,6 +297,9 @@ void player_movement_input_system(Scene_t* scene)
// Ladder handling // Ladder handling
if (!p_pstate->ladder_state) if (!p_pstate->ladder_state)
{
// Transit into ladder state if possible
if (!p_pstate->locked)
{ {
if (p_pstate->player_dir.y < 0) if (p_pstate->player_dir.y < 0)
{ {
@ -338,6 +341,7 @@ void player_movement_input_system(Scene_t* scene)
} }
} }
} }
}
else else
{ {
unsigned int tile_x = (p_player->position.x + p_bbox->half_size.x) / TILE_SIZE; unsigned int tile_x = (p_player->position.x + p_bbox->half_size.x) / TILE_SIZE;
@ -353,10 +357,11 @@ void player_movement_input_system(Scene_t* scene)
p_pstate->ladder_state |= tilemap.tiles[tile_idx].tile_type == LADDER; p_pstate->ladder_state |= tilemap.tiles[tile_idx].tile_type == LADDER;
} }
} }
if (p_pstate->ladder_state) if (p_pstate->ladder_state)
{ {
p_ctransform->velocity.y = p_pstate->player_dir.y * 150; 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_ctransform->velocity.x = p_pstate->player_dir.x * 40 * (p_pstate->locked ? 0 : 1);
if (p_pstate->player_dir.y != 0) if (p_pstate->player_dir.y != 0)
{ {
p_player->position.x = tile_x * TILE_SIZE + 1; p_player->position.x = tile_x * TILE_SIZE + 1;
@ -365,40 +370,13 @@ void player_movement_input_system(Scene_t* scene)
} }
bool in_water = (p_mstate->water_state & 1); bool in_water = (p_mstate->water_state & 1);
p_pstate->is_crouch |= (p_pstate->player_dir.y > 0)? 0b10 : 0; if (p_pstate->locked)
if (!in_water)
{ {
p_pstate->is_crouch |= (p_pstate->is_crouch & 1)? 0b10 : 0;
p_pstate->player_dir.y = 0;
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL);
} }
else else
{ {
// Although this can be achieved via higher friction, i'll explain away as the player is not p_pstate->is_crouch |= (p_pstate->player_dir.y > 0)? 0b10 : 0;
// good with swimming, resulting in lower movement acceleration
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL/1.2);
}
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)
{
p_cjump->jump_released = true;
if (!p_cjump->short_hop && p_ctransform->velocity.y < 0)
{
p_cjump->short_hop = true;
p_ctransform->velocity.y /= 2;
}
}
}
Vector2 point_to_check = Vector2Add( Vector2 point_to_check = Vector2Add(
p_player->position, p_player->position,
(Vector2){0, p_bbox->size.y - PLAYER_HEIGHT} (Vector2){0, p_bbox->size.y - PLAYER_HEIGHT}
@ -411,11 +389,47 @@ void player_movement_input_system(Scene_t* scene)
p_pstate->is_crouch |= 0b10; p_pstate->is_crouch |= 0b10;
} }
if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01; if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01;
p_pstate->is_crouch >>= 1; }
if (!in_water)
{
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 || p_pstate->locked)
{
p_cjump->jump_released = true;
if (!p_cjump->short_hop && p_ctransform->velocity.y < 0)
{
p_cjump->short_hop = true;
p_ctransform->velocity.y /= 2;
}
}
}
// Jumps is possible as long as you have a jump // Jumps is possible as long as you have a jump
// Check if possible to jump when jump is pressed // Check if possible to jump when jump is pressed
if (!p_pstate->locked)
{
if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready) if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
{ {
play_sfx(scene->engine, PLAYER_JMP_SFX); play_sfx(scene->engine, PLAYER_JMP_SFX);
@ -451,6 +465,10 @@ void player_movement_input_system(Scene_t* scene)
play_sfx_pitched(scene->engine, PLAYER_LAND_SFX, 0.8f + rand() * 0.4f / (float)RAND_MAX); 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;
}
} }
void player_bbox_update_system(Scene_t* scene) void player_bbox_update_system(Scene_t* scene)