-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathattack.rs
More file actions
269 lines (245 loc) · 8.94 KB
/
attack.rs
File metadata and controls
269 lines (245 loc) · 8.94 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use std::time::Duration;
use bevy::{hierarchy::DespawnRecursiveExt, math::Vec2, prelude::*, reflect::Reflect};
use bevy_rapier2d::prelude::*;
use serde::Deserialize;
use crate::{
animation::Animation,
damage::{DamageEvent, Damageable, Health},
enemy::Enemy,
fighter_state::MeleeWeapon,
item::{Drop, Explodable},
metadata::ColliderMeta,
player::Player,
GameState,
};
pub struct AttackPlugin;
impl Plugin for AttackPlugin {
fn build(&self, app: &mut App) {
app
// Register reflect types
.register_type::<Attack>()
// Add systems
.add_systems(
Update,
(
activate_hitbox,
deactivate_hitbox,
breakable_system,
damage_flash,
)
.run_if(in_state(GameState::InGame)),
)
// Attack damage is run in PostUpdate to make sure it runs after rapier generates collision events
.add_systems(PostUpdate, attack_damage_system)
// Event for when Breakable breaks
.add_event::<BrokeEvent>();
}
}
//Component to representing a timer to start and stop the flash upon damage impact
#[derive(Component)]
pub struct FlashingTimer {
pub timer: Timer,
}
/// A component representing an attack that can do damage to [`Damageable`]s with [`Health`].
#[derive(Component, Clone, Copy, Default, Reflect)]
#[reflect(Component)]
pub struct Attack {
//maybe just replace all fields with AttackMeta
pub damage: i32,
/// The direction and speed that the attack is hitting something in.
pub pushback: Vec2,
pub hitstun_duration: f32,
/// add this for attacks that are not immediately active, used in activate_hitbox
pub hitbox_meta: Option<ColliderMeta>,
}
#[derive(Component)]
pub struct Hurtbox;
/// A component that depawns an entity after collision.
#[derive(Component, Clone, Copy, Default, Reflect)]
pub struct Breakable {
/// The number of collisions allowed before the entity is breakable.
pub hit_tolerance: i32,
/// The number of collisions occured.
pub hit_count: i32,
/// If it should despawn it's parent on break
pub despawn_parent: bool,
}
impl Breakable {
pub fn new(hits: i32, despawn_parent: bool) -> Self {
Self {
hit_tolerance: hits,
hit_count: 0,
despawn_parent,
}
}
}
#[derive(Event)]
pub struct BrokeEvent {
pub drop: Option<Drop>,
pub transform: Option<Transform>,
pub explodable: Option<Explodable>,
}
/// A component identifying the attacks active collision frames.
///
/// Must be added to an entity that is a child of an entity with an [`Animation`] and an [`Attack`]
/// and will be used to spawn a collider for that attack during the `active` frames.
/// Each field is an index refering to an animation frame
#[derive(Component, Debug, Clone, Copy, Deserialize, Reflect)]
pub struct AttackFrames {
pub startup: usize,
pub active: usize,
pub recovery: usize,
}
/// Activates inactive attacks after the animation on the attack reaches the active frames by
/// adding a collider to the attack entity.
//TODO: is there a way we can move the adding of collision layers here as well?
fn activate_hitbox(
attack_query: Query<(Entity, &Attack, &AttackFrames, &Parent), Without<Collider>>,
parent_query: Query<
&Animation,
Or<(
With<Player>,
With<Enemy>,
With<MeleeWeapon>,
With<Explodable>,
)>,
>,
mut commands: Commands,
) {
for (entity, attack, attack_frames, parent) in attack_query.iter() {
if let Ok(animation) = parent_query.get(**parent) {
if animation.current_frame >= attack_frames.startup
&& animation.current_frame <= attack_frames.active
{
if let Some(hitbox_meta) = attack.hitbox_meta {
commands
.entity(entity)
.insert(Sensor)
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(
ActiveCollisionTypes::default() | ActiveCollisionTypes::STATIC_STATIC,
)
.insert(Collider::cuboid(
hitbox_meta.size.x / 2.,
hitbox_meta.size.y / 2.,
));
}
}
}
}
}
/// Deactivate collisions for entities with [`AttackFrames`]
fn deactivate_hitbox(
query: Query<(Entity, &AttackFrames, &Parent), (With<Attack>, With<Collider>)>,
animated_query: Query<&Animation>,
mut commands: Commands,
) {
for (entity, attack_frames, parent) in query.iter() {
if let Ok(animation) = animated_query.get(**parent) {
if animation.current_frame >= attack_frames.recovery {
commands.entity(entity).despawn_recursive();
}
}
}
}
// flash component
// changes an entity's sprite to white for a specified amount of time
fn damage_flash(
mut commands: Commands,
mut flash_query: Query<(&mut FlashingTimer, Entity, &mut TextureAtlasSprite)>,
time: Res<Time>,
) {
for (mut timer, timer_e, mut timer_sprite) in flash_query.iter_mut() {
//Set the color to white
timer_sprite.color = Color::rgb(255.0, 255.0, 255.0);
//run the timer
timer.timer.tick(time.delta());
//Reset the color back to normal and remove the flash component
if timer.timer.finished() {
timer_sprite.color = Color::rgba(1.0, 1.0, 1.0, 1.0);
commands.entity(timer_e).remove::<FlashingTimer>();
}
}
}
/// Depletes the health of damageables that have collided with attacks
fn attack_damage_system(
mut commands: Commands,
mut events: EventReader<CollisionEvent>,
mut damageables: Query<(&mut Health, &Damageable)>,
attacks: Query<&Attack>,
hurtboxes: Query<&Parent, With<Hurtbox>>,
mut event_writer: EventWriter<DamageEvent>,
) {
for event in events.iter() {
if let CollisionEvent::Started(e1, e2, _flags) = event {
let (attack_entity, hurtbox_entity) =
if attacks.contains(*e1) && hurtboxes.contains(*e2) {
(*e1, *e2)
} else if attacks.contains(*e2) && hurtboxes.contains(*e1) {
(*e2, *e1)
} else {
continue;
};
let attack = attacks.get(attack_entity).unwrap();
if let Ok(hurtbox_parent) = hurtboxes.get(hurtbox_entity) {
let hurtbox_parent_entity = hurtbox_parent.get();
let (mut health, damageable) = damageables.get_mut(hurtbox_parent_entity).unwrap();
//apply damage to target
if **damageable {
**health -= attack.damage;
//Damage flash of 100ms upon an entity taking damage
commands
.entity(hurtbox_parent_entity)
.insert(FlashingTimer {
timer: Timer::new(Duration::from_millis(100), TimerMode::Repeating),
});
event_writer.send(DamageEvent {
damageing_entity: attack_entity,
damage_velocity: attack.pushback,
damage: attack.damage,
damaged_entity: hurtbox_parent_entity,
hitstun_duration: attack.hitstun_duration,
})
}
}
}
}
}
fn breakable_system(
mut events: EventReader<CollisionEvent>,
mut despawn_query: Query<(
&mut Breakable,
Option<&Drop>,
Option<&Transform>,
Option<&Parent>,
Option<&Explodable>,
)>,
mut commands: Commands,
mut event_writer: EventWriter<BrokeEvent>,
) {
for ev in events.iter() {
if let CollisionEvent::Started(e1, e2, _flags) = ev {
for e in [e1, e2].iter() {
if let Ok((mut breakable, drop, transform, parent, explodable)) =
despawn_query.get_mut(**e)
{
if breakable.hit_count < breakable.hit_tolerance {
breakable.hit_count += 1;
} else {
event_writer.send(BrokeEvent {
drop: drop.cloned(),
transform: transform.cloned(),
explodable: explodable.cloned(),
});
commands.entity(**e).despawn_recursive();
if breakable.despawn_parent {
if let Some(parent) = parent {
commands.entity(parent.get()).despawn_recursive()
}
}
}
}
}
}
}
}