1
1
use crate :: common:: * ;
2
2
use bevy:: {
3
3
prelude:: {
4
- trace, App , Color , Commands , Component , DespawnRecursiveExt , Entity , Plugin , Query , Res ,
4
+ trace, App , Commands , Component , DespawnRecursiveExt , Entity , Plugin , Query , Res ,
5
5
Transform , Update , Vec2 , With , Without ,
6
6
} ,
7
7
sprite:: { Sprite , SpriteBundle } ,
8
- time:: Time ,
8
+ time:: { Time , Timer } ,
9
9
utils:: default,
10
10
} ;
11
11
use rand:: Rng ;
12
12
13
13
const MINION_SCALE : f32 = 100. ;
14
+ const DESTROY_MINIONS_AFTER_SECS : f32 = 120. ;
14
15
15
16
pub struct MinionsPlugin ;
16
17
17
18
#[ derive( Component ) ]
18
- struct Minion ;
19
+ struct Minion {
20
+ destroy_timer : Timer ,
21
+ }
19
22
20
23
impl Plugin for MinionsPlugin {
21
24
fn build ( & self , app : & mut App ) {
22
- app. add_systems ( Update , update_move_minions) ;
25
+ app. add_systems ( Update , ( update_move_minions, destroy_minions ) ) ;
23
26
}
24
27
}
25
28
@@ -42,7 +45,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
42
45
) ,
43
46
..default ( )
44
47
} ,
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
+ } ,
46
56
team,
47
57
) )
48
58
. id ( ) ;
@@ -51,24 +61,14 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
51
61
}
52
62
53
63
fn update_move_minions (
54
- mut commands : Commands ,
55
64
time : Res < Time > ,
56
65
// if we want minion to move over other minions later, maybe we want something like this instead of having 2 queries
57
66
// let mut combinations = query.iter_combinations_mut();
58
67
// 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 > > ,
60
69
query_targets : Query < ( & mut Transform , & Team ) , Without < Minion > > ,
61
70
) {
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 {
72
72
// acquire new target
73
73
let mut closer_target: & Transform = & Transform :: from_xyz ( 0. , 0. , 0. ) ;
74
74
let mut found_target = false ;
@@ -105,3 +105,21 @@ fn update_move_minions(
105
105
time. delta_seconds ( ) * MINION_SCALE * normalized_target_position. y ;
106
106
}
107
107
}
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