Skip to content

Commit

Permalink
Implement MapEntities for ActionDiffEvent (#598)
Browse files Browse the repository at this point in the history
* Implement MapEntities for ActionDiffEvent

* Use proper terminology in comments

Co-authored-by: Periwink <charlesbour@gmail.com>

* Add change to RELEASES.md

* Run cargo fmt --all

---------

Co-authored-by: Periwink <charlesbour@gmail.com>
  • Loading branch information
zhaop and cBournhonesque authored Aug 19, 2024
1 parent 9f9c3f3 commit 43654e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

- Reflect `Component` and `Resource`, which enables accessing the data in the type registry

#### ActionDiffEvent

- Implement `MapEntities`, which lets networking crates translate owner entity IDs between ECS worlds

## Version 0.15.0

### Enhancements (0.15)
Expand Down
17 changes: 15 additions & 2 deletions src/action_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
//! about things like keybindings or input devices.

use bevy::{
ecs::{entity::Entity, event::Event},
ecs::{
entity::{Entity, MapEntities},
event::Event,
},
math::Vec2,
prelude::{EventWriter, Query, Res},
prelude::{EntityMapper, EventWriter, Query, Res},
utils::{HashMap, HashSet},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -62,6 +65,16 @@ pub struct ActionDiffEvent<A: Actionlike> {
pub action_diffs: Vec<ActionDiff<A>>,
}

/// Implements entity mapping for `ActionDiffEvent`.
///
/// This allows the owner entity to be remapped when transferring event diffs
/// between different ECS worlds (e.g. client and server).
impl<A: Actionlike> MapEntities for ActionDiffEvent<A> {
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
self.owner = self.owner.map(|entity| entity_mapper.map_entity(entity));
}
}

/// Stores the state of all actions in the current frame.
///
/// Inside of the hashmap, [`Entity::PLACEHOLDER`] represents the global / resource state of the action.
Expand Down

0 comments on commit 43654e5

Please sign in to comment.