Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
TakeAwayPlayerControl component is inserted instead of removing `Pl…
Browse files Browse the repository at this point in the history
…ayer` component to stop player from moving. (#68)
  • Loading branch information
porkbrain authored Feb 28, 2024
1 parent 346e27e commit bef5617
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
4 changes: 0 additions & 4 deletions common/top_down/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ pub struct Actor {
occupies: Vec<TileIndex>,
/// This information is duplicated.
/// We also have a player component that's assigned to the player entity.
///
/// However, we do sometimes remove the [`Player`] component to take away
/// control from the player.
/// On the other hand, we never change this flag.
is_player: bool,
}

Expand Down
12 changes: 9 additions & 3 deletions common/top_down/src/actor/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ use crate::layout::{TileMap, TopDownScene};
///
/// This information is duplicated.
/// It's also stored on the [`Actor`] component as a flag.
/// However, since we sometimes remove the [`Player`] component to disable
/// player control, using the flag is more reliable.
#[derive(Component, Reflect)]
pub struct Player;

/// If the [`Player`] has this component, the player cannot perform movement
/// actions.
#[derive(Component, Reflect)]
pub struct TakeAwayPlayerControl;

/// Use keyboard to move around the player.
pub fn move_around<T: TopDownScene>(
map: Res<TileMap<T>>,
controls: Res<ActionState<GlobalAction>>,

mut player: Query<(Entity, &mut Actor), With<Player>>,
mut player: Query<
(Entity, &mut Actor),
(With<Player>, Without<TakeAwayPlayerControl>),
>,
) {
// there must be some user action
let Some(action) = controls
Expand Down
41 changes: 22 additions & 19 deletions main_game_lib/src/cutscene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use bevy_pixel_camera::{PixelViewport, PixelZoom};
use common_loading_screen::{LoadingScreenSettings, LoadingScreenState};
use common_store::GlobalStore;
use common_story::portrait_dialog::{DialogRoot, PortraitDialog};
use common_top_down::{Actor, ActorTarget, Player};
use common_top_down::{
actor::player::TakeAwayPlayerControl, Actor, ActorTarget,
};
use common_visuals::{
camera::{order, render_layer, PIXEL_ZOOM},
AtlasAnimation, AtlasAnimationTimer, BeginInterpolationEvent,
Expand Down Expand Up @@ -108,13 +110,13 @@ pub enum CutsceneStep {

/// Simple await until this duration passes.
Sleep(Duration),
/// Removes the [`Player`] component from the player entity.
/// Inserts the [`TakeAwayPlayerControl`] component to the player entity.
/// Ideal for the first step of the cutscene.
RemovePlayerComponent(Entity),
/// Inserts the [`Player`] component to the player entity.
TakeAwayPlayerControl(Entity),
/// Removes the [`TakeAwayPlayerControl`] component from the player entity.
/// This gives the player back control, ideal for the last step of the
/// cutscene.
AddPlayerComponent(Entity),
ReturnPlayerControl(Entity),
/// Inserts [`AtlasAnimationTimer`] component to the given entity.
/// This typically is used to start an animation when the entity has
/// [`AtlasAnimation`] component.
Expand Down Expand Up @@ -402,8 +404,8 @@ static CUTSCENE_SYSTEMS: OnceLock<CutsceneSystems> = OnceLock::new();

struct CutsceneSystems {
if_true_this_else_that: SystemId,
remove_player_component: SystemId,
add_player_component: SystemId,
take_away_player_control: SystemId,
return_player_control: SystemId,
sleep: SystemId,
insert_atlas_animation_timer_to: SystemId,
change_global_state: SystemId,
Expand All @@ -421,8 +423,9 @@ impl CutsceneSystems {
fn register_systems(w: &mut World) -> Self {
Self {
if_true_this_else_that: w.register_system(if_true_this_else_that),
remove_player_component: w.register_system(remove_player_component),
add_player_component: w.register_system(add_player_component),
take_away_player_control: w
.register_system(take_away_player_control),
return_player_control: w.register_system(return_player_control),
sleep: w.register_system(sleep),
insert_atlas_animation_timer_to: w
.register_system(insert_atlas_animation_timer_to),
Expand Down Expand Up @@ -454,8 +457,8 @@ fn system_id(step: &CutsceneStep) -> SystemId {
let s = CUTSCENE_SYSTEMS.get().unwrap();
match step {
IfTrueThisElseThat(_, _, _) => s.if_true_this_else_that,
RemovePlayerComponent(_) => s.remove_player_component,
AddPlayerComponent(_) => s.add_player_component,
TakeAwayPlayerControl(_) => s.take_away_player_control,
ReturnPlayerControl(_) => s.return_player_control,
Sleep(_) => s.sleep,
InsertAtlasAnimationTimerTo { .. } => s.insert_atlas_animation_timer_to,
ChangeGlobalState { .. } => s.change_global_state,
Expand All @@ -470,24 +473,24 @@ fn system_id(step: &CutsceneStep) -> SystemId {
}
}

fn remove_player_component(mut cmd: Commands, mut cutscene: ResMut<Cutscene>) {
fn take_away_player_control(mut cmd: Commands, mut cutscene: ResMut<Cutscene>) {
let step = &cutscene.sequence[cutscene.sequence_index];
let CutsceneStep::RemovePlayerComponent(entity) = &step else {
panic!("Expected RemovePlayerComponent step, got {step}");
let CutsceneStep::TakeAwayPlayerControl(entity) = &step else {
panic!("Expected TakeAwayPlayerControl step, got {step}");
};

cmd.entity(*entity).remove::<Player>();
cmd.entity(*entity).insert(TakeAwayPlayerControl);

cutscene.schedule_next_step_or_despawn(&mut cmd);
}

fn add_player_component(mut cmd: Commands, mut cutscene: ResMut<Cutscene>) {
fn return_player_control(mut cmd: Commands, mut cutscene: ResMut<Cutscene>) {
let step = &cutscene.sequence[cutscene.sequence_index];
let CutsceneStep::AddPlayerComponent(entity) = &step else {
panic!("Expected AddPlayerComponent step, got {step}");
let CutsceneStep::ReturnPlayerControl(entity) = &step else {
panic!("Expected ReturnPlayerControl step, got {step}");
};

cmd.entity(*entity).insert(Player);
cmd.entity(*entity).remove::<TakeAwayPlayerControl>();

cutscene.schedule_next_step_or_despawn(&mut cmd);
}
Expand Down
5 changes: 2 additions & 3 deletions scenes/apartment/src/actor/cutscenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ impl IntoCutscene for EnterTheElevator {
} = self;

vec![
// take away player control
RemovePlayerComponent(player),
TakeAwayPlayerControl(player),
SetActorFacingDirection(player, GridDirection::Top),
// move camera to the player
BeginMovingEntity {
Expand Down Expand Up @@ -102,7 +101,7 @@ impl IntoCutscene for EnterTheElevator {
over: from_millis(1000),
animation_curve: Some(EASE_IN_OUT.clone()),
},
AddPlayerComponent(player),
ReturnPlayerControl(player),
]),
),
]
Expand Down

0 comments on commit bef5617

Please sign in to comment.