Skip to content

Commit

Permalink
Merge pull request #4 from TheRawMeatball/fix-derive-component-again
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizi authored Sep 30, 2021
2 parents fd62374 + 7e08fd3 commit f7c0354
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ mod tests {
let mut world = World::default();
assert!(world.get_resource::<i32>().is_none());
assert!(!world.contains_resource::<i32>());
assert!(!world.is_resource_added::<i32>());
assert!(!world.is_resource_changed::<i32>());

world.insert_resource(123);
let resource_id = world
Expand All @@ -1002,6 +1004,8 @@ mod tests {

assert_eq!(*world.get_resource::<i32>().expect("resource exists"), 123);
assert!(world.contains_resource::<i32>());
assert!(world.is_resource_added::<i32>());
assert!(world.is_resource_changed::<i32>());

world.insert_resource(456u64);
assert_eq!(
Expand Down
26 changes: 22 additions & 4 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,16 +677,34 @@ impl World {
}

pub fn is_resource_added<T: Resource>(&self) -> bool {
let component_id = self.components.get_resource_id(TypeId::of::<T>()).unwrap();
let column = self.get_populated_resource_column(component_id).unwrap();
let component_id =
if let Some(component_id) = self.components.get_resource_id(TypeId::of::<T>()) {
component_id
} else {
return false;
};
let column = if let Some(column) = self.get_populated_resource_column(component_id) {
column
} else {
return false;
};
// SAFE: resources table always have row 0
let ticks = unsafe { column.get_ticks_unchecked(0) };
ticks.is_added(self.last_change_tick(), self.read_change_tick())
}

pub fn is_resource_changed<T: Resource>(&self) -> bool {
let component_id = self.components.get_resource_id(TypeId::of::<T>()).unwrap();
let column = self.get_populated_resource_column(component_id).unwrap();
let component_id =
if let Some(component_id) = self.components.get_resource_id(TypeId::of::<T>()) {
component_id
} else {
return false;
};
let column = if let Some(column) = self.get_populated_resource_column(component_id) {
column
} else {
return false;
};
// SAFE: resources table always have row 0
let ticks = unsafe { column.get_ticks_unchecked(0) };
ticks.is_changed(self.last_change_tick(), self.read_change_tick())
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ downcast-rs = "1.2.0"
thiserror = "1.0"
anyhow = "1.0.4"
hex = "0.4.2"
hexasphere = "4.0.0"
hexasphere = "5.0.0"
parking_lot = "0.11.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ impl TextureAtlas {
}

/// Add a sprite to the list of textures in the `TextureAtlas`
/// returns an index to the texture which can be used with `TextureAtlasSprite`
///
/// # Arguments
///
/// * `rect` - The section of the atlas that contains the texture to be added,
/// from the top-left corner of the texture to the bottom-right corner
pub fn add_texture(&mut self, rect: Rect) {
pub fn add_texture(&mut self, rect: Rect) -> u32 {
self.textures.push(rect);
(self.textures.len() - 1) as u32
}

/// How many textures are in the `TextureAtlas`
Expand Down
18 changes: 15 additions & 3 deletions crates/bevy_transform/src/hierarchy/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::components::{Children, Parent};
use bevy_ecs::{
entity::Entity,
system::{Command, EntityCommands},
world::World,
world::{EntityMut, World},
};
use bevy_utils::tracing::debug;

Expand Down Expand Up @@ -44,17 +44,29 @@ impl Command for DespawnRecursive {

pub trait DespawnRecursiveExt {
/// Despawns the provided entity and its children.
fn despawn_recursive(&mut self);
fn despawn_recursive(self);
}

impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> {
/// Despawns the provided entity and its children.
fn despawn_recursive(&mut self) {
fn despawn_recursive(mut self) {
let entity = self.id();
self.commands().add(DespawnRecursive { entity });
}
}

impl<'w> DespawnRecursiveExt for EntityMut<'w> {
/// Despawns the provided entity and its children.
fn despawn_recursive(mut self) {
let entity = self.id();
// SAFE: EntityMut is consumed so even though the location is no longer
// valid, it cannot be accessed again with the invalid location.
unsafe {
despawn_with_children_recursive(self.world_mut(), entity);
}
}
}

#[cfg(test)]
mod tests {
use bevy_ecs::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::components::*;
use bevy_ecs::{
entity::Entity,
prelude::Changed,
query::Without,
system::{Commands, Query},
};
Expand All @@ -10,9 +11,7 @@ use smallvec::SmallVec;
pub fn parent_update_system(
mut commands: Commands,
removed_parent_query: Query<(Entity, &PreviousParent), Without<Parent>>,
// The next query could be run with a Changed<Parent> filter. However, this would mean that
// modifications later in the frame are lost. See issue 891: https://github.com/bevyengine/bevy/issues/891
mut parent_query: Query<(Entity, &Parent, Option<&mut PreviousParent>)>,
mut parent_query: Query<(Entity, &Parent, Option<&mut PreviousParent>), Changed<Parent>>,
mut children_query: Query<&mut Children>,
) {
// Entities with a missing `Parent` (ie. ones that have a `PreviousParent`), remove
Expand Down

0 comments on commit f7c0354

Please sign in to comment.