Skip to content

Commit 7f48db4

Browse files
committed
✨ minions atk each others
1 parent 36cae2f commit 7f48db4

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/minions.rs

+43-33
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy::{
66
prelude::*,
77
sprite::{Sprite, SpriteBundle},
88
time::{Time, Timer},
9-
utils::default,
9+
utils::{default, HashMap},
1010
};
1111
use bevy_rapier2d::prelude::*;
1212
use rand::Rng;
@@ -86,47 +86,57 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
8686

8787
fn update_move_minions(
8888
time: Res<Time>,
89-
// if we want minion to move over other minions later, maybe we want something like this instead of having 2 queries
90-
// let mut combinations = query.iter_combinations_mut();
91-
// while let Some([mut a, mut b]) = combinations.fetch_next() {
92-
mut query: Query<(&mut Transform, &Team), With<Minion>>,
93-
query_targets: Query<(&mut Transform, &Team), Without<Minion>>,
89+
mut query: Query<(&mut Transform, Entity, &Team, Option<&Minion>)>,
9490
) {
95-
for (mut transform, team) in &mut query {
96-
// acquire new target
97-
let mut closer_target: &Transform = &Transform::from_xyz(0., 0., 0.);
98-
let mut found_target = false;
99-
for (target_transform, target_team) in &query_targets {
100-
if team.id == target_team.id {
101-
continue;
102-
}
91+
let mut closest_translations: HashMap<Entity, Vec3> = HashMap::new();
10392

104-
if !found_target {
105-
found_target = true;
106-
closer_target = target_transform;
107-
continue;
108-
}
109-
if transform
93+
let mut combinations = query.iter_combinations_mut();
94+
while let Some(
95+
[(transform_a, entity_a, team_a, minion_a), (transform_b, entity_b, team_b, minion_b)],
96+
) = combinations.fetch_next()
97+
{
98+
if minion_a.is_none() && minion_b.is_none() {
99+
continue;
100+
}
101+
102+
if team_a.id == team_b.id {
103+
continue;
104+
}
105+
106+
let (minion_transform, minion_entity, target_translation) = if minion_a.is_some() {
107+
(transform_a, entity_a, transform_b.translation)
108+
} else {
109+
(transform_b, entity_b, transform_a.translation)
110+
};
111+
112+
// found a translation for this entity
113+
// but this is farther combination so we do nothing
114+
if let Some(closest_translation) = closest_translations.get(&minion_entity) {
115+
if minion_transform
110116
.translation
111-
.distance_squared(closer_target.translation)
112-
> transform
117+
.distance_squared(*closest_translation)
118+
<= minion_transform
113119
.translation
114-
.distance_squared(target_transform.translation)
120+
.distance_squared(target_translation)
115121
{
116-
closer_target = target_transform;
122+
continue;
117123
}
118124
}
119125

120-
// move toward the target
121-
if !found_target {
122-
continue;
126+
// we are here if we found a closer combination
127+
// so we insert it and move toward this combination
128+
closest_translations.insert(minion_entity, target_translation);
129+
}
130+
131+
for (entity, translation) in closest_translations {
132+
if let Ok((mut transform, _, _, _)) = query.get_mut(entity) {
133+
let normalized_target_position = (translation - transform.translation).normalize();
134+
135+
transform.translation.x +=
136+
time.delta_seconds() * MINION_SCALE * normalized_target_position.x;
137+
transform.translation.y +=
138+
time.delta_seconds() * MINION_SCALE * normalized_target_position.y;
123139
}
124-
let normalized_target_position =
125-
(closer_target.translation - transform.translation).normalize();
126-
transform.translation.x +=
127-
time.delta_seconds() * MINION_SCALE * normalized_target_position.x;
128-
transform.translation.y +=
129-
time.delta_seconds() * MINION_SCALE * normalized_target_position.y;
130140
}
131141
}
132142

0 commit comments

Comments
 (0)