mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 05:48:50 +00:00
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:
parent
ffb4bdf35f
commit
fef128f8a7
1 changed files with 356 additions and 353 deletions
|
|
@ -2,14 +2,11 @@
|
||||||
// You can use these components as is or as a reference to create your own components
|
// 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,
|
// 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)
|
// so it's a collection of components bundled together (like Transform2D)
|
||||||
use comet_math::m4;
|
use crate::math::{v2, v3};
|
||||||
use crate::math::{
|
|
||||||
v2,
|
|
||||||
v3
|
|
||||||
};
|
|
||||||
use comet_colors::Color as ColorTrait;
|
|
||||||
use component_derive::Component;
|
|
||||||
use crate::{Entity, Scene};
|
use crate::{Entity, Scene};
|
||||||
|
use comet_colors::Color as ColorTrait;
|
||||||
|
use comet_math::m4;
|
||||||
|
use component_derive::Component;
|
||||||
|
|
||||||
// ##################################################
|
// ##################################################
|
||||||
// # BASIC #
|
// # BASIC #
|
||||||
|
|
@ -17,44 +14,44 @@ use crate::{Entity, Scene};
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Position2D {
|
pub struct Position2D {
|
||||||
position: v2
|
position: v2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Position3D {
|
pub struct Position3D {
|
||||||
position: v3
|
position: v3,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Rotation2D {
|
pub struct Rotation2D {
|
||||||
theta: f32
|
theta: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Rotation3D {
|
pub struct Rotation3D {
|
||||||
theta_x: f32,
|
theta_x: f32,
|
||||||
theta_y: f32,
|
theta_y: f32,
|
||||||
theta_z: f32
|
theta_z: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Rectangle2D{
|
pub struct Rectangle2D {
|
||||||
position: Position2D,
|
position: Position2D,
|
||||||
size: v2
|
size: v2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Render2D {
|
pub struct Render2D {
|
||||||
is_visible: bool,
|
is_visible: bool,
|
||||||
texture_name: &'static str,
|
texture_name: &'static str,
|
||||||
scale: v2
|
scale: v2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Camera2D {
|
pub struct Camera2D {
|
||||||
zoom: f32,
|
zoom: f32,
|
||||||
dimensions: v2,
|
dimensions: v2,
|
||||||
priority: u8
|
priority: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|
@ -63,7 +60,7 @@ pub struct Text {
|
||||||
font: &'static str,
|
font: &'static str,
|
||||||
font_size: f32,
|
font_size: f32,
|
||||||
color: Color,
|
color: Color,
|
||||||
is_visible: bool
|
is_visible: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|
@ -71,14 +68,14 @@ pub struct Color {
|
||||||
r: f32,
|
r: f32,
|
||||||
g: f32,
|
g: f32,
|
||||||
b: f32,
|
b: f32,
|
||||||
a: f32
|
a: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Timer {
|
pub struct Timer {
|
||||||
time_stack: f32,
|
time_stack: f32,
|
||||||
interval: f32,
|
interval: f32,
|
||||||
done: bool
|
done: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ##################################################
|
// ##################################################
|
||||||
|
|
@ -88,13 +85,13 @@ pub struct Timer {
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Transform2D {
|
pub struct Transform2D {
|
||||||
position: Position2D,
|
position: Position2D,
|
||||||
rotation: Rotation2D
|
rotation: Rotation2D,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Transform3D {
|
pub struct Transform3D {
|
||||||
position: Position3D,
|
position: Position3D,
|
||||||
rotation: Rotation3D
|
rotation: Rotation3D,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ##################################################
|
// ##################################################
|
||||||
|
|
@ -102,7 +99,9 @@ pub struct Transform3D {
|
||||||
// ##################################################
|
// ##################################################
|
||||||
|
|
||||||
pub trait Component: Send + Sync + PartialEq + Default + 'static {
|
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 {
|
fn type_id() -> std::any::TypeId {
|
||||||
std::any::TypeId::of::<Self>()
|
std::any::TypeId::of::<Self>()
|
||||||
|
|
@ -135,9 +134,7 @@ pub trait Camera {
|
||||||
|
|
||||||
impl Position2D {
|
impl Position2D {
|
||||||
pub fn from_vec(vec: v2) -> Self {
|
pub fn from_vec(vec: v2) -> Self {
|
||||||
Self {
|
Self { position: vec }
|
||||||
position: vec
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_vec(&self) -> v2 {
|
pub fn as_vec(&self) -> v2 {
|
||||||
|
|
@ -159,13 +156,15 @@ impl Position2D {
|
||||||
pub fn set_y(&mut self, new_y: f32) {
|
pub fn set_y(&mut self, new_y: f32) {
|
||||||
self.position.set_y(new_y);
|
self.position.set_y(new_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_vec(&mut self, new_pos: v2) {
|
||||||
|
self.position = new_pos;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rotation2D {
|
impl Rotation2D {
|
||||||
pub fn new(angle: f32) -> Self {
|
pub fn new(angle: f32) -> Self {
|
||||||
Self {
|
Self { theta: angle }
|
||||||
theta: angle
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn angle(&self) -> f32 {
|
pub fn angle(&self) -> f32 {
|
||||||
|
|
@ -191,9 +190,7 @@ impl Rotation2D {
|
||||||
|
|
||||||
impl Position3D {
|
impl Position3D {
|
||||||
pub fn from_vec(vec: v3) -> Self {
|
pub fn from_vec(vec: v3) -> Self {
|
||||||
Self {
|
Self { position: vec }
|
||||||
position: vec
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_vec(&self) -> v3 {
|
pub fn as_vec(&self) -> v3 {
|
||||||
|
|
@ -223,14 +220,15 @@ impl Position3D {
|
||||||
pub fn set_z(&mut self, new_z: f32) {
|
pub fn set_z(&mut self, new_z: f32) {
|
||||||
self.position.set_z(new_z);
|
self.position.set_z(new_z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_vec(&mut self, new_pos: v3) {
|
||||||
|
self.position = new_pos;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rectangle2D {
|
impl Rectangle2D {
|
||||||
pub fn new(position: Position2D, size: v2) -> Self {
|
pub fn new(position: Position2D, size: v2) -> Self {
|
||||||
Self {
|
Self { position, size }
|
||||||
position,
|
|
||||||
size
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn position(&self) -> Position2D {
|
pub fn position(&self) -> Position2D {
|
||||||
|
|
@ -260,10 +258,7 @@ impl Collider for Rectangle2D {
|
||||||
let w2 = other.size().x();
|
let w2 = other.size().x();
|
||||||
let h2 = other.size().y();
|
let h2 = other.size().y();
|
||||||
|
|
||||||
x1 < x2 + w2 &&
|
x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
|
||||||
x1 + w1 > x2 &&
|
|
||||||
y1 < y2 + h2 &&
|
|
||||||
y1 + h1 > y2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -345,7 +340,7 @@ impl Camera2D {
|
||||||
Self {
|
Self {
|
||||||
dimensions,
|
dimensions,
|
||||||
zoom,
|
zoom,
|
||||||
priority
|
priority,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -388,7 +383,13 @@ impl Camera for Camera2D {
|
||||||
let entities = scene.entities();
|
let entities = scene.entities();
|
||||||
let mut visible_entities = Vec::new();
|
let mut visible_entities = Vec::new();
|
||||||
for entity in entities {
|
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());
|
visible_entities.push(entity.clone().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -406,13 +407,19 @@ impl Camera for Camera2D {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Text {
|
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 {
|
Self {
|
||||||
content,
|
content,
|
||||||
font,
|
font,
|
||||||
font_size,
|
font_size,
|
||||||
color: Color::from_wgpu_color(color.to_wgpu()),
|
color: Color::from_wgpu_color(color.to_wgpu()),
|
||||||
is_visible
|
is_visible,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,12 +462,7 @@ impl Text {
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||||
Self {
|
Self { r, g, b, a }
|
||||||
r,
|
|
||||||
g,
|
|
||||||
b,
|
|
||||||
a
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn r(&self) -> f32 {
|
pub fn r(&self) -> f32 {
|
||||||
|
|
@ -500,7 +502,7 @@ impl Color {
|
||||||
r: color.r as f32,
|
r: color.r as f32,
|
||||||
g: color.g as f32,
|
g: color.g as f32,
|
||||||
b: color.b 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,
|
r: self.r as f64,
|
||||||
g: self.g as f64,
|
g: self.g as f64,
|
||||||
b: self.b 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;
|
self.done = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue