mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 13:58:49 +00:00
feat: added 2D scene rendering and initialization of the texture atlas
This commit is contained in:
parent
878e220249
commit
780365aeb8
12 changed files with 280 additions and 122 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::path::Path;
|
||||
//use comet_resources::Vertex;
|
||||
use crate::math::{
|
||||
Vec2,
|
||||
Vec3
|
||||
|
|
@ -23,15 +23,12 @@ pub trait Component: Send + Sync + PartialEq + Default + 'static {
|
|||
|
||||
#[derive(Component)]
|
||||
pub struct Position2D {
|
||||
x: f32,
|
||||
y: f32
|
||||
position: Vec2
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Position3D {
|
||||
x: f32,
|
||||
y: f32,
|
||||
z: f32
|
||||
position: Vec3
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
@ -50,83 +47,111 @@ pub struct Rotation3D {
|
|||
pub struct Renderer2D {
|
||||
is_visible: bool,
|
||||
texture: &'static str,
|
||||
scale: f32
|
||||
scale: Vec2
|
||||
}
|
||||
|
||||
impl Position2D {
|
||||
pub fn from_vec(vec: Vec2) -> Self {
|
||||
Self {
|
||||
x: vec.x(),
|
||||
y: vec.y()
|
||||
position: vec
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_vec(&self) -> Vec2 {
|
||||
Vec2::new(
|
||||
self.x,
|
||||
self.y
|
||||
)
|
||||
self.position
|
||||
}
|
||||
|
||||
pub fn x(&self) -> &f32 {
|
||||
&self.x
|
||||
pub fn x(&self) -> f32 {
|
||||
self.position.x()
|
||||
}
|
||||
|
||||
pub fn y(&self) -> &f32 {
|
||||
&self.y
|
||||
pub fn y(&self) -> f32 {
|
||||
self.position.y()
|
||||
}
|
||||
|
||||
pub fn set_x(&mut self, new_x: f32) {
|
||||
self.x = new_x;
|
||||
self.position.set_x(new_x);
|
||||
}
|
||||
|
||||
pub fn set_y(&mut self, new_y: f32) {
|
||||
self.y = new_y;
|
||||
self.position.set_y(new_y);
|
||||
}
|
||||
}
|
||||
|
||||
impl Position3D {
|
||||
pub fn from_vec(vec: Vec3) -> Self {
|
||||
Self {
|
||||
x: vec.x(),
|
||||
y: vec.y(),
|
||||
z: vec.z()
|
||||
position: vec
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_vec(&self) -> Vec3 {
|
||||
Vec3::new(
|
||||
self.x,
|
||||
self.y,
|
||||
self.z
|
||||
)
|
||||
self.position
|
||||
}
|
||||
|
||||
pub fn x(&self) -> &f32 {
|
||||
&self.x
|
||||
pub fn x(&self) -> f32 {
|
||||
self.position.x()
|
||||
}
|
||||
|
||||
pub fn y(&self) -> &f32 {
|
||||
&self.y
|
||||
pub fn y(&self) -> f32 {
|
||||
self.position.y()
|
||||
}
|
||||
|
||||
pub fn z(&self) -> &f32 {
|
||||
&self.z
|
||||
pub fn z(&self) -> f32 {
|
||||
self.position.z()
|
||||
}
|
||||
|
||||
pub fn set_x(&mut self, new_x: f32) {
|
||||
self.x = new_x;
|
||||
self.position.set_x(new_x);
|
||||
}
|
||||
|
||||
pub fn set_y(&mut self, new_y: f32) {
|
||||
self.y = new_y;
|
||||
self.position.set_y(new_y);
|
||||
}
|
||||
|
||||
pub fn set_z(&mut self, new_z: f32) {
|
||||
self.z = new_z
|
||||
self.position.set_z(new_z);
|
||||
}
|
||||
}
|
||||
|
||||
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 Render for Renderer2D {
|
||||
fn is_visible(&self) -> bool {
|
||||
self.is_visible
|
||||
}
|
||||
|
||||
fn set_visibility(&mut self, is_visible: bool) {
|
||||
self.is_visible = is_visible;
|
||||
}
|
||||
|
||||
fn get_texture(&self) -> String {
|
||||
self.texture.clone().parse().unwrap()
|
||||
}
|
||||
|
||||
/// Use the actual file name of the texture instead of the path
|
||||
/// e.g. "comet_icon.png" instead of "resources/textures/comet_icon.png"
|
||||
/// The resource manager will already look in the resources/textures folder
|
||||
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 #
|
||||
// ##################################################
|
||||
|
|
|
|||
|
|
@ -411,6 +411,17 @@ impl ComponentStorage {
|
|||
self.keys.contains(type_id)
|
||||
}
|
||||
|
||||
pub fn contains_components(&self, component_set: ComponentSet) -> bool {
|
||||
let mut contains = true;
|
||||
for type_id in component_set.set.iter() {
|
||||
if !self.keys.contains(type_id) {
|
||||
contains = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
contains
|
||||
}
|
||||
|
||||
pub fn get<T: Component + 'static>(&self) -> Option<&SparseSet> {
|
||||
self.components.get(*self.index_map.get(&T::type_id()).unwrap())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,6 +125,25 @@ impl World {
|
|||
self.archetypes.create_archetype(components);
|
||||
}
|
||||
|
||||
fn remove_archetype(&mut self, components: ComponentSet) {
|
||||
self.archetypes.remove_archetype(&components);
|
||||
}
|
||||
|
||||
fn remove_archetype_subsets(&mut self, components: ComponentSet) {
|
||||
let component_sets = self.archetypes.component_sets();
|
||||
let keys: Vec<ComponentSet> = component_sets.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, &ref elem)| if elem.is_subset(&components) { Some(i) } else { None })
|
||||
.collect::<Vec<usize>>()
|
||||
.iter()
|
||||
.map(|index| component_sets[*index].clone())
|
||||
.collect::<Vec<ComponentSet>>();
|
||||
|
||||
for key in keys {
|
||||
self.remove_archetype(key.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn add_entity_to_archetype(&mut self, entity_id: u32, components: ComponentSet) {
|
||||
self.archetypes.add_entity_to_archetype(&components, entity_id);
|
||||
}
|
||||
|
|
@ -159,6 +178,7 @@ 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()));
|
||||
}
|
||||
|
||||
|
|
@ -177,8 +197,10 @@ impl World {
|
|||
if !self.archetypes.contains_archetype(&self.get_component_set(entity_id)) {
|
||||
self.create_archetype(self.get_component_set(entity_id));
|
||||
}
|
||||
self.add_entity_to_archetype(entity_id as u32, self.get_component_set(entity_id));
|
||||
|
||||
self.add_entity_to_archetype(entity_id as u32, ComponentSet::from_ids(vec![T::type_id()]));
|
||||
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));
|
||||
}
|
||||
|
|
@ -191,7 +213,7 @@ impl World {
|
|||
|
||||
pub fn get_component<T: Component + 'static>(&self, entity_id: usize) -> &T {
|
||||
assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id);
|
||||
assert!(self.components.get_component::<T>(entity_id) != None, "There is no component {} bound to the entity {} in the world!", T::type_name(), entity_id);
|
||||
//assert_ne!(self.components.get_component::<T>(entity_id), None, "There is no component {} bound to the entity {} in the world!", T::type_name(), entity_id);
|
||||
self.components.get_component::<T>(entity_id).unwrap()
|
||||
}
|
||||
|
||||
|
|
@ -203,7 +225,7 @@ impl World {
|
|||
|
||||
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);
|
||||
info!(format!("Querying entities with components: {:?}", components));
|
||||
//debug!(format!("Querying entities with components: {:?}", components));
|
||||
self.archetypes.get_archetype(&components).unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue