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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::{thread, time};
use specs::{prelude::*, World};
use crate::{floor::Floor, unit::UnitType, Player};
pub mod components;
pub mod systems;
use components::UnitComponent;
use systems::{PlayerSystem, ShooterSystem, SludgeSystem, UiSystem};
pub fn start(
name: String,
floor: Floor,
player: impl Player + Send + Sync + 'static,
) -> Result<(), String> {
let mut world = World::new();
let player_system = PlayerSystem::new(name.clone(), floor.clone(), player);
let sludge_system = SludgeSystem::new(name.clone());
let shooter_system = ShooterSystem::new(name.clone());
let ui_system = UiSystem::new(floor.clone());
let mut dispatcher = DispatcherBuilder::new()
.with(player_system, "player", &[])
.with(sludge_system, "sludge", &["player"])
.with(shooter_system, "shooter", &["player", "sludge"])
.with(ui_system, "ui", &["player", "sludge", "shooter"])
.build();
dispatcher.setup(&mut world);
for unit in &floor.units {
UnitComponent::create(&mut world, unit.clone());
}
floor.draw();
let mut step = 0;
loop {
step += 1;
{
let units = world.read_storage::<UnitComponent>();
for entity in world.entities().join() {
match units.get(entity) {
Some(warrior_comp) if warrior_comp.unit.unit_type == UnitType::Warrior => {
let (current, _) = warrior_comp.unit.hp;
if current == 0 {
return Err(format!("{} died!", &name));
}
if step > 100 {
return Err(format!("{} seems to have gotten lost...", &name));
}
if warrior_comp.unit.position == floor.stairs {
return Ok(());
}
}
_ => {}
}
}
}
dispatcher.dispatch(&world);
world.maintain();
thread::sleep(time::Duration::from_millis(500));
}
}