use bevy::prelude::*;
use super::components::{ActionConfig, ActionPoints};
use super::events::{
ActionConsumedHook, ActionConsumedMessage, ActionsDepletedHook, ActionsResetHook,
ActionsResetMessage, CheckTurnEndMessage, ConsumeActionMessage,
};
use super::systems::{
check_turn_end_all_players, handle_action_consume, handle_action_reset,
on_actions_depleted_check_turn_end,
};
use crate::IssunSet;
pub struct ActionPlugin {
pub config: ActionConfig,
pub enable_default_turn_check: bool,
}
impl Default for ActionPlugin {
fn default() -> Self {
Self {
config: ActionConfig::default(),
enable_default_turn_check: true,
}
}
}
impl ActionPlugin {
pub fn new(config: ActionConfig) -> Self {
Self {
config,
enable_default_turn_check: true,
}
}
pub fn with_config(mut self, config: ActionConfig) -> Self {
self.config = config;
self
}
pub fn without_default_turn_check() -> Self {
Self {
config: ActionConfig::default(),
enable_default_turn_check: false,
}
}
}
impl Plugin for ActionPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(self.config.clone());
app.add_message::<ConsumeActionMessage>()
.add_message::<ActionConsumedMessage>()
.add_message::<ActionsResetMessage>()
.add_message::<CheckTurnEndMessage>();
app.register_type::<ActionPoints>()
.register_type::<ActionConfig>()
.register_type::<ConsumeActionMessage>()
.register_type::<ActionConsumedMessage>()
.register_type::<ActionsResetMessage>()
.register_type::<CheckTurnEndMessage>()
.register_type::<ActionConsumedHook>()
.register_type::<ActionsDepletedHook>()
.register_type::<ActionsResetHook>();
app.add_systems(
Update,
(handle_action_consume, handle_action_reset).in_set(IssunSet::Logic),
);
if self.enable_default_turn_check {
app.add_systems(Update, check_turn_end_all_players.in_set(IssunSet::Logic));
}
app.add_observer(on_actions_depleted_check_turn_end);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plugins::time::TimePlugin;
use crate::IssunCorePlugin;
#[test]
fn test_action_plugin_builds() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(bevy::state::app::StatesPlugin); app.add_plugins(IssunCorePlugin);
app.add_plugins(TimePlugin::default()); app.add_plugins(ActionPlugin::default());
assert!(app.world().get_resource::<ActionConfig>().is_some());
app.update();
}
#[test]
fn test_action_plugin_custom_config() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(bevy::state::app::StatesPlugin); app.add_plugins(IssunCorePlugin);
app.add_plugins(TimePlugin::default()); app.add_plugins(ActionPlugin::new(ActionConfig {
default_max_per_period: 10,
}));
let config = app.world().get_resource::<ActionConfig>().unwrap();
assert_eq!(config.default_max_per_period, 10);
app.update();
}
#[test]
fn test_action_plugin_without_turn_check() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(bevy::state::app::StatesPlugin); app.add_plugins(IssunCorePlugin);
app.add_plugins(TimePlugin::default()); app.add_plugins(ActionPlugin::without_default_turn_check());
app.update();
}
}