use bevy::prelude::*;
#[cfg(any(feature = "actionset_loader"))]
use serde::{Serialize, Deserialize};
use crate::considerations::ConsiderationData;
use crate::types::{self, ActionContextRef, CraniumList, CraniumKvMap};
use crate::identifiers::{ContextFetcherIdentifier};
pub type ActionContext = Entity;
pub type ActionKey = String;
#[derive(Clone, Reflect, Debug)]
pub struct Action {
pub name: String,
pub context: ActionContextRef,
pub action_key: ActionKey,
}
#[derive(Clone, Reflect, Debug)]
pub struct ScoredAction {
pub action: Action,
pub score: types::ActionScore,
}
#[derive(Debug, Clone, Reflect)]
#[cfg_attr(any(feature = "actionset_loader"), derive(Serialize, Deserialize))]
pub struct ActionTemplate {
pub name: String,
#[cfg_attr(any(feature = "actionset_loader"), serde(rename="context_fetcher"))]
pub context_fetcher_name: ContextFetcherIdentifier,
pub considerations: CraniumList<ConsiderationData>,
pub priority: types::ActionScore,
pub action_key: ActionKey,
pub lod_min: Option<types::AiLodLevelPrimitive>,
pub lod_max: Option<types::AiLodLevelPrimitive>,
}
impl ActionTemplate {
pub fn new<
INS: Into<String>,
CFN: Into<ContextFetcherIdentifier>,
IAK: Into<String>,
>(
name: INS,
context_fetcher_name: CFN,
considerations: CraniumList<ConsiderationData>,
priority: types::ActionScore,
action_key: IAK,
lod_min: Option<types::AiLodLevelPrimitive>,
lod_max: Option<types::AiLodLevelPrimitive>,
) -> Self {
Self {
name: name.into(),
context_fetcher_name: context_fetcher_name.into(),
considerations: considerations,
priority: priority,
action_key: action_key.into(),
lod_min: lod_min,
lod_max: lod_max,
}
}
pub fn is_within_lod_range(&self, lod: &Option<crate::lods::AiLevelOfDetailValue>) -> bool {
let qry_lod = lod.map(|lv| lv.to_primitive()).unwrap_or(crate::lods::LOD_NORMAL);
let min = self.lod_min.unwrap_or(crate::lods::LOD_NORMAL);
let max = self.lod_min.unwrap_or(crate::lods::LOD_MINIMAL);
qry_lod >= min && qry_lod <= max
}
}
impl core::hash::Hash for ActionTemplate {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.action_key.hash(state);
}
}
impl PartialEq for ActionTemplate {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.action_key == other.action_key
}
}
impl Eq for ActionTemplate {}
pub type ActionHandlerInputs = (
types::AiEntity,
types::PawnEntityRef,
types::ActionContextRef,
);
pub type ActionHandlerOutputs = ();
pub type ActionHandlerFn = dyn Send + Sync + FnMut(ActionHandlerInputs, Commands) -> ();
pub trait ActionTriggerEvent: Event {
fn build_from_action_handler_inputs(inputs: ActionHandlerInputs) -> Self;
fn trigger_from_action_handler(inputs: ActionHandlerInputs, commands: Commands);
}
pub struct ActionPickCallback(Box<ActionHandlerFn>);
impl ActionPickCallback {
pub fn new<F: Send + Sync + FnMut(ActionHandlerInputs, Commands) -> () + 'static>(func: F) -> Self {
Self(Box::new(func))
}
pub fn call(&mut self, inputs: ActionHandlerInputs, commands: Commands) {
self.0(inputs, commands)
}
}
impl<F: 'static + Send + Sync + FnMut(ActionHandlerInputs, Commands)> From<F> for ActionPickCallback {
fn from(value: F) -> Self {
Self::new(value)
}
}
#[derive(Default, Resource)]
pub struct ActionHandlerKeyToSystemMap {
pub mapping: CraniumKvMap<
types::ActionKey,
ActionPickCallback,
>
}
pub trait AcceptsActionHandlerRegistrations {
fn register_action_handler<
ITF: Into<ActionPickCallback>,
IS: Into<String>,
>(
&mut self,
trigger_fn: ITF,
key: IS,
) -> &mut Self;
}
impl AcceptsActionHandlerRegistrations for App {
fn register_action_handler<
ITF: Into<ActionPickCallback>,
IS: Into<String>,
>(
&mut self,
trigger_fn: ITF,
key: IS,
) -> &mut Self{
self.world_mut().register_action_handler(trigger_fn, key);
self
}
}
impl AcceptsActionHandlerRegistrations for World {
fn register_action_handler<
ITF: Into<ActionPickCallback>,
IS: Into<String>,
>(
&mut self,
trigger_fn: ITF,
key: IS,
) -> &mut Self {
let system_key: ActionKey = key.into();
let callback = trigger_fn.into();
let cell = self.as_unsafe_world_cell();
let mut system_registry = match unsafe { cell.world_mut() }.get_non_send_resource_mut::<ActionHandlerKeyToSystemMap>() {
Some(registry) => registry,
None => {
unsafe { cell.world_mut() }.init_resource::<ActionHandlerKeyToSystemMap>();
unsafe { cell.world_mut() }.get_resource_mut::<ActionHandlerKeyToSystemMap>().unwrap()
}
};
let old = system_registry.mapping.insert(
system_key.to_owned(),
callback
);
match old {
None => {},
Some(_) => {
#[cfg(feature = "logging")]
bevy::log::warn!(
"Detected a key collision for key {:?}. Ejecting previous registration...",
system_key
);
}
}
self
}
}
pub struct ActionHandlerPlugin;
impl Plugin for ActionHandlerPlugin {
fn build(&self, app: &mut App) {
app
.init_resource::<ActionHandlerKeyToSystemMap>()
;
}
}