fix: changed the name of World to Scene (because the name fits better)

This commit is contained in:
lisk77 2025-03-11 01:23:33 +01:00
parent e94df6c221
commit ab73b145b1
8 changed files with 55 additions and 57 deletions

View file

@ -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

View file

@ -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<Box<dyn Any>>,
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::<Transform2D>();
self.world.register_component::<Render2D>();
self.world.register_component::<Camera2D>()
self.scene.register_component::<Transform2D>();
self.scene.register_component::<Render2D>();
self.scene.register_component::<Camera2D>()
},
ApplicationType::App3D => {
info!("Creating 3D app!");
self.world.register_component::<Transform3D>()
self.scene.register_component::<Transform3D>()
}
};
self
@ -132,8 +132,8 @@ impl App {
self.game_state.as_mut()?.downcast_mut::<T>()
}
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<C: Component>(&mut self) {
self.world.register_component::<C>()
self.scene.register_component::<C>()
}
pub fn deregister_component<C: Component>(&mut self) {
self.world.deregister_component::<C>()
self.scene.deregister_component::<C>()
}
pub fn add_component<C: 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<C: Component>(&mut self, entity_id: usize) {
self.world.remove_component::<C>(entity_id)
self.scene.remove_component::<C>(entity_id)
}
pub fn get_component<C: Component>(&self, entity_id: usize) -> Option<&C> {
self.world.get_component::<C>(entity_id)
self.scene.get_component::<C>(entity_id)
}
pub fn get_component_mut<C: Component>(&mut self, entity_id: usize) -> Option<&mut C> {
self.world.get_component_mut::<C>(entity_id)
self.scene.get_component_mut::<C>(entity_id)
}
pub fn get_entities_with(&self, components: ComponentSet) -> Vec<usize> {
self.world.get_entities_with(components)
self.scene.get_entities_with(components)
}
pub fn quit(&mut self) {

View file

@ -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<Entity>;
fn get_visible_entities(&self, camera_position: Position2D, scene: Scene) -> Vec<Entity>;
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<Entity> {
let entities = world.entities();
fn get_visible_entities(&self, camera_position: Position2D, scene: Scene) -> Vec<Entity> {
let entities = scene.entities();
let mut visible_entities = Vec::new();
for entity in entities {
if self.in_view_frustum(camera_position, *world.get_component::<Transform2D>(*entity.clone().unwrap().id() as usize).unwrap().position()) {
if self.in_view_frustum(camera_position, *scene.get_component::<Transform2D>(*entity.clone().unwrap().id() as usize).unwrap().position()) {
visible_entities.push(entity.clone().unwrap());
}
}

View file

@ -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;

View file

@ -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<Option<Entity>>,
@ -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<Entity>` which contains all the entities in the current World.
/// Retuns the `Vec` of `Option<Entity>` which contains all the entities in the current Scene.
pub fn entities(&self) -> &Vec<Option<Entity>> {
&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<C: Component + 'static>(&mut self) {
self.components.register_component::<C>(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<C: Component + 'static>(&mut self) {
self.components.deregister_component::<C>();
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()
}
}

View file

@ -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<usize>, world: &'b World) -> (&'b Position2D, &'b Camera2D){
fn setup_camera<'b>(&mut self, cameras: Vec<usize>, scene: &'b Scene) -> (&'b Position2D, &'b Camera2D){
let cam = cameras.get(
self.find_priority_camera(
cameras.iter().map(|e| *world.get_component::<Camera2D>(*e).unwrap()
cameras.iter().map(|e| *scene.get_component::<Camera2D>(*e).unwrap()
).collect::<Vec<Camera2D>>())
).unwrap();
let camera_component = world.get_component::<Camera2D>(*cam).unwrap();
let camera_position = world.get_component::<Transform2D>(*cam).unwrap().position();
let camera_component = scene.get_component::<Camera2D>(*cam).unwrap();
let camera_position = scene.get_component::<Transform2D>(*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<Vertex> = Vec::new();
let mut index_buffer: Vec<u16> = Vec::new();
for entity in entities {
let renderer_component = world.get_component::<Render2D>(entity).unwrap();
let transform_component = world.get_component::<Transform2D>(entity).unwrap();
let renderer_component = scene.get_component::<Render2D>(entity).unwrap();
let transform_component = scene.get_component::<Transform2D>(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![] {

View file

@ -7,8 +7,8 @@ Simply run
cargo run --example <example_name>
```
| 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 |

View file

@ -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() {