feat: moved the ECS related functions to the App to remove unnecessary calling of the World struct inside App.

This commit is contained in:
lisk77 2025-02-23 16:51:31 +01:00
parent f07b829b7d
commit ee3d0bdb9e
7 changed files with 258 additions and 81 deletions

View file

@ -8,6 +8,7 @@ use crate::math::{
Vec3
};
use component_derive::Component;
use crate::Entity;
// ##################################################
// # BASIC #
@ -48,6 +49,17 @@ pub struct Render2D {
scale: Vec2
}
#[derive(Component)]
pub struct Camera2D {
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
zoom: f32
}
// ##################################################
// # BUNDLES #
// ##################################################
@ -91,6 +103,10 @@ pub trait Render {
fn set_texture(&mut self, texture: &'static str);
}
pub trait Camera {
fn get_visible_entities(&self) -> Vec<Entity>;
}
// ##################################################
// # IMPLS #
// ##################################################
@ -170,11 +186,9 @@ impl Rectangle2D {
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
}
@ -261,4 +275,33 @@ impl Transform3D {
pub fn rotation_mut(&mut self) -> &mut Rotation3D {
&mut self.rotation
}
}
impl Camera2D {
pub fn new(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, zoom: f32) -> Self {
Self {
left,
right,
bottom,
top,
near,
far,
zoom
}
}
fn in_view_frustum(&self, camera_pos: Position2D, entity: Position2D) -> bool {
let left = camera_pos.x() - self.zoom;
let right = camera_pos.x() + self.zoom;
let bottom = camera_pos.y() - self.zoom;
let top = camera_pos.y() + self.zoom;
entity.x() < right && entity.x() > left && entity.y() < top && entity.y() > bottom
}
}
impl Camera for Camera2D {
fn get_visible_entities(&self) -> Vec<Entity> {
unimplemented!()
}
}