rendering, physics, player and camera WIP

This commit is contained in:
Jonas H
2026-01-01 19:54:00 +01:00
commit 5d2eca0393
51 changed files with 8734 additions and 0 deletions

518
src/world.rs Normal file
View File

@@ -0,0 +1,518 @@
use std::collections::HashMap;
use crate::components::jump::JumpComponent;
use crate::components::{
CameraComponent, CameraFollowComponent, InputComponent, MeshComponent, MovementComponent,
PhysicsComponent,
};
use crate::entity::{EntityHandle, EntityManager};
use crate::state::StateMachine;
pub use crate::utility::transform::Transform;
pub struct TransformStorage
{
pub components: HashMap<EntityHandle, Transform>,
}
impl TransformStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: Transform)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&Transform>
{
self.components.get(&entity)
}
pub fn get_mut(&mut self, entity: EntityHandle) -> Option<&mut Transform>
{
self.components.get_mut(&entity)
}
pub fn with<F, R>(&self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&Transform) -> R,
{
self.components.get(&entity).map(f)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut Transform) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct MeshStorage
{
pub components: HashMap<EntityHandle, MeshComponent>,
}
impl MeshStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: MeshComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&MeshComponent>
{
self.components.get(&entity)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct PhysicsStorage
{
pub components: HashMap<EntityHandle, PhysicsComponent>,
}
impl PhysicsStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: PhysicsComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<PhysicsComponent>
{
self.components.get(&entity).copied()
}
pub fn with<F, R>(&self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&PhysicsComponent) -> R,
{
self.components.get(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct MovementStorage
{
pub components: HashMap<EntityHandle, MovementComponent>,
}
impl MovementStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: MovementComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&MovementComponent>
{
self.components.get(&entity)
}
pub fn with<F, R>(&self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&MovementComponent) -> R,
{
self.components.get(&entity).map(f)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut MovementComponent) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct JumpStorage
{
pub components: HashMap<EntityHandle, JumpComponent>,
}
impl JumpStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: JumpComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&JumpComponent>
{
self.components.get(&entity)
}
pub fn get_mut(&mut self, entity: EntityHandle) -> Option<&mut JumpComponent>
{
self.components.get_mut(&entity)
}
pub fn with<F, R>(&self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&JumpComponent) -> R,
{
self.components.get(&entity).map(f)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut JumpComponent) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct InputStorage
{
pub components: HashMap<EntityHandle, InputComponent>,
}
impl InputStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: InputComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&InputComponent>
{
self.components.get(&entity)
}
pub fn with<F, R>(&self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&InputComponent) -> R,
{
self.components.get(&entity).map(f)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut InputComponent) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct PlayerTagStorage
{
pub components: HashMap<EntityHandle, ()>,
}
impl PlayerTagStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle)
{
self.components.insert(entity, ());
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct StateMachineStorage
{
pub components: HashMap<EntityHandle, StateMachine>,
}
impl StateMachineStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: StateMachine)
{
self.components.insert(entity, component);
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut StateMachine) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct CameraStorage
{
pub components: HashMap<EntityHandle, CameraComponent>,
}
impl CameraStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: CameraComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&CameraComponent>
{
self.components.get(&entity)
}
pub fn get_mut(&mut self, entity: EntityHandle) -> Option<&mut CameraComponent>
{
self.components.get_mut(&entity)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut CameraComponent) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
pub fn get_active(&self) -> Option<(EntityHandle, &CameraComponent)>
{
self.components
.iter()
.find(|(_, cam)| cam.is_active)
.map(|(e, c)| (*e, c))
}
}
pub struct CameraFollowStorage
{
pub components: HashMap<EntityHandle, CameraFollowComponent>,
}
impl CameraFollowStorage
{
pub fn new() -> Self
{
Self {
components: HashMap::new(),
}
}
pub fn insert(&mut self, entity: EntityHandle, component: CameraFollowComponent)
{
self.components.insert(entity, component);
}
pub fn get(&self, entity: EntityHandle) -> Option<&CameraFollowComponent>
{
self.components.get(&entity)
}
pub fn get_mut(&mut self, entity: EntityHandle) -> Option<&mut CameraFollowComponent>
{
self.components.get_mut(&entity)
}
pub fn with_mut<F, R>(&mut self, entity: EntityHandle, f: F) -> Option<R>
where
F: FnOnce(&mut CameraFollowComponent) -> R,
{
self.components.get_mut(&entity).map(f)
}
pub fn remove(&mut self, entity: EntityHandle)
{
self.components.remove(&entity);
}
pub fn all(&self) -> Vec<EntityHandle>
{
self.components.keys().copied().collect()
}
}
pub struct World
{
pub entities: EntityManager,
pub transforms: TransformStorage,
pub meshes: MeshStorage,
pub physics: PhysicsStorage,
pub movements: MovementStorage,
pub jumps: JumpStorage,
pub inputs: InputStorage,
pub player_tags: PlayerTagStorage,
pub state_machines: StateMachineStorage,
pub cameras: CameraStorage,
pub camera_follows: CameraFollowStorage,
}
impl World
{
pub fn new() -> Self
{
Self {
entities: EntityManager::new(),
transforms: TransformStorage::new(),
meshes: MeshStorage::new(),
physics: PhysicsStorage::new(),
movements: MovementStorage::new(),
jumps: JumpStorage::new(),
inputs: InputStorage::new(),
player_tags: PlayerTagStorage::new(),
state_machines: StateMachineStorage::new(),
cameras: CameraStorage::new(),
camera_follows: CameraFollowStorage::new(),
}
}
pub fn spawn(&mut self) -> EntityHandle
{
self.entities.spawn()
}
pub fn despawn(&mut self, entity: EntityHandle)
{
self.transforms.remove(entity);
self.meshes.remove(entity);
self.physics.remove(entity);
self.movements.remove(entity);
self.jumps.remove(entity);
self.inputs.remove(entity);
self.player_tags.remove(entity);
self.state_machines.remove(entity);
self.cameras.remove(entity);
self.camera_follows.remove(entity);
self.entities.despawn(entity);
}
}