use super::config::EntropyConfig;
use super::hook_ecs::{DefaultEntropyHookECS, EntropyHookECS};
use super::service::EntropyService;
use super::state_ecs::EntropyStateECS;
use super::system_ecs::EntropySystemECS;
use issun_macros::Plugin;
use std::sync::Arc;
#[derive(Plugin)]
#[plugin(name = "issun:entropy_ecs")]
pub struct EntropyPluginECS {
#[plugin(skip)]
hook: Arc<dyn EntropyHookECS>,
#[plugin(resource)]
config: EntropyConfig,
#[plugin(runtime_state)]
state: EntropyStateECS,
#[plugin(service)]
service: EntropyService,
#[plugin(system)]
system: EntropySystemECS,
}
impl EntropyPluginECS {
pub fn new() -> Self {
let hook = Arc::new(DefaultEntropyHookECS);
Self {
hook: hook.clone(),
config: EntropyConfig::default(),
state: EntropyStateECS::default(),
service: EntropyService,
system: EntropySystemECS::new(hook),
}
}
pub fn with_hook<H: EntropyHookECS + 'static>(mut self, hook: H) -> Self {
let hook = Arc::new(hook);
self.hook = hook.clone();
self.system = EntropySystemECS::new(hook);
self
}
pub fn with_config(mut self, config: EntropyConfig) -> Self {
self.config = config;
self
}
pub fn config(&self) -> &EntropyConfig {
&self.config
}
pub fn state(&self) -> &EntropyStateECS {
&self.state
}
pub fn state_mut(&mut self) -> &mut EntropyStateECS {
&mut self.state
}
pub fn system(&self) -> &EntropySystemECS {
&self.system
}
pub fn system_mut(&mut self) -> &mut EntropySystemECS {
&mut self.system
}
}
impl Default for EntropyPluginECS {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::super::types::*;
use super::*;
#[test]
fn test_plugin_creation() {
let plugin = EntropyPluginECS::new();
assert_eq!(plugin.config().global_decay_multiplier, 1.0);
assert_eq!(plugin.state().entity_count(), 0);
}
#[test]
fn test_plugin_with_config() {
let config = EntropyConfig {
global_decay_multiplier: 2.0,
auto_destroy_on_zero: false,
max_decay_events: 500,
..Default::default()
};
let plugin = EntropyPluginECS::new().with_config(config);
assert_eq!(plugin.config().global_decay_multiplier, 2.0);
assert!(!plugin.config().auto_destroy_on_zero);
assert_eq!(plugin.config().max_decay_events, 500);
}
#[tokio::test]
async fn test_plugin_basic_workflow() {
let hook = Arc::new(DefaultEntropyHookECS);
let mut system = EntropySystemECS::new(hook);
let mut state = EntropyStateECS::new();
let config = EntropyConfig::default();
let entity1 = state.spawn_entity(
Durability::new(100.0, 0.01, MaterialType::Metal),
EnvironmentalExposure::default(),
);
let entity2 = state.spawn_entity(
Durability::new(100.0, 0.02, MaterialType::Organic),
EnvironmentalExposure::default(),
);
assert_eq!(state.entity_count(), 2);
system.update_decay(&mut state, &config, 1.0).await;
let dur1 = state.world.get::<&Durability>(entity1).unwrap();
assert!(dur1.current < 100.0);
let dur2 = state.world.get::<&Durability>(entity2).unwrap();
assert!(dur2.current < 100.0);
assert!(dur2.current < dur1.current);
let metrics = state.metrics();
assert_eq!(metrics.entities_processed, 2);
}
#[tokio::test]
async fn test_plugin_repair() {
let hook = Arc::new(DefaultEntropyHookECS);
let mut system = EntropySystemECS::new(hook);
let mut state = EntropyStateECS::new();
let entity = state.spawn_entity(
Durability {
current: 50.0,
max: 100.0,
decay_rate: 0.01,
material: MaterialType::Metal,
status: DurabilityStatus::Worn,
},
EnvironmentalExposure::default(),
);
let repaired = system
.repair_entity(entity, 30.0, &mut state)
.await
.unwrap();
assert_eq!(repaired, 30.0);
let durability = state.world.get::<&Durability>(entity).unwrap();
assert_eq!(durability.current, 80.0);
assert_eq!(durability.status, DurabilityStatus::Intact);
}
}