refactor(ecs)!: reworked the entity id system

This commit is contained in:
lisk77 2025-11-25 23:11:37 +01:00
parent eced6ddf3f
commit 607bf94f1e
7 changed files with 198 additions and 95 deletions

View file

@ -1,5 +1,7 @@
use comet_colors::{Color as ColorTrait, LinearRgba};
use comet_ecs::{Camera2D, Component, Entity, Render2D, Scene, Text, Transform2D, Transform3D};
use comet_ecs::{
Camera2D, Component, Entity, EntityId, Render2D, Scene, Text, Transform2D, Transform3D,
};
use comet_input::keyboard::Key;
use comet_log::*;
use comet_renderer::renderer::Renderer;
@ -163,22 +165,22 @@ impl App {
}
/// Creates a new entity and returns its ID.
pub fn new_entity(&mut self) -> usize {
self.scene.new_entity() as usize
pub fn new_entity(&mut self) -> EntityId {
self.scene.new_entity()
}
/// Deletes an entity by its ID.
pub fn delete_entity(&mut self, entity_id: usize) {
pub fn delete_entity(&mut self, entity_id: EntityId) {
self.scene.delete_entity(entity_id)
}
/// Gets an immutable reference to an entity by its ID.
pub fn get_entity(&self, entity_id: usize) -> Option<&Entity> {
pub fn get_entity(&self, entity_id: EntityId) -> Option<&Entity> {
self.scene.get_entity(entity_id)
}
/// Gets a mutable reference to an entity by its ID.
pub fn get_entity_mut(&mut self, entity_id: usize) -> Option<&mut Entity> {
pub fn get_entity_mut(&mut self, entity_id: EntityId) -> Option<&mut Entity> {
self.scene.get_entity_mut(entity_id)
}
@ -194,29 +196,29 @@ impl App {
/// Adds a component to an entity by its ID and an instance of the component.
/// Overwrites the previous component if another component of the same type is added.
pub fn add_component<C: Component>(&mut self, entity_id: usize, component: C) {
pub fn add_component<C: Component>(&mut self, entity_id: EntityId, component: C) {
self.scene.add_component(entity_id, component)
}
/// Removes a component from an entity by its ID.
pub fn remove_component<C: Component>(&mut self, entity_id: usize) {
pub fn remove_component<C: Component>(&mut self, entity_id: EntityId) {
self.scene.remove_component::<C>(entity_id)
}
/// Returns a reference to a component of an entity by its ID.
pub fn get_component<C: Component>(&self, entity_id: usize) -> Option<&C> {
pub fn get_component<C: Component>(&self, entity_id: EntityId) -> Option<&C> {
self.scene.get_component::<C>(entity_id)
}
/// Returns a mutable reference to a component of an entity by its ID.
pub fn get_component_mut<C: Component>(&mut self, entity_id: usize) -> Option<&mut C> {
pub fn get_component_mut<C: Component>(&mut self, entity_id: EntityId) -> Option<&mut C> {
self.scene.get_component_mut::<C>(entity_id)
}
/// Returns a list of entities that have the given components.
/// The amount of queriable components is limited to 3 such that the `Archetype` creation is more efficient.
/// Otherwise it would be a factorial complexity chaos.
pub fn get_entities_with(&self, components: Vec<TypeId>) -> Vec<usize> {
pub fn get_entities_with(&self, components: Vec<TypeId>) -> Vec<EntityId> {
self.scene.get_entities_with(components)
}
@ -233,7 +235,7 @@ impl App {
}
/// Returns whether an entity has the given component.
pub fn has<C: Component>(&self, entity_id: usize) -> bool {
pub fn has<C: Component>(&self, entity_id: EntityId) -> bool {
self.scene.has::<C>(entity_id)
}
@ -243,7 +245,7 @@ impl App {
}
/// Spawns a prefab with the given name.
pub fn spawn_prefab(&mut self, name: &str) -> Option<usize> {
pub fn spawn_prefab(&mut self, name: &str) -> Option<EntityId> {
self.scene.spawn_prefab(name)
}