feat: added a camera with orthographic projection and did some work restructuring the comet_app to make the setup system optional. Input handling is moved to the app

This commit is contained in:
lisk77 2024-11-13 03:33:02 +01:00
parent 780365aeb8
commit 5a9f632e3a
22 changed files with 1173 additions and 349 deletions

View file

@ -4,9 +4,10 @@ version = "0.2.0"
edition = "2021"
[dependencies]
bit-set = "0.8.0"
component_derive = { path = "./component_derive" }
comet_math = { path = "../comet_math" }
comet_resources = { path = "../comet_resources" }
comet_log = { path = "../comet_log" }
chrono = "0.4"
chrono = "0.4"
bit-set = "0.8.0"

View file

@ -1,22 +1,11 @@
//use comet_resources::Vertex;
use std::cell::RefCell;
use std::rc::Rc;
use crate::math::{
Vec2,
Vec3
};
use component_derive::Component;
pub trait Component: Send + Sync + PartialEq + Default + 'static {
fn new() -> Self where Self: Sized;
fn type_id() -> std::any::TypeId {
std::any::TypeId::of::<Self>()
}
fn type_name() -> String {
std::any::type_name::<Self>().to_string()
}
}
// ##################################################
// # BASIC #
// ##################################################
@ -43,6 +32,12 @@ pub struct Rotation3D {
theta_z: f32
}
#[derive(Component)]
pub struct Rectangle2D{
position: Position2D,
size: Vec2
}
#[derive(Component)]
pub struct Renderer2D {
is_visible: bool,
@ -50,6 +45,53 @@ pub struct Renderer2D {
scale: Vec2
}
// ##################################################
// # BUNDLES #
// ##################################################
#[derive(Component)]
pub struct Transform2D {
position: Position2D,
rotation: Rotation2D
}
#[derive(Component)]
pub struct Transform3D {
position: Position3D,
rotation: Rotation3D
}
// ##################################################
// # TRAITS #
// ##################################################
pub trait Component: Send + Sync + PartialEq + Default + 'static {
fn new() -> Self where Self: Sized;
fn type_id() -> std::any::TypeId {
std::any::TypeId::of::<Self>()
}
fn type_name() -> String {
std::any::type_name::<Self>().to_string()
}
}
pub trait Collider {
fn is_colliding(&self, other: &Self) -> bool;
}
pub trait Render {
fn is_visible(&self) -> bool;
fn set_visibility(&mut self, is_visible: bool);
fn get_texture(&self) -> String;
fn set_texture(&mut self, texture: &'static str);
}
// ##################################################
// # IMPLS #
// ##################################################
impl Position2D {
pub fn from_vec(vec: Vec2) -> Self {
Self {
@ -114,12 +156,44 @@ impl Position3D {
}
}
pub trait Render {
fn is_visible(&self) -> bool;
fn set_visibility(&mut self, is_visible: bool);
fn get_texture(&self) -> String;
fn set_texture(&mut self, texture: &'static str);
//fn get_vertex_data(&self) -> Vec<Vertex>;
impl Rectangle2D {
pub fn new(position: Position2D, size: Vec2) -> Self {
Self {
position,
size
}
}
pub fn position(&self) -> Position2D {
self.position
}
pub fn set_position(&mut self, position: Position2D) {
self.position = position;
}
pub fn size(&self) -> Vec2 {
self.size
}
}
impl Collider for Rectangle2D {
fn is_colliding(&self, other: &Self) -> bool {
let x1 = self.position().x();
let y1 = self.position().y();
let w1 = self.size().x();
let h1 = self.size().y();
let x2 = other.position().x();
let y2 = other.position().y();
let w2 = other.size().x();
let h2 = other.size().y();
x1 < x2 + w2 &&
x1 + w1 > x2 &&
y1 < y2 + h2 &&
y1 + h1 > y2
}
}
impl Render for Renderer2D {
@ -141,31 +215,6 @@ impl Render for Renderer2D {
fn set_texture(&mut self, texture: &'static str) {
self.texture = texture;
}
/*fn get_vertex_data(&self) -> Vec<Vertex> {
vec![
Vertex::new([0.0, 0.0, 0.0], [0.0, 0.0]),
Vertex::new([1.0, 0.0, 0.0], [1.0, 0.0]),
Vertex::new([1.0, 1.0, 0.0], [1.0, 1.0]),
Vertex::new([0.0, 1.0, 0.0], [0.0, 1.0])
]
}*/
}
// ##################################################
// # BUNDLES #
// ##################################################
#[derive(Component)]
pub struct Transform2D {
position: Position2D,
rotation: Rotation2D
}
#[derive(Component)]
pub struct Transform3D {
position: Position3D,
rotation: Rotation3D
}
impl Transform2D {
@ -184,6 +233,13 @@ impl Transform2D {
pub fn rotation_mut(&mut self) -> &mut Rotation2D {
&mut self.rotation
}
pub fn translate(&mut self, displacement: Vec2) {
let x = self.position().x() + displacement.x();
let y = self.position().y() + displacement.y();
self.position_mut().set_x(x);
self.position_mut().set_y(y);
}
}
impl Transform3D {
@ -202,6 +258,4 @@ impl Transform3D {
pub fn rotation_mut(&mut self) -> &mut Rotation3D {
&mut self.rotation
}
}
}

View file

@ -23,16 +23,16 @@ pub struct World {
}
impl World {
pub fn new(dimension: &str) -> Self {
pub fn new(application: &str) -> Self {
let mut component_storage = ComponentStorage::new();
match dimension {
match application {
"2D" => component_storage.register_component::<Transform2D>(0),
"3D" => component_storage.register_component::<Transform3D>(0),
_ => {}
}
Self {
dimension: dimension.to_string(),
dimension: application.to_string(),
id_queue: IdQueue::new(),
next_id: 0,
entities: Vec::new(),
@ -118,7 +118,7 @@ impl World {
self.id_queue.sorted_enqueue(entity_id as u32);
self.get_next_id();
self.remove_entity_from_archetype_subsets(entity_id as u32, self.get_component_set(entity_id));
info!(format!("Deleted entity! ID: {}", entity_id));
info!("Deleted entity! ID: {}", entity_id);
}
fn create_archetype(&mut self, components: ComponentSet) {
@ -179,12 +179,12 @@ impl World {
pub fn register_component<T: Component + 'static>(&mut self) {
self.components.register_component::<T>(self.entities.len());
self.create_archetype(ComponentSet::from_ids(vec![T::type_id()]));
info!(format!("Registered component: {}", T::type_name()));
info!("Registered component: {}", T::type_name());
}
pub fn deregister_component<T: Component + 'static>(&mut self) {
self.components.deregister_component::<T>();
info!(format!("Deregistered component: {}", T::type_name()));
info!("Deregistered component: {}", T::type_name());
}
pub fn add_component<T: Component + 'static>(&mut self, entity_id: usize, component: T) {
@ -201,14 +201,13 @@ impl World {
if self.get_component_set(entity_id) != ComponentSet::from_ids(vec![T::type_id()]) {
self.add_entity_to_archetype(entity_id as u32, self.get_component_set(entity_id));
}
info!(format!("Added component {} to entity {}", T::type_name(), entity_id));
debug!(format!("{:?}", self.archetypes));
info!("Added component {} to entity {}", T::type_name(), entity_id);
}
pub fn remove_component<T: Component + 'static>(&mut self, entity_id: usize) {
self.components.remove_component::<T>(entity_id);
self.remove_entity_from_archetype_subsets(entity_id as u32, self.get_component_set(entity_id));
info!(format!("Removed component {} from entity {}", T::type_name(), entity_id));
info!("Removed component {} from entity {}", T::type_name(), entity_id);
}
pub fn get_component<T: Component + 'static>(&self, entity_id: usize) -> &T {