Skip to content

Commit 19881c4

Browse files
committed
fix State incorrectly being reused when calling handlers in swarm
1 parent 9a687f0 commit 19881c4

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

azalea-core/src/position.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Vec3 {
168168
f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
169169
}
170170

171-
/// Get the squared distance from this position to another position.
171+
/// Get the distance from this position to another position.
172172
/// Equivalent to `(self - other).length()`.
173173
pub fn distance_to(&self, other: &Self) -> f64 {
174174
(self - other).length()

azalea/src/swarm/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,19 @@ where
387387
});
388388

389389
// bot events
390-
while let Some((Some(event), bot)) = bots_rx.recv().await {
390+
while let Some((Some(first_event), first_bot)) = bots_rx.recv().await {
391391
if let Some(handler) = &self.handler {
392-
let state = bot.component::<S>();
393-
tokio::spawn((handler)(bot, event, state.clone()));
392+
let first_bot_state = first_bot.component::<S>();
393+
let first_bot_entity = first_bot.entity;
394+
tokio::spawn((handler)(first_bot, first_event, first_bot_state.clone()));
395+
394396
// this makes it not have to keep locking the ecs
397+
let mut states = HashMap::new();
398+
states.insert(first_bot_entity, first_bot_state);
395399
while let Ok((Some(event), bot)) = bots_rx.try_recv() {
400+
let state = states
401+
.entry(bot.entity)
402+
.or_insert_with(|| bot.component::<S>().clone());
396403
tokio::spawn((handler)(bot, event, state.clone()));
397404
}
398405
}

0 commit comments

Comments
 (0)