Skip to content
Snippets Groups Projects
Unverified Commit 7f8ed12b authored by Kat Marchán's avatar Kat Marchán
Browse files

fix(thinker): disposed of ActiveThinker and circular state-setting

This was, in fact, completely broken for switching actions.
parent 037a7c0d
No related branches found
No related tags found
No related merge requests found
...@@ -135,7 +135,6 @@ impl ActionBuilder for ThinkerBuilder { ...@@ -135,7 +135,6 @@ impl ActionBuilder for ThinkerBuilder {
current_action: None, current_action: None,
}) })
.insert(Name::new("Thinker")) .insert(Name::new("Thinker"))
.insert(ActiveThinker(false))
.insert(ActionState::Requested); .insert(ActionState::Requested);
} }
} }
...@@ -176,9 +175,6 @@ pub fn actor_gone_cleanup( ...@@ -176,9 +175,6 @@ pub fn actor_gone_cleanup(
#[derive(Debug)] #[derive(Debug)]
pub struct HasThinker(Entity); pub struct HasThinker(Entity);
#[derive(Debug)]
pub struct ActiveThinker(bool);
pub struct ThinkerIterations { pub struct ThinkerIterations {
index: usize, index: usize,
max_duration: Duration, max_duration: Duration,
...@@ -200,14 +196,12 @@ impl Default for ThinkerIterations { ...@@ -200,14 +196,12 @@ impl Default for ThinkerIterations {
pub fn thinker_system( pub fn thinker_system(
mut cmd: Commands, mut cmd: Commands,
mut iterations: Local<ThinkerIterations>, mut iterations: Local<ThinkerIterations>,
mut thinker_q: Query<(Entity, &Actor, &mut Thinker, &ActiveThinker)>, mut thinker_q: Query<(Entity, &Actor, &mut Thinker)>,
scores: Query<&Score>, scores: Query<&Score>,
mut action_states: Query<&mut actions::ActionState>, mut action_states: Query<&mut actions::ActionState>,
) { ) {
let start = Instant::now(); let start = Instant::now();
for (thinker_ent, Actor(actor), mut thinker, active_thinker) in for (thinker_ent, Actor(actor), mut thinker) in thinker_q.iter_mut().skip(iterations.index) {
thinker_q.iter_mut().skip(iterations.index)
{
iterations.index += 1; iterations.index += 1;
let thinker_state = action_states let thinker_state = action_states
...@@ -215,21 +209,21 @@ pub fn thinker_system( ...@@ -215,21 +209,21 @@ pub fn thinker_system(
.expect("Where is it?") .expect("Where is it?")
.clone(); .clone();
match thinker_state { match thinker_state {
ActionState::Init | ActionState::Success | ActionState::Failure => { ActionState::Init => {
if let ActiveThinker(true) = active_thinker { let mut act_state = action_states.get_mut(thinker_ent).expect("???");
let mut act_state = action_states.get_mut(thinker_ent).expect("???"); *act_state = ActionState::Requested;
*act_state = ActionState::Requested;
}
} }
ActionState::Requested => {
let mut act_state = action_states.get_mut(thinker_ent).expect("???");
*act_state = ActionState::Executing;
}
ActionState::Success | ActionState::Failure => {}
ActionState::Cancelled => { ActionState::Cancelled => {
if let Some(current) = &mut thinker.current_action { if let Some(current) = &mut thinker.current_action {
let state = action_states.get_mut(current.0.0).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.").clone(); let state = action_states.get_mut(current.0.0).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.").clone();
match state { match state {
ActionState::Success | ActionState::Failure => { ActionState::Success | ActionState::Failure => {
let mut act_state = action_states.get_mut(thinker_ent).expect("???"); cmd.entity(current.0 .0).despawn_recursive();
*act_state = state.clone();
let mut state = action_states.get_mut(current.0.0).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*state = ActionState::Init;
thinker.current_action = None; thinker.current_action = None;
} }
_ => { _ => {
...@@ -237,9 +231,12 @@ pub fn thinker_system( ...@@ -237,9 +231,12 @@ pub fn thinker_system(
*state = ActionState::Cancelled; *state = ActionState::Cancelled;
} }
} }
} else {
let mut act_state = action_states.get_mut(thinker_ent).expect("???");
*act_state = ActionState::Success;
} }
} }
ActionState::Requested | ActionState::Executing => { ActionState::Executing => {
if let Some(choice) = thinker.picker.pick(&thinker.choices, &scores) { if let Some(choice) = thinker.picker.pick(&thinker.choices, &scores) {
// Think about what action we're supposed to be taking. We do this // Think about what action we're supposed to be taking. We do this
// every tick, because we might change our mind. // every tick, because we might change our mind.
...@@ -320,19 +317,14 @@ fn exec_picked_action( ...@@ -320,19 +317,14 @@ fn exec_picked_action(
match *curr_action_state { match *curr_action_state {
ActionState::Executing | ActionState::Requested => { ActionState::Executing | ActionState::Requested => {
*curr_action_state = ActionState::Cancelled; *curr_action_state = ActionState::Cancelled;
let mut thinker_state = states.get_mut(thinker_ent).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*thinker_state = ActionState::Cancelled;
} }
ActionState::Init | ActionState::Success | ActionState::Failure => { ActionState::Init | ActionState::Success | ActionState::Failure => {
let old_state = curr_action_state.clone();
// Despawn the action itself. // Despawn the action itself.
cmd.entity(action_ent.0).despawn_recursive(); cmd.entity(action_ent.0).despawn_recursive();
thinker.current_action = Some(( thinker.current_action = Some((
ActionEnt(picked_action.1.attach(cmd, actor)), ActionEnt(picked_action.1.attach(cmd, actor)),
picked_action.clone(), picked_action.clone(),
)); ));
let mut thinker_state = states.get_mut(thinker_ent).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*thinker_state = old_state;
} }
ActionState::Cancelled => {} ActionState::Cancelled => {}
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment