diff --git a/README.md b/README.md index 7fa5ecc..9c00ad3 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {} fn main() { App::new() // Generate a basic 2D app - .with_preset(App2D) // Pre-registers the `Transform2D` component in the world + .with_preset(App2D) // Pre-registers the `Transform2D` component in the scene .with_title("Comet App") // Sets the window title .with_icon(r"resources/textures/comet_icon.png") // Sets the window icon .with_size(1920, 1080) // Sets the window size @@ -94,7 +94,7 @@ fn main() -> Result<()> { - [ ] (out of the box) Rendering - [ ] 2D - [x] texture rendering - - [x] world rendering + - [x] scene rendering - [ ] text rendering - [ ] 3D - [ ] flexible camera system @@ -120,7 +120,7 @@ fn main() -> Result<()> { - [x] Components - [x] Entities - [x] Archetypes - - [ ] World + - [ ] Scene - [x] general management - [ ] saving - [ ] loading diff --git a/crates/comet_app/src/app.rs b/crates/comet_app/src/app.rs index e33d9c2..1211716 100644 --- a/crates/comet_app/src/app.rs +++ b/crates/comet_app/src/app.rs @@ -4,7 +4,7 @@ use std::sync::atomic::AtomicBool; use std::thread; use std::time::{Duration, Instant}; use crossbeam_channel::bounded; -use comet_ecs::{Camera2D, Component, Entity, Render, Render2D, Transform2D, Transform3D, World}; +use comet_ecs::{Camera2D, Component, Entity, Render, Render2D, Scene, Transform2D, Transform3D}; use comet_resources::{ResourceManager, Vertex}; use comet_renderer::renderer2d::Renderer2D; @@ -48,7 +48,7 @@ pub struct App { delta_time: f32, update_timer: f32, game_state: Option>, - world: World, + scene: Scene, fullscreen: bool, should_quit: bool } @@ -64,7 +64,7 @@ impl App { delta_time: 0.0, update_timer: 0.0166667, game_state: None, - world: World::new(), + scene: Scene::new(), fullscreen: false, should_quit: false } @@ -99,13 +99,13 @@ impl App { match preset { ApplicationType::App2D => { info!("Creating 2D app!"); - self.world.register_component::(); - self.world.register_component::(); - self.world.register_component::() + self.scene.register_component::(); + self.scene.register_component::(); + self.scene.register_component::() }, ApplicationType::App3D => { info!("Creating 3D app!"); - self.world.register_component::() + self.scene.register_component::() } }; self @@ -132,8 +132,8 @@ impl App { self.game_state.as_mut()?.downcast_mut::() } - pub fn world(&self) -> &World { - &self.world + pub fn scene(&self) -> &Scene { + &self.scene } pub fn input_manager(&self) -> &WinitInputHelper { @@ -153,47 +153,47 @@ impl App { } pub fn new_entity(&mut self) -> usize{ - self.world.new_entity() as usize + self.scene.new_entity() as usize } pub fn delete_entity(&mut self, entity_id: usize) { - self.world.delete_entity(entity_id) + self.scene.delete_entity(entity_id) } pub fn get_entity(&self, entity_id: usize) -> Option<&Entity> { - self.world.get_entity(entity_id) + self.scene.get_entity(entity_id) } pub fn get_entity_mut(&mut self, entity_id: usize) -> Option<&mut Entity> { - self.world.get_entity_mut(entity_id) + self.scene.get_entity_mut(entity_id) } pub fn register_component(&mut self) { - self.world.register_component::() + self.scene.register_component::() } pub fn deregister_component(&mut self) { - self.world.deregister_component::() + self.scene.deregister_component::() } pub fn add_component(&mut self, entity_id: usize, component: C) { - self.world.add_component(entity_id, component) + self.scene.add_component(entity_id, component) } pub fn remove_component(&mut self, entity_id: usize) { - self.world.remove_component::(entity_id) + self.scene.remove_component::(entity_id) } pub fn get_component(&self, entity_id: usize) -> Option<&C> { - self.world.get_component::(entity_id) + self.scene.get_component::(entity_id) } pub fn get_component_mut(&mut self, entity_id: usize) -> Option<&mut C> { - self.world.get_component_mut::(entity_id) + self.scene.get_component_mut::(entity_id) } pub fn get_entities_with(&self, components: ComponentSet) -> Vec { - self.world.get_entities_with(components) + self.scene.get_entities_with(components) } pub fn quit(&mut self) { diff --git a/crates/comet_ecs/src/component.rs b/crates/comet_ecs/src/component.rs index 0d9fa5e..78e260c 100644 --- a/crates/comet_ecs/src/component.rs +++ b/crates/comet_ecs/src/component.rs @@ -9,7 +9,7 @@ use crate::math::{ Vec3 }; use component_derive::Component; -use crate::{Entity, World}; +use crate::{Entity, Scene}; // ################################################## // # BASIC # @@ -101,7 +101,7 @@ pub trait Render { } pub trait Camera { - fn get_visible_entities(&self, camera_position: Position2D, world: World) -> Vec; + fn get_visible_entities(&self, camera_position: Position2D, scene: Scene) -> Vec; fn get_projection_matrix(&self) -> Mat4; } @@ -328,11 +328,11 @@ impl Camera2D { } impl Camera for Camera2D { - fn get_visible_entities(&self, camera_position: Position2D, world: World) -> Vec { - let entities = world.entities(); + fn get_visible_entities(&self, camera_position: Position2D, scene: Scene) -> Vec { + let entities = scene.entities(); let mut visible_entities = Vec::new(); for entity in entities { - if self.in_view_frustum(camera_position, *world.get_component::(*entity.clone().unwrap().id() as usize).unwrap().position()) { + if self.in_view_frustum(camera_position, *scene.get_component::(*entity.clone().unwrap().id() as usize).unwrap().position()) { visible_entities.push(entity.clone().unwrap()); } } diff --git a/crates/comet_ecs/src/lib.rs b/crates/comet_ecs/src/lib.rs index 8d85a74..53429a6 100644 --- a/crates/comet_ecs/src/lib.rs +++ b/crates/comet_ecs/src/lib.rs @@ -1,12 +1,12 @@ pub use entity::*; pub use component::*; -pub use world::*; +pub use scene::*; pub use id::*; pub use component_derive::*; pub use comet_math as math; mod entity; mod component; -mod world; +mod scene; mod id; mod archetypes; \ No newline at end of file diff --git a/crates/comet_ecs/src/world.rs b/crates/comet_ecs/src/scene.rs similarity index 96% rename from crates/comet_ecs/src/world.rs rename to crates/comet_ecs/src/scene.rs index d974593..a567a51 100644 --- a/crates/comet_ecs/src/world.rs +++ b/crates/comet_ecs/src/scene.rs @@ -11,7 +11,7 @@ use comet_log::*; use comet_structs::*; use crate::archetypes::Archetypes; -pub struct World { +pub struct Scene { id_queue: IdQueue, next_id: u32, entities: Vec>, @@ -19,7 +19,7 @@ pub struct World { archetypes: Archetypes } -impl World { +impl Scene { pub fn new() -> Self { Self { id_queue: IdQueue::new(), @@ -30,7 +30,7 @@ impl World { } } - /// Returns the number of how many entities exist in the current World. + /// Returns the number of how many entities exist in the current Scene. pub fn active_entities(&self) -> u32 { self.entities.len() as u32 - self.id_queue.size() } @@ -45,7 +45,7 @@ impl World { } } - /// Retuns the `Vec` of `Option` which contains all the entities in the current World. + /// Retuns the `Vec` of `Option` which contains all the entities in the current Scene. pub fn entities(&self) -> &Vec> { &self.entities } @@ -86,8 +86,6 @@ impl World { } self.id_queue.sorted_enqueue(entity_id as u32); self.get_next_id(); - - } fn create_archetype(&mut self, components: ComponentSet) { @@ -156,14 +154,14 @@ impl World { ComponentSet::from_ids(type_ids) } - /// Registers a new component in the world. + /// Registers a new component in the scene. pub fn register_component(&mut self) { self.components.register_component::(self.entities.len()); self.create_archetype(ComponentSet::from_ids(vec![C::type_id()])); info!("Registered component: {}", C::type_name()); } - /// Deregisters a component from the world. + /// Deregisters a component from the scene. pub fn deregister_component(&mut self) { self.components.deregister_component::(); info!("Deregistered component: {}", C::type_name()); @@ -207,7 +205,7 @@ impl World { if self.archetypes.contains_archetype(&components) { return self.archetypes.get_archetype(&components).unwrap().clone().iter().map(|x| *x as usize).collect(); } - error!("The given components {:?} are not registered in the world!", components); + error!("The given components {:?} are not registered in the scene!", components); Vec::new() } } diff --git a/crates/comet_renderer/src/renderer2d.rs b/crates/comet_renderer/src/renderer2d.rs index fa3eb26..e67e099 100644 --- a/crates/comet_renderer/src/renderer2d.rs +++ b/crates/comet_renderer/src/renderer2d.rs @@ -8,7 +8,7 @@ use wgpu::util::DeviceExt; use winit::dpi::PhysicalSize; use winit::window::Window; use comet_colors::LinearRgba; -use comet_ecs::{Camera, Camera2D, Component, Position2D, Render, Render2D, Transform2D, World}; +use comet_ecs::{Camera, Camera2D, Component, Position2D, Render, Render2D, Transform2D, Scene}; use comet_log::{debug, info}; use comet_math::{Point3, Vec2, Vec3}; use comet_resources::{texture, graphic_resource_manager::GraphicResorceManager, Texture, Vertex}; @@ -757,15 +757,15 @@ impl<'a> Renderer2D<'a> { position } - fn setup_camera<'b>(&mut self, cameras: Vec, world: &'b World) -> (&'b Position2D, &'b Camera2D){ + fn setup_camera<'b>(&mut self, cameras: Vec, scene: &'b Scene) -> (&'b Position2D, &'b Camera2D){ let cam = cameras.get( self.find_priority_camera( - cameras.iter().map(|e| *world.get_component::(*e).unwrap() + cameras.iter().map(|e| *scene.get_component::(*e).unwrap() ).collect::>()) ).unwrap(); - let camera_component = world.get_component::(*cam).unwrap(); - let camera_position = world.get_component::(*cam).unwrap().position(); + let camera_component = scene.get_component::(*cam).unwrap(); + let camera_position = scene.get_component::(*cam).unwrap().position(); let camera = RenderCamera::new( camera_component.zoom(), @@ -814,24 +814,24 @@ impl<'a> Renderer2D<'a> { (camera_position, camera_component) } - /// A function to automatically render all the entities of the `World` struct. + /// A function to automatically render all the entities of the `Scene` struct. /// The entities must have the `Render2D` and `Transform2D` components to be rendered as well as set visible. - pub fn render_scene_2d(&mut self, world: &World) { - let cameras = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Camera2D::type_id()])); - let entities = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])); + pub fn render_scene_2d(&mut self, scene: &Scene) { + let cameras = scene.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Camera2D::type_id()])); + let entities = scene.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])); if cameras.is_empty() { return; } - self.setup_camera(cameras, world); + self.setup_camera(cameras, scene); let mut vertex_buffer: Vec = Vec::new(); let mut index_buffer: Vec = Vec::new(); for entity in entities { - let renderer_component = world.get_component::(entity).unwrap(); - let transform_component = world.get_component::(entity).unwrap(); + let renderer_component = scene.get_component::(entity).unwrap(); + let transform_component = scene.get_component::(entity).unwrap(); if renderer_component.is_visible() { let mut position = transform_component.position().clone(); @@ -952,7 +952,7 @@ impl<'a> Renderer2D<'a> { /// A function to render the screen from top to bottom. /// Generally useful in top-down games to render trees in front of players for example. - pub fn render_layered_scene_2d(&mut self, world: &World) { + pub fn render_layered_scene_2d(&mut self, world: &Scene) { let cameras = world.get_entities_with(ComponentSet::from_ids(vec![Camera2D::type_id()])); if cameras == vec![] { diff --git a/examples/README.md b/examples/README.md index 89fb6c3..31f7f8d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,8 +7,8 @@ Simply run cargo run --example ``` -| Example | Description | -|------------------------------------|-----------------------------------------------------------------------------------------| -| [hello_world](hello_world) | A simple boilerplate example to show how to properly start creating a Comet App. | -| [textured_entity](textured_entity) | This covers the basics on how to create a camera and your first entity with a texture | +| Example | Description | +|---------------------------------------------------|-----------------------------------------------------------------------------------------| +| [hello_world](comet/examples/hello_world) | A simple boilerplate example to show how to properly start creating a Comet App. | +| [textured_entity](comet/examples/textured_entity) | This covers the basics on how to create a camera and your first entity with a texture | diff --git a/examples/textured_entity.rs b/examples/textured_entity.rs index 603cc22..f4ab254 100644 --- a/examples/textured_entity.rs +++ b/examples/textured_entity.rs @@ -21,7 +21,7 @@ fn setup(app: &mut App, renderer: &mut Renderer2D) { } fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) { - renderer.render_scene_2d(app.world()) + renderer.render_scene_2d(app.scene()) } fn main() {