use crate::{condition::*, decision_makers::*, task::*};
pub struct SelectorState<M = ()> {
condition: Box<dyn Condition<M>>,
task: Box<dyn Task<M>>,
}
impl<M> SelectorState<M> {
pub fn new<C, T>(condition: C, task: T) -> Self
where
C: Condition<M> + 'static,
T: Task<M> + 'static,
{
Self {
condition: Box::new(condition),
task: Box::new(task),
}
}
pub fn new_raw(condition: Box<dyn Condition<M>>, task: Box<dyn Task<M>>) -> Self {
Self { condition, task }
}
}
impl<M> std::fmt::Debug for SelectorState<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SelectorState").finish()
}
}
pub struct Selector<M = ()> {
states: Vec<SelectorState<M>>,
active_index: Option<usize>,
}
impl<M> Selector<M> {
pub fn new(states: Vec<SelectorState<M>>) -> Self {
Self {
states,
active_index: None,
}
}
pub fn new_raw(states: Vec<SelectorState<M>>) -> Self {
Self {
states,
active_index: None,
}
}
pub fn active_index(&self) -> Option<usize> {
self.active_index
}
pub fn reset(&mut self, memory: &mut M, forced: bool) -> bool {
if let Some(index) = self.active_index {
let state = self.states.get_mut(index).unwrap();
if !forced && state.task.is_locked(memory) {
return false;
}
state.task.on_exit(memory);
self.active_index = None;
}
true
}
fn change_active_index(&mut self, index: Option<usize>, memory: &mut M) -> bool {
if index == self.active_index {
return false;
}
if let Some(index) = self.active_index {
let state = self.states.get_mut(index).unwrap();
if state.task.is_locked(memory) {
return false;
}
state.task.on_exit(memory);
}
if let Some(index) = index {
self.states.get_mut(index).unwrap().task.on_enter(memory);
}
self.active_index = index;
true
}
pub fn process(&mut self, memory: &mut M) -> bool {
if self.states.is_empty() {
return false;
}
let index = self
.states
.iter()
.position(|state| state.condition.validate(memory));
if self.change_active_index(index, memory) {
return true;
}
if let Some(index) = self.active_index {
return self.states.get_mut(index).unwrap().task.on_process(memory);
}
false
}
pub fn update(&mut self, memory: &mut M) {
if let Some(index) = self.active_index {
self.states.get_mut(index).unwrap().task.on_update(memory);
}
}
}
impl<M, K> DecisionMaker<M, K> for Selector<M>
where
K: Default,
{
fn decide(&mut self, memory: &mut M) -> Option<K> {
self.process(memory);
Some(K::default())
}
fn change_mind(&mut self, _: Option<K>, memory: &mut M) -> bool {
self.reset(memory, true)
}
}
impl<M> Task<M> for Selector<M> {
fn is_locked(&self, memory: &M) -> bool {
if let Some(index) = self.active_index
&& let Some(state) = self.states.get(index)
{
return state.task.is_locked(memory);
}
false
}
fn on_enter(&mut self, memory: &mut M) {
self.reset(memory, true);
self.process(memory);
}
fn on_exit(&mut self, memory: &mut M) {
self.reset(memory, true);
}
fn on_update(&mut self, memory: &mut M) {
self.update(memory);
}
fn on_process(&mut self, memory: &mut M) -> bool {
self.process(memory)
}
}
impl<M> std::fmt::Debug for Selector<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Selector")
.field("states", &self.states)
.field("active_index", &self.active_index)
.finish()
}
}