Skip to content

Commit

Permalink
💎 destroy minions that are too old (avoid leak)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Oct 30, 2023
1 parent 925e21b commit 892e3c8
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/minions.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use crate::common::*;
use bevy::{
prelude::{
trace, App, Color, Commands, Component, DespawnRecursiveExt, Entity, Plugin, Query, Res,
trace, App, Commands, Component, DespawnRecursiveExt, Entity, Plugin, Query, Res,
Transform, Update, Vec2, With, Without,
},
sprite::{Sprite, SpriteBundle},
time::Time,
time::{Time, Timer},
utils::default,
};
use rand::Rng;

const MINION_SCALE: f32 = 100.;
const DESTROY_MINIONS_AFTER_SECS: f32 = 120.;

pub struct MinionsPlugin;

#[derive(Component)]
struct Minion;
struct Minion {
destroy_timer: Timer,
}

impl Plugin for MinionsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, update_move_minions);
app.add_systems(Update, (update_move_minions, destroy_minions));
}
}

Expand All @@ -42,7 +45,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
),
..default()
},
Minion {},
Minion {
// to avoid leaks
// maybe a better option on top of that is to leach health every seconds on minions and make them die!
destroy_timer: Timer::from_seconds(
DESTROY_MINIONS_AFTER_SECS,
bevy::time::TimerMode::Once,
),
},
team,
))
.id();
Expand All @@ -51,24 +61,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
}

fn update_move_minions(
mut commands: Commands,
time: Res<Time>,
// if we want minion to move over other minions later, maybe we want something like this instead of having 2 queries
// let mut combinations = query.iter_combinations_mut();
// while let Some([mut a, mut b]) = combinations.fetch_next() {
mut query: Query<(&mut Transform, Entity, &Team), With<Minion>>,
mut query: Query<(&mut Transform, &Team), With<Minion>>,
query_targets: Query<(&mut Transform, &Team), Without<Minion>>,
) {
for (mut transform, entity, team) in &mut query {
// unspawn minions time to time
if transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.
{
trace!("Unspawning Minion: {:?}", entity);
commands.entity(entity).despawn_recursive();
continue;
}

for (mut transform, team) in &mut query {
// acquire new target
let mut closer_target: &Transform = &Transform::from_xyz(0., 0., 0.);
let mut found_target = false;
Expand Down Expand Up @@ -105,3 +105,21 @@ fn update_move_minions(
time.delta_seconds() * MINION_SCALE * normalized_target_position.y;
}
}

fn destroy_minions(
time: Res<Time>,
mut commands: Commands,
mut query: Query<(&mut Minion, &Transform, Entity)>,
) {
for (mut minion, transform, entity) in &mut query {
let escape_edges_of_the_world = transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.;
let too_old = minion.destroy_timer.tick(time.delta()).just_finished();

if escape_edges_of_the_world || too_old {
trace!("Unspawning Minion: {:?}", entity);
commands.entity(entity).despawn_recursive();
continue;
}
}
}

0 comments on commit 892e3c8

Please sign in to comment.