//! This example shows how to send, mutate, and receive, messages. It also demonstrates //! how to control system ordering so that messages are processed in a specific order. //! It does this by simulating a damage over time effect that you might find in a game. use bevy::prelude::*; // In order to send or receive messages first you must define them // This message should be sent when something attempts to deal damage to another entity. #[derive(Message, Debug)] struct DealDamage { pub amount: i32, } // This message should be sent when an entity receives damage. #[derive(Message, Debug, Default)] struct DamageReceived; // This message should be sent when an entity blocks damage with armor. #[derive(Message, Debug, Default)] struct ArmorBlockedDamage; // This resource represents a timer used to determine when to deal damage // By default it repeats once per second #[derive(Resource, Deref, DerefMut)] struct DamageTimer(pub Timer); impl Default for DamageTimer { fn default() -> Self { DamageTimer(Timer::from_seconds(1.0, TimerMode::Repeating)) } } // Next we define systems that send, mutate, and receive messages // This system reads 'DamageTimer', updates it, then sends a 'DealDamage' message // if the timer has finished. // // Messages are sent using an 'MessageWriter' by calling 'write' or 'write_default'. // The 'write_default' method will send the message with the default value if the message // has a 'Default' implementation. fn deal_damage_over_time( time: Res