diff --git a/crates/energy/src/conservation.rs b/crates/energy/src/conservation.rs index 0b38620..6f3e6f3 100644 --- a/crates/energy/src/conservation.rs +++ b/crates/energy/src/conservation.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use forces::prelude::{RotationalWorkEvent, WorkDoneEvent}; /// Enum representing different types of energy #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Component, Reflect)] @@ -86,7 +87,7 @@ pub struct EnergyTransferEvent { /// Component for precise energy accounting #[derive(Component, Debug, Reflect)] #[reflect(Component)] -pub struct EnergyAccountingLedger { +pub struct EnergyBalance { /// History of all transactions, newest first pub transactions: Vec, /// Maximum number of transactions to store @@ -116,7 +117,7 @@ pub struct EnergyTransaction { pub duration: f32, } -impl Default for EnergyAccountingLedger { +impl Default for EnergyBalance { fn default() -> Self { Self { transactions: Vec::new(), @@ -127,7 +128,7 @@ impl Default for EnergyAccountingLedger { } } -impl EnergyAccountingLedger { +impl EnergyBalance { /// Record a new energy transaction pub fn record_transaction(&mut self, transaction: EnergyTransaction) { match transaction.transaction_type { @@ -258,6 +259,72 @@ impl EnergyDriftMonitor { } } +/// System to ensure entities with Mass have energy balance tracking +pub fn initialize_energy_balance( + mut commands: Commands, + query: Query, Without)>, +) { + for entity in query.iter() { + commands.entity(entity).insert(EnergyBalance::default()); + } +} + +/// System to track work done by forces and record in energy balance +pub fn track_work_from_forces( + mut work_events: MessageReader, + mut query: Query<&mut EnergyBalance>, + time: Res