use bevy::prelude::*;
use crate::{actions::ActionContext, types::{self, RequestKey}};
#[derive(EntityEvent, Debug)]
pub struct AiActionPicked {
pub entity: Entity,
pub action_key: crate::types::ActionKey,
pub action_name: String,
pub action_context: crate::types::ActionContextRef,
pub action_score: crate::types::ActionScore,
pub request_key: Option<RequestKey>,
}
impl AiActionPicked {
pub fn new(
ai_owner: Entity,
action_key: crate::types::ActionKey,
action_name: String,
action_context: ActionContext,
action_score: crate::types::ActionScore,
request_key: Option<RequestKey>,
) -> Self {
#[cfg(feature = "logging")]
bevy::log::debug!(
"Creating a new AiActionPicked event for {:?} with key {:?} ({:?})",
ai_owner,
action_key,
action_name
);
let wrapped_ctx = action_context;
Self {
entity: ai_owner,
action_key: action_key,
action_name: action_name,
action_context: wrapped_ctx,
action_score: action_score,
request_key: request_key,
}
}
}
#[derive(Event, Debug)]
pub struct SomeAiDecisionProcessed;
#[derive(EntityEvent)]
pub struct AiDecisionRequested {
pub entity: types::AiEntity,
pub request_key: Option<RequestKey>,
pub smart_objects: Option<crate::types::SmartObjects>,
}
#[derive(EntityEvent)]
pub struct AiDecisionInitiated {
pub entity: types::AiEntity,
pub request_key: Option<RequestKey>,
pub smart_objects: Option<crate::types::SmartObjects>,
}
#[derive(Message, Debug)]
pub struct AiActionDispatchToUserCode {
pub entity: Entity,
pub action_key: crate::types::ActionKey,
pub action_name: String,
pub action_context: crate::types::ActionContextRef,
pub action_score: crate::types::ActionScore,
}
impl AiActionDispatchToUserCode {
pub fn new(
ai_owner: Entity,
action_key: crate::types::ActionKey,
action_name: String,
action_context: ActionContext,
action_score: crate::types::ActionScore,
) -> Self {
#[cfg(feature = "logging")]
bevy::log::debug!(
"Creating a new AiActionDispatchToUserCode event for {:?} with key {:?} ({:?})",
ai_owner,
action_key,
action_name
);
let wrapped_ctx = action_context;
Self {
entity: ai_owner,
action_key: action_key,
action_name: action_name,
action_context: wrapped_ctx,
action_score: action_score,
}
}
}
#[derive(Message)]
pub struct NoDecisionMessage {
pub entity: Option<Entity>,
pub request_key: Option<RequestKey>,
pub comment: Option<&'static str>,
}
#[cfg(test)]
mod tests {
#[cfg(feature = "logging")]
use bevy::log::LogPlugin;
use bevy::{app::ScheduleRunnerPlugin, prelude::*};
use super::*;
use crate::ai::AIController;
#[derive(Component)]
struct TestContextData {
_foo: u8,
_bar: i8,
}
#[derive(Debug, Default, Event)]
struct TestActionEvent;
fn setup_test_entity(
mut commands: Commands,
) {
let entity_cmds = commands.spawn(
AIController::default()
);
let entity = entity_cmds.id();
let ctx2 = commands.spawn(
TestContextData {
_foo: 1,
_bar: 2,
}
).id();
commands.trigger(AiActionPicked {
action_name: "TestAction".into(),
action_key: "TestActionEvent".into(),
action_context: ctx2,
action_score: 1.,
entity: entity.into(),
request_key: None,
});
}
fn dispatch_events(
trigger: On<AiActionPicked>,
mut commands: Commands,
) {
let evt = trigger.event();
let actionkey = evt.action_key.as_str();
match actionkey {
"TestActionEvent" => { commands.trigger(TestActionEvent) }
_ => { panic!("Unrecognized Action Key: {}", actionkey) }
};
}
fn handle_event(
trigger: On<TestActionEvent>
) {
let evt = trigger.event();
#[cfg(feature = "logging")]
bevy::log::debug!("Processing event {:?}", evt);
}
#[test]
fn test_run_action() {
let mut app = App::new();
app
.add_plugins((
MinimalPlugins.set(ScheduleRunnerPlugin::run_once()),
#[cfg(feature = "logging")]
LogPlugin {
level: bevy::log::Level::DEBUG,
custom_layer: |_| None,
filter: "wgpu=error,bevy_render=info,bevy_ecs=info".to_string(),
fmt_layer: |_| None,
}
))
.add_systems(Startup, setup_test_entity)
.add_observer(dispatch_events)
.add_observer(handle_event)
;
app.run();
}
}