Skip to main content

concinnity_core/components/
spawner.rs

1// src/components/spawner.rs
2//
3// Runtime `Spawner` component. Its authored args live in the schema crate
4// (concinnity_asset::spawner).
5
6use concinnity_asset::cook;
7
8use crate::ecs::Component;
9use crate::ecs::asset_id::AssetId;
10
11/// Periodically instantiates copies of an existing placement at this entity's
12/// position.
13///
14/// A spawner clones `template` (the name of another placement in the world)
15/// every `interval` seconds, giving each copy a `lifetime` after which it is
16/// automatically removed. Pairing a short lifetime with a short interval keeps a
17/// bounded population churning (an enemy wave, a particle of debris, a fountain
18/// of props) and is what exercises GPU draw-slot recycling: each expiry frees a
19/// slot the next spawn reuses.
20///
21/// The spawner's own `Transform` (its position) is where copies appear, so place
22/// the spawner where you want the stream to originate.
23#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
24pub struct Spawner {
25    /// Name of the placement to copy on each spawn.
26    pub template: AssetId,
27    /// Seconds between spawns.
28    pub interval: f32,
29    /// Seconds each spawned copy lives before auto-removal; 0 keeps it forever.
30    pub lifetime: f32,
31    /// Runtime: seconds accumulated toward the next spawn.
32    pub elapsed: f32,
33    /// Runtime: number of copies spawned so far.
34    pub count: u32,
35}
36
37impl Spawner {
38    /// Translate the authored args into the runtime spawner: clamp the timing
39    /// knobs and zero the runtime counters. Run by cook at build time (the
40    /// baked blob record carries the result).
41    pub fn bake(args: cook::Spawner) -> Self {
42        Self {
43            template: args.template,
44            interval: args.interval.max(0.0),
45            lifetime: args.lifetime.max(0.0),
46            elapsed: 0.0,
47            count: 0,
48        }
49    }
50}
51
52impl Component for Spawner {
53    const NAME: &'static str = "Spawner";
54
55    fn from_baked(bytes: &[u8]) -> Result<Self, crate::result::CnResult> {
56        Ok(crate::blob::decode_exact(bytes)?)
57    }
58}