This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
units.rs
80 lines (71 loc) · 1.92 KB
/
units.rs
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
use bevy::prelude::*;
use crate::entity_map::EntityMap;
use crate::graphics::sprite_bundle_from_position;
use crate::id::ID;
use crate::organisms::{Impassable, OrganismBundle};
use crate::position::Position;
#[derive(Clone, Default)]
pub struct Unit;
#[derive(Bundle, Default)]
pub struct UnitBundle {
unit: Unit,
#[bundle]
organism_bundle: OrganismBundle,
}
#[derive(Clone, Default)]
pub struct Ant;
#[derive(Bundle, Default)]
pub struct AntBundle {
ant: Ant,
#[bundle]
unit_bundle: UnitBundle,
}
impl AntBundle {
pub fn new(position: Position, material: Handle<ColorMaterial>) -> Self {
Self {
unit_bundle: UnitBundle {
organism_bundle: OrganismBundle {
sprite_bundle: sprite_bundle_from_position(position, material),
id: ID::Ant,
..Default::default()
},
..Default::default()
},
..Default::default()
}
}
}
pub struct UnitsPlugin;
impl Plugin for UnitsPlugin {
fn build(&self, app: &mut AppBuilder) {
app.insert_resource(UnitTimer(Timer::from_seconds(0.5, true)))
.add_system(act.system());
}
}
/// Global timer that controls when units should act
struct UnitTimer(Timer);
fn act(
time: Res<Time>,
entity_map: Res<EntityMap>,
mut timer: ResMut<UnitTimer>,
mut query: Query<(&Unit, &mut Position)>,
passable_query: Query<&Impassable>,
) {
timer.0.tick(time.delta());
if timer.0.finished() {
for (_, mut position) in query.iter_mut() {
*position = wander(*position, &entity_map, &passable_query);
}
}
}
fn wander(
position: Position,
entity_map: &EntityMap,
passable_query: &Query<&Impassable>,
) -> Position {
let target = position.random_passable_neighbor(entity_map, passable_query);
match target {
Some(p) => p,
None => position,
}
}