mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 05:48:50 +00:00
feat: added the beginnings of a ecs based camera system. render_scene_2d crashes miserably right now but theoretically everything *should* be in place for a full adoption
This commit is contained in:
parent
a9a8d076ca
commit
4ce24b58dd
5 changed files with 128 additions and 33 deletions
|
|
@ -3,12 +3,13 @@
|
|||
// Also just as a nomenclature: bundles are a component made up of multiple components,
|
||||
// so it's a collection of components bundled together (like Transform2D)
|
||||
|
||||
use comet_math::Mat4;
|
||||
use crate::math::{
|
||||
Vec2,
|
||||
Vec3
|
||||
};
|
||||
use component_derive::Component;
|
||||
use crate::Entity;
|
||||
use crate::{Entity, World};
|
||||
|
||||
// ##################################################
|
||||
// # BASIC #
|
||||
|
|
@ -51,13 +52,8 @@ pub struct Render2D {
|
|||
|
||||
#[derive(Component)]
|
||||
pub struct Camera2D {
|
||||
left: f32,
|
||||
right: f32,
|
||||
bottom: f32,
|
||||
top: f32,
|
||||
near: f32,
|
||||
far: f32,
|
||||
zoom: f32
|
||||
zoom: f32,
|
||||
dimensions: Vec2,
|
||||
}
|
||||
|
||||
// ##################################################
|
||||
|
|
@ -104,7 +100,8 @@ pub trait Render {
|
|||
}
|
||||
|
||||
pub trait Camera {
|
||||
fn get_visible_entities(&self) -> Vec<Entity>;
|
||||
fn get_visible_entities(&self, camera_position: Position2D, world: World) -> Vec<Entity>;
|
||||
fn get_projection_matrix(&self) -> Mat4;
|
||||
}
|
||||
|
||||
// ##################################################
|
||||
|
|
@ -278,18 +275,29 @@ impl Transform3D {
|
|||
}
|
||||
|
||||
impl Camera2D {
|
||||
pub fn new(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, zoom: f32) -> Self {
|
||||
pub fn new(dimensions: Vec2, zoom: f32) -> Self {
|
||||
Self {
|
||||
left,
|
||||
right,
|
||||
bottom,
|
||||
top,
|
||||
near,
|
||||
far,
|
||||
dimensions,
|
||||
zoom
|
||||
}
|
||||
}
|
||||
|
||||
pub fn zoom(&self) -> f32 {
|
||||
self.zoom
|
||||
}
|
||||
|
||||
pub fn set_zoom(&mut self, zoom: f32) {
|
||||
self.zoom = zoom;
|
||||
}
|
||||
|
||||
pub fn dimensions(&self) -> Vec2 {
|
||||
self.dimensions
|
||||
}
|
||||
|
||||
pub fn set_dimensions(&mut self, dimensions: Vec2) {
|
||||
self.dimensions = dimensions;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
@ -301,7 +309,23 @@ impl Camera2D {
|
|||
}
|
||||
|
||||
impl Camera for Camera2D {
|
||||
fn get_visible_entities(&self) -> Vec<Entity> {
|
||||
unimplemented!()
|
||||
fn get_visible_entities(&self, camera_position: Position2D, world: World) -> Vec<Entity> {
|
||||
let entities = world.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()) {
|
||||
visible_entities.push(entity.clone().unwrap());
|
||||
}
|
||||
}
|
||||
visible_entities
|
||||
}
|
||||
|
||||
fn get_projection_matrix(&self) -> Mat4 {
|
||||
let left = -self.dimensions.x() / 2.0;
|
||||
let right = self.dimensions.x() / 2.0;
|
||||
let bottom = -self.dimensions.y() / 2.0;
|
||||
let top = self.dimensions.y() / 2.0;
|
||||
|
||||
Mat4::OPENGL * Mat4::orthographic_matrix(left, right, bottom, top, 1.0, 0.0)
|
||||
}
|
||||
}
|
||||
|
|
@ -200,7 +200,10 @@ impl World {
|
|||
|
||||
/// Returns a list of entities that have the given components.
|
||||
pub fn get_entities_with(&self, components: ComponentSet) -> Vec<u32> {
|
||||
assert!(self.archetypes.contains_archetype(&components), "The given components {:?} are not registered in the world!", components);
|
||||
self.archetypes.get_archetype(&components).unwrap().clone()
|
||||
//assert!(self.archetypes.contains_archetype(&components), "The given components {:?} are not registered in the world!", components);
|
||||
if self.archetypes.contains_archetype(&components) {
|
||||
return self.archetypes.get_archetype(&components).unwrap().clone();
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue