feat(ecs): added a set_vec function to Position2D and Position3D to simply change the position components with a vector instead of manually needing to change it

This commit is contained in:
lisk77 2025-07-13 03:18:23 +02:00
parent ffb4bdf35f
commit fef128f8a7

View file

@ -2,14 +2,11 @@
// You can use these components as is or as a reference to create your own components
// 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::m4;
use crate::math::{
v2,
v3
};
use comet_colors::Color as ColorTrait;
use component_derive::Component;
use crate::math::{v2, v3};
use crate::{Entity, Scene};
use comet_colors::Color as ColorTrait;
use comet_math::m4;
use component_derive::Component;
// ##################################################
// # BASIC #
@ -17,44 +14,44 @@ use crate::{Entity, Scene};
#[derive(Component)]
pub struct Position2D {
position: v2
position: v2,
}
#[derive(Component)]
pub struct Position3D {
position: v3
position: v3,
}
#[derive(Component)]
pub struct Rotation2D {
theta: f32
theta: f32,
}
#[derive(Component)]
pub struct Rotation3D {
theta_x: f32,
theta_y: f32,
theta_z: f32
theta_z: f32,
}
#[derive(Component)]
pub struct Rectangle2D {
position: Position2D,
size: v2
size: v2,
}
#[derive(Component)]
pub struct Render2D {
is_visible: bool,
texture_name: &'static str,
scale: v2
scale: v2,
}
#[derive(Component)]
pub struct Camera2D {
zoom: f32,
dimensions: v2,
priority: u8
priority: u8,
}
#[derive(Component)]
@ -63,7 +60,7 @@ pub struct Text {
font: &'static str,
font_size: f32,
color: Color,
is_visible: bool
is_visible: bool,
}
#[derive(Component)]
@ -71,14 +68,14 @@ pub struct Color {
r: f32,
g: f32,
b: f32,
a: f32
a: f32,
}
#[derive(Component)]
pub struct Timer {
time_stack: f32,
interval: f32,
done: bool
done: bool,
}
// ##################################################
@ -88,13 +85,13 @@ pub struct Timer {
#[derive(Component)]
pub struct Transform2D {
position: Position2D,
rotation: Rotation2D
rotation: Rotation2D,
}
#[derive(Component)]
pub struct Transform3D {
position: Position3D,
rotation: Rotation3D
rotation: Rotation3D,
}
// ##################################################
@ -102,7 +99,9 @@ pub struct Transform3D {
// ##################################################
pub trait Component: Send + Sync + PartialEq + Default + 'static {
fn new() -> Self where Self: Sized;
fn new() -> Self
where
Self: Sized;
fn type_id() -> std::any::TypeId {
std::any::TypeId::of::<Self>()
@ -135,9 +134,7 @@ pub trait Camera {
impl Position2D {
pub fn from_vec(vec: v2) -> Self {
Self {
position: vec
}
Self { position: vec }
}
pub fn as_vec(&self) -> v2 {
@ -159,13 +156,15 @@ impl Position2D {
pub fn set_y(&mut self, new_y: f32) {
self.position.set_y(new_y);
}
pub fn set_vec(&mut self, new_pos: v2) {
self.position = new_pos;
}
}
impl Rotation2D {
pub fn new(angle: f32) -> Self {
Self {
theta: angle
}
Self { theta: angle }
}
pub fn angle(&self) -> f32 {
@ -191,9 +190,7 @@ impl Rotation2D {
impl Position3D {
pub fn from_vec(vec: v3) -> Self {
Self {
position: vec
}
Self { position: vec }
}
pub fn as_vec(&self) -> v3 {
@ -223,14 +220,15 @@ impl Position3D {
pub fn set_z(&mut self, new_z: f32) {
self.position.set_z(new_z);
}
pub fn set_vec(&mut self, new_pos: v3) {
self.position = new_pos;
}
}
impl Rectangle2D {
pub fn new(position: Position2D, size: v2) -> Self {
Self {
position,
size
}
Self { position, size }
}
pub fn position(&self) -> Position2D {
@ -260,10 +258,7 @@ impl Collider for Rectangle2D {
let w2 = other.size().x();
let h2 = other.size().y();
x1 < x2 + w2 &&
x1 + w1 > x2 &&
y1 < y2 + h2 &&
y1 + h1 > y2
x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
}
}
@ -345,7 +340,7 @@ impl Camera2D {
Self {
dimensions,
zoom,
priority
priority,
}
}
@ -388,7 +383,13 @@ impl Camera for Camera2D {
let entities = scene.entities();
let mut visible_entities = Vec::new();
for entity in entities {
if self.in_view_frustum(camera_position, *scene.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());
}
}
@ -406,13 +407,19 @@ impl Camera for Camera2D {
}
impl Text {
pub fn new(content: &'static str, font: &'static str, font_size: f32, is_visible: bool, color: impl ColorTrait) -> Self {
pub fn new(
content: &'static str,
font: &'static str,
font_size: f32,
is_visible: bool,
color: impl ColorTrait,
) -> Self {
Self {
content,
font,
font_size,
color: Color::from_wgpu_color(color.to_wgpu()),
is_visible
is_visible,
}
}
@ -455,12 +462,7 @@ impl Text {
impl Color {
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self {
r,
g,
b,
a
}
Self { r, g, b, a }
}
pub fn r(&self) -> f32 {
@ -500,7 +502,7 @@ impl Color {
r: color.r as f32,
g: color.g as f32,
b: color.b as f32,
a: color.a as f32
a: color.a as f32,
}
}
@ -509,7 +511,7 @@ impl Color {
r: self.r as f64,
g: self.g as f64,
b: self.b as f64,
a: self.a as f64
a: self.a as f64,
}
}
}
@ -535,3 +537,4 @@ impl Timer {
self.done = false;
}
}