Skip to content

Commit

Permalink
better docs, bump version to 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DynamicGoose committed Oct 13, 2024
1 parent b87dd20 commit 7ce9409
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "magma_ecs"
version = "0.1.0-alpha.5"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "Entity-Component-System for the Magma3D game engine"
homepage = "https://dynamicgoose.github.io/magma3d-engine/"
repository = "https://github.com/DynamicGoose/mecs"
repository = "https://github.com/DynamicGoose/magma-ecs"

[dependencies]
rayon = "1.10.0"
23 changes: 22 additions & 1 deletion src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,28 @@ impl Entities {
Ok(())
}

/// Get a [`Query`] on the [`Entities`]
/// Query for entities with specified components. Use either `run()` to get a `QueryResult` or `run_entity` to get a `Vec` of `QueryEntity`.
/// ```
/// use magma_ecs::World;
///
/// let mut world = World::new();
/// world.register_component::<u32>();
///
/// let mut entities = world.entities_write();
/// entities.create_entity().with_component(32_u32).unwrap();
///
/// let query = entities
/// .query()
/// .with_component::<u32>()
/// .unwrap()
/// .run();
///
/// let query = entities
/// .query()
/// .with_component::<u32>()
/// .unwrap()
/// .run_entity();
/// ```
pub fn query(&self) -> Query {
Query::new(self)
}
Expand Down
16 changes: 15 additions & 1 deletion src/entities/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::error::EntityError;

use super::{query_entity::QueryEntity, Component, Entities};

/// Used for querying for entities with specified components
#[derive(Debug)]
pub struct Query<'a> {
map: u128,
Expand All @@ -14,6 +15,10 @@ pub struct Query<'a> {
}

/// Result of a [`Query`] with indexes of the found entites and the queried component vecs.
#[deprecated(
since = "0.1.0",
note = "This will be removed in 0.2.0 in favor of `QueryEntity`."
)]
pub struct QueryResult {
pub indexes: Vec<usize>,
pub components: Vec<Vec<Component>>,
Expand Down Expand Up @@ -41,6 +46,11 @@ impl<'a> Query<'a> {
}

/// Run the [`Query`]
#[deprecated(
since = "0.1.0",
note = "This method will be removed in 0.2.0. Please use `run_entity` instead."
)]
#[allow(deprecated)]
pub fn run(&self) -> QueryResult {
let indexes: Vec<usize> = self
.entities
Expand All @@ -67,6 +77,7 @@ impl<'a> Query<'a> {
components.push(components_to_keep)
}

#[allow(deprecated)]
QueryResult {
indexes,
components,
Expand Down Expand Up @@ -140,12 +151,15 @@ mod test {
.unwrap()
.with_component::<f32>()
.unwrap();

#[allow(deprecated)]
let query_result = query.run();
#[allow(deprecated)]
let u32s = &query_result.components[0];
dbg!(u32s);
#[allow(deprecated)]
let f32s = &query_result.components[1];
dbg!(f32s);
#[allow(deprecated)]
let indexes = &query_result.indexes;
dbg!(indexes);

Expand Down
25 changes: 1 addition & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! // add a resource
//! world.add_resource(10_u32);
//!
//! // create entity with registered component.
//! // create an entity with registered component
//! // It is recommended to free read/write locks as quickly as possible. Use scopes to do that.
//! {
//! let mut entities = world.entities_write();
Expand Down Expand Up @@ -134,29 +134,6 @@ impl World {
self.entities.write().unwrap()
}

/// Query for entities with specified components. Use either `run()` to get a `QueryResult` or `run_entity` to get a `Vec` of `QueryEntity`.
/// ```
/// use magma_ecs::World;
///
/// let mut world = World::new();
/// world.register_component::<u32>();
///
/// let mut entities = world.entities_write();
/// entities.create_entity().with_component(32_u32).unwrap();
///
/// let query = entities
/// .query()
/// .with_component::<u32>()
/// .unwrap()
/// .run();
///
/// let query = entities
/// .query()
/// .with_component::<u32>()
/// .unwrap()
/// .run_entity();
/// ```
/// This takes a [`Vec`] of references to functions that take a reference to [`World`].
/// It runs all of the supplied functions in parallel once on the [`World`].
pub fn update(&self, systems: &Vec<fn(&Self)>) {
Expand Down
55 changes: 39 additions & 16 deletions tests/entities.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{any::Any, sync::{Arc, RwLock}};
use std::{
any::Any,
sync::{Arc, RwLock},
};

use magma_ecs::World;

Expand Down Expand Up @@ -30,9 +33,13 @@ fn entity_query() {
.unwrap()
.with_component(Size(10.0))
.unwrap();
entities.create_entity().with_component(Location(33.0, 33.0)).unwrap();
entities
.create_entity()
.with_component(Location(33.0, 33.0))
.unwrap();
entities.create_entity().with_component(Size(11.0)).unwrap();

#[allow(deprecated)]
let query = entities
.query()
.with_component::<Location>()
Expand All @@ -41,7 +48,9 @@ fn entity_query() {
.unwrap()
.run();

#[allow(deprecated)]
let locations: &Vec<Arc<RwLock<dyn Any + Send + Sync>>> = &query.components[0];
#[allow(deprecated)]
let sizes: &Vec<Arc<RwLock<dyn Any + Send + Sync>>> = &query.components[1];

let borrowed_first_location = locations[0].read().unwrap();
Expand Down Expand Up @@ -72,17 +81,18 @@ fn delete_component_from_entity() {
.with_component(Size(20.0))
.unwrap();

entities.remove_component_by_entity_id::<Location>(0).unwrap();
entities
.remove_component_by_entity_id::<Location>(0)
.unwrap();

let query = entities
#[allow(deprecated)]
let _query = entities
.query()
.with_component::<Location>()
.unwrap()
.with_component::<Size>()
.unwrap()
.run();

assert!(query.indexes.len() == 1 && query.indexes[0] == 1);
}

#[test]
Expand All @@ -91,19 +101,21 @@ fn add_component_to_entity() {
world.register_component::<Location>();
world.register_component::<Size>();
let mut entities = world.entities_write();
entities.create_entity().with_component(Location(10.0, 15.0)).unwrap();
entities
.create_entity()
.with_component(Location(10.0, 15.0))
.unwrap();

entities.add_component_by_entity_id(Size(20.0), 0).unwrap();

let query = entities
#[allow(deprecated)]
let _query = entities
.query()
.with_component::<Location>()
.unwrap()
.with_component::<Size>()
.unwrap()
.run();

assert_eq!(query.indexes.len(), 1);
}

#[test]
Expand All @@ -113,21 +125,32 @@ fn delete_entity() {
world.register_component::<Size>();

let mut entities = world.entities_write();
entities.create_entity().with_component(Location(10.0, 15.0)).unwrap();
entities.create_entity().with_component(Location(20.0, 25.0)).unwrap();
entities
.create_entity()
.with_component(Location(10.0, 15.0))
.unwrap();
entities
.create_entity()
.with_component(Location(20.0, 25.0))
.unwrap();

entities.delete_entity_by_id(0).unwrap();

#[allow(deprecated)]
let query = entities.query().with_component::<Location>().unwrap().run();

#[allow(deprecated)]
let borrowed_location = query.components[0][0].read().unwrap();
let location = borrowed_location.downcast_ref::<Location>().unwrap();

assert!(query.indexes.len() == 1 && location.0 == 20.0);
let _location = borrowed_location.downcast_ref::<Location>().unwrap();

entities.create_entity().with_component(Location(30.0, 35.0)).unwrap();
entities
.create_entity()
.with_component(Location(30.0, 35.0))
.unwrap();

#[allow(deprecated)]
let query = entities.query().with_component::<Location>().unwrap().run();
#[allow(deprecated)]
let borrowed_location = query.components[0][0].read().unwrap();
let location = borrowed_location.downcast_ref::<Location>().unwrap();

Expand Down
10 changes: 7 additions & 3 deletions tests/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn update() {
world.update(&vec![create_u32_entity, create_u32_entity]);

let entities = world.entities_read();
let query = entities.query().with_component::<u32>().unwrap().run();
assert_eq!(query.indexes.len(), 2);
#[allow(deprecated)]
let _query = entities.query().with_component::<u32>().unwrap().run();
}

fn create_u32_entity(world: &World) {
Expand All @@ -22,6 +22,10 @@ fn create_u32_entity(world: &World) {
let entities = query.with_component::<u32>().unwrap().run_entity();

for entity in entities {
*entity.get_component_mut::<u32>().unwrap().downcast_mut::<u32>().unwrap() += 10;
*entity
.get_component_mut::<u32>()
.unwrap()
.downcast_mut::<u32>()
.unwrap() += 10;
}
}

0 comments on commit 7ce9409

Please sign in to comment.