This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
/
combat.rs
203 lines (180 loc) · 6.34 KB
/
combat.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use amethyst::{
core::Time,
ecs::*,
shrev::{EventChannel, ReaderId},
};
use crate::components::combat;
use crate::components::combat::{Cooldown, Damage, Speed};
use crate::components::health::Health;
use crate::systems::collision::CollisionEvent;
#[cfg(test)]
use amethyst::Error;
#[cfg(test)]
use amethyst_test::AmethystApplication;
use std::time::Duration;
pub struct CooldownSystem;
impl<'s> System<'s> for CooldownSystem {
type SystemData = (WriteStorage<'s, Cooldown>, Entities<'s>, Read<'s, Time>);
fn run(&mut self, (mut cooldowns, entities, time): Self::SystemData) {
let mut to_remove = Vec::new();
for (mut cooldown, entity) in (&mut cooldowns, &*entities).join() {
match cooldown.time_left.checked_sub(time.delta_time()) {
Some(time_left) => {
cooldown.time_left = time_left;
}
None => {
to_remove.push(entity);
}
}
}
for entity in &to_remove {
cooldowns.remove(*entity);
}
}
}
///
///
///
pub struct AttackEvent {
pub attacker: Entity,
pub defender: Entity,
}
#[derive(Default)]
pub struct PerformDefaultAttackSystem {
event_reader: Option<ReaderId<AttackEvent>>,
}
impl<'s> System<'s> for PerformDefaultAttackSystem {
type SystemData = (
Read<'s, EventChannel<AttackEvent>>,
ReadStorage<'s, Damage>,
WriteStorage<'s, Cooldown>,
ReadStorage<'s, Speed>,
WriteStorage<'s, Health>,
);
fn run(
&mut self,
(attack_events, damages, mut cooldowns, speeds, mut healths): Self::SystemData,
) {
let event_reader = self
.event_reader
.as_mut()
.expect("`PerformDefaultAttackSystem::setup` was not called before `PerformDefaultAttackSystem::run`");
for event in attack_events.read(event_reader) {
let mut attack_set = BitSet::new();
attack_set.add(event.attacker.id());
let mut defender_set = BitSet::new();
defender_set.add(event.defender.id());
let mut cooldown = None;
for (damage, _, speed, _) in (&damages, !&cooldowns, &speeds, &attack_set).join() {
for (mut health, _) in (&mut healths, &defender_set).join() {
health.value = health.value - damage.damage;
cooldown = Some(Cooldown::new(Duration::from_millis(
(1000.0 / speed.attacks_per_second) as u64,
)));
}
}
if let Some(value) = cooldown {
cooldowns
.insert(event.attacker, value)
.expect("Unreachable: we are inserting now.");
}
}
}
fn setup(&mut self, res: &mut Resources) {
Self::SystemData::setup(res);
self.event_reader = Some(
res.fetch_mut::<EventChannel<AttackEvent>>()
.register_reader(),
)
}
}
#[derive(Default)]
pub struct FindAttackSystem {
event_reader: Option<ReaderId<CollisionEvent>>,
}
// Determine if a collision will trigger an attack. If that is the case, generate an `AttackEvent`
impl<'s> System<'s> for FindAttackSystem {
type SystemData = (
Read<'s, EventChannel<CollisionEvent>>,
Write<'s, EventChannel<AttackEvent>>,
ReadStorage<'s, combat::HasFaction>,
ReadStorage<'s, combat::FactionEnemies>,
);
fn run(
&mut self,
(collision_events, mut attack_events, has_faction, faction_enemies): Self::SystemData,
) {
let event_reader = self
.event_reader
.as_mut()
.expect("`FindAttackSystem::setup` was not called before `FindAttackSystem::run`");
for event in collision_events.read(event_reader) {
let opt_factions = has_faction
.get(event.entity_a)
.and_then(|a| has_faction.get(event.entity_b).map(|b| (a, b)));
if let Some((faction_a, faction_b)) = opt_factions {
let enemies_a = faction_enemies.get(faction_a.faction);
if let Some(enemies) = enemies_a {
if enemies.is_enemy(&faction_b.faction) {
attack_events.single_write(AttackEvent {
attacker: event.entity_a,
defender: event.entity_b,
});
}
}
let enemies_b = faction_enemies.get(faction_b.faction);
if let Some(enemies) = enemies_b {
if enemies.is_enemy(&faction_a.faction) {
attack_events.single_write(AttackEvent {
attacker: event.entity_b,
defender: event.entity_a,
});
}
}
}
}
}
fn setup(&mut self, res: &mut Resources) {
Self::SystemData::setup(res);
self.event_reader = Some(
res.fetch_mut::<EventChannel<CollisionEvent>>()
.register_reader(),
)
}
}
#[test]
fn test_cooldown_is_reduced() -> Result<(), Error> {
AmethystApplication::blank()
.with_system(CooldownSystem, "cooldown_system", &[])
.with_setup(|world| {
world
.create_entity()
.with(Cooldown::new(Duration::from_millis(5000)))
.build();
})
.with_assertion(|world| {
let entity = world.entities().entity(0);
let cooldowns = world.read_storage::<Cooldown>();
let cooldown = cooldowns.get(entity).unwrap();
assert!(cooldown.time_left.as_millis() < 5000);
})
.run()
}
#[test]
fn test_cooldown_is_removed() -> Result<(), Error> {
AmethystApplication::blank()
.with_system(CooldownSystem, "cooldown_system", &[])
.with_setup(|world| {
world
.create_entity()
.with(Cooldown::new(Duration::from_millis(0)))
.build();
})
.with_assertion(|world| {
let entity = world.entities().entity(0);
let cooldowns = world.read_storage::<Cooldown>();
let cooldown = cooldowns.get(entity);
assert!(cooldown.is_none());
})
.run()
}