Skip to content

Commit 892e3c8

Browse files
committed
💎 destroy minions that are too old (avoid leak)
1 parent 925e21b commit 892e3c8

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/minions.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
use crate::common::*;
22
use bevy::{
33
prelude::{
4-
trace, App, Color, Commands, Component, DespawnRecursiveExt, Entity, Plugin, Query, Res,
4+
trace, App, Commands, Component, DespawnRecursiveExt, Entity, Plugin, Query, Res,
55
Transform, Update, Vec2, With, Without,
66
},
77
sprite::{Sprite, SpriteBundle},
8-
time::Time,
8+
time::{Time, Timer},
99
utils::default,
1010
};
1111
use rand::Rng;
1212

1313
const MINION_SCALE: f32 = 100.;
14+
const DESTROY_MINIONS_AFTER_SECS: f32 = 120.;
1415

1516
pub struct MinionsPlugin;
1617

1718
#[derive(Component)]
18-
struct Minion;
19+
struct Minion {
20+
destroy_timer: Timer,
21+
}
1922

2023
impl Plugin for MinionsPlugin {
2124
fn build(&self, app: &mut App) {
22-
app.add_systems(Update, update_move_minions);
25+
app.add_systems(Update, (update_move_minions, destroy_minions));
2326
}
2427
}
2528

@@ -42,7 +45,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
4245
),
4346
..default()
4447
},
45-
Minion {},
48+
Minion {
49+
// to avoid leaks
50+
// maybe a better option on top of that is to leach health every seconds on minions and make them die!
51+
destroy_timer: Timer::from_seconds(
52+
DESTROY_MINIONS_AFTER_SECS,
53+
bevy::time::TimerMode::Once,
54+
),
55+
},
4656
team,
4757
))
4858
.id();
@@ -51,24 +61,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
5161
}
5262

5363
fn update_move_minions(
54-
mut commands: Commands,
5564
time: Res<Time>,
5665
// if we want minion to move over other minions later, maybe we want something like this instead of having 2 queries
5766
// let mut combinations = query.iter_combinations_mut();
5867
// while let Some([mut a, mut b]) = combinations.fetch_next() {
59-
mut query: Query<(&mut Transform, Entity, &Team), With<Minion>>,
68+
mut query: Query<(&mut Transform, &Team), With<Minion>>,
6069
query_targets: Query<(&mut Transform, &Team), Without<Minion>>,
6170
) {
62-
for (mut transform, entity, team) in &mut query {
63-
// unspawn minions time to time
64-
if transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
65-
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.
66-
{
67-
trace!("Unspawning Minion: {:?}", entity);
68-
commands.entity(entity).despawn_recursive();
69-
continue;
70-
}
71-
71+
for (mut transform, team) in &mut query {
7272
// acquire new target
7373
let mut closer_target: &Transform = &Transform::from_xyz(0., 0., 0.);
7474
let mut found_target = false;
@@ -105,3 +105,21 @@ fn update_move_minions(
105105
time.delta_seconds() * MINION_SCALE * normalized_target_position.y;
106106
}
107107
}
108+
109+
fn destroy_minions(
110+
time: Res<Time>,
111+
mut commands: Commands,
112+
mut query: Query<(&mut Minion, &Transform, Entity)>,
113+
) {
114+
for (mut minion, transform, entity) in &mut query {
115+
let escape_edges_of_the_world = transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
116+
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.;
117+
let too_old = minion.destroy_timer.tick(time.delta()).just_finished();
118+
119+
if escape_edges_of_the_world || too_old {
120+
trace!("Unspawning Minion: {:?}", entity);
121+
commands.entity(entity).despawn_recursive();
122+
continue;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)