summaryrefslogtreecommitdiff
path: root/ecs/examples/extension.rs
blob: 2022b056b2114be6ef495ff596a5128f2afa8e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use ecs::actions::Actions;
use ecs::event::Event;
use ecs::extension::{Collector as ExtensionCollector, Extension};
use ecs::{Component, Query, World};

#[derive(Debug, Component)]
struct Position
{
    x: u32,
    y: u32,
}

#[derive(Debug, Component)]
struct EnemySpawnSource;

#[derive(Debug, Component)]
enum EvilnessLevel
{
    Medium,
}

#[derive(Debug)]
struct Update;

impl Event for Update {}

fn spawn_enemies(
    spawner_query: Query<(EnemySpawnSource, Position)>,
    enemies_query: Query<(EvilnessLevel,)>,
    mut actions: Actions,
)
{
    let Some((_, enemy_spawner_position)) = spawner_query.iter().next() else {
        return;
    };

    let enemy_cnt = enemies_query.iter().count();

    if enemy_cnt > 3 {
        return;
    }

    actions.spawn((
        EvilnessLevel::Medium,
        Position {
            x: enemy_spawner_position.x * enemy_cnt as u32,
            y: enemy_spawner_position.y,
        },
    ));

    println!("Spawned enemy with medium evilness and 45 strength");
}

struct EnemySpawningExtension;

impl Extension for EnemySpawningExtension
{
    fn collect(self, mut collector: ExtensionCollector<'_>)
    {
        collector.add_system(Update, spawn_enemies);

        collector.add_entity((Position { x: 187, y: 30 }, EnemySpawnSource));
    }
}

fn main()
{
    let mut world = World::new();

    world.add_extension(EnemySpawningExtension);

    for _ in 0..7 {
        world.emit(Update);

        world.perform_queued_actions();
    }
}