Skip to content
Snippets Groups Projects
choices.rs 1.19 KiB
Newer Older
use std::sync::Arc;

use bevy::prelude::*;
Kat Marchán's avatar
Kat Marchán committed

use crate::{
    actions::{ActionBuilder, ActionBuilderWrapper},
    scorers::{Score, ScorerBuilder},
    thinker::ScorerEnt,
Kat Marchán's avatar
Kat Marchán committed
};

// Contains different types of Considerations and Actions
#[derive(Debug, Clone)]
Kat Marchán's avatar
Kat Marchán committed
pub struct Choice {
    pub(crate) scorer: ScorerEnt,
    pub(crate) action: ActionBuilderWrapper,
Kat Marchán's avatar
Kat Marchán committed
}
impl Choice {
    pub fn calculate(&self, scores: &Query<&Score>) -> f32 {
        scores
            .get(self.scorer.0)
            .expect("Where did the score go?")
            .0
#[derive(Debug)]
Kat Marchán's avatar
Kat Marchán committed
pub struct ChoiceBuilder {
    pub when: Arc<dyn ScorerBuilder>,
    pub then: Arc<dyn ActionBuilder>,
Kat Marchán's avatar
Kat Marchán committed
}
impl ChoiceBuilder {
    pub fn new(scorer: Arc<dyn ScorerBuilder>, action: Arc<dyn ActionBuilder>) -> Self {
        Self {
            when: scorer,
            then: action,
        }
    }

    pub fn build(&self, cmd: &mut Commands, actor: Entity, parent: Entity) -> Choice {
        let scorer_ent = self.when.spawn_scorer(cmd, actor);
        cmd.entity(parent).push_children(&[scorer_ent]);
Kat Marchán's avatar
Kat Marchán committed
        Choice {
            scorer: ScorerEnt(scorer_ent),
            action: ActionBuilderWrapper::new(self.then.clone()),