flatten component configs

This commit is contained in:
Jonas H
2026-03-05 15:03:55 +01:00
parent d888dc077e
commit 28f8c65571
4 changed files with 38 additions and 87 deletions

View File

@@ -6,9 +6,10 @@ use rapier3d::control::{CharacterAutostep, KinematicCharacterController};
use rapier3d::prelude::{ColliderBuilder, RigidBodyBuilder}; use rapier3d::prelude::{ColliderBuilder, RigidBodyBuilder};
use crate::bundles::Bundle; use crate::bundles::Bundle;
use crate::components::jump::JumpComponent;
use crate::components::lights::spot::SpotlightComponent; use crate::components::lights::spot::SpotlightComponent;
use crate::components::{InputComponent, MeshComponent, MovementComponent, PhysicsComponent}; use crate::components::{
InputComponent, JumpComponent, MeshComponent, MovementComponent, PhysicsComponent,
};
use crate::entity::EntityHandle; use crate::entity::EntityHandle;
use crate::loaders::mesh::Mesh; use crate::loaders::mesh::Mesh;
use crate::physics::PhysicsManager; use crate::physics::PhysicsManager;
@@ -70,7 +71,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerFallingState, PlayerIdleState>(move |world| { state_machine.add_transition::<PlayerFallingState, PlayerIdleState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let has_input = world let has_input = world
.inputs .inputs
@@ -82,7 +83,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerFallingState, PlayerWalkingState>(move |world| { state_machine.add_transition::<PlayerFallingState, PlayerWalkingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let has_input = world let has_input = world
.inputs .inputs
@@ -94,7 +95,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerIdleState, PlayerWalkingState>(move |world| { state_machine.add_transition::<PlayerIdleState, PlayerWalkingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let has_input = world let has_input = world
.inputs .inputs
@@ -106,7 +107,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerWalkingState, PlayerIdleState>(move |world| { state_machine.add_transition::<PlayerWalkingState, PlayerIdleState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let has_input = world let has_input = world
.inputs .inputs
@@ -118,7 +119,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerIdleState, PlayerFallingState>(move |world| { state_machine.add_transition::<PlayerIdleState, PlayerFallingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
!is_grounded !is_grounded
}); });
@@ -126,7 +127,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerWalkingState, PlayerFallingState>(move |world| { state_machine.add_transition::<PlayerWalkingState, PlayerFallingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
!is_grounded !is_grounded
}); });
@@ -134,7 +135,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerIdleState, PlayerJumpingState>(move |world| { state_machine.add_transition::<PlayerIdleState, PlayerJumpingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let jump_pressed = world let jump_pressed = world
.inputs .inputs
@@ -146,7 +147,7 @@ impl Bundle for PlayerBundle
state_machine.add_transition::<PlayerWalkingState, PlayerJumpingState>(move |world| { state_machine.add_transition::<PlayerWalkingState, PlayerJumpingState>(move |world| {
let is_grounded = world let is_grounded = world
.movements .movements
.with(entity_id, |m| m.movement_config.movement_context.is_floored) .with(entity_id, |m| m.movement_context.is_floored)
.unwrap_or(false); .unwrap_or(false);
let jump_pressed = world let jump_pressed = world
.inputs .inputs
@@ -159,14 +160,14 @@ impl Bundle for PlayerBundle
world world
.jumps .jumps
.with(entity_id, |jump| { .with(entity_id, |jump| {
jump.jump_config.jump_context.duration >= jump.jump_config.jump_duration jump.jump_context.duration >= jump.jump_duration
}) })
.unwrap_or(true) .unwrap_or(true)
}); });
world.transforms.insert(entity, spawn_transform); world.transforms.insert(entity, spawn_transform);
world.movements.insert(entity, MovementComponent::new()); world.movements.insert(entity, MovementComponent::new());
world.jumps.insert(entity, JumpComponent::new()); world.jumps.insert(entity, JumpComponent::default());
world.inputs.insert(entity, InputComponent::default()); world.inputs.insert(entity, InputComponent::default());
world.physics.insert( world.physics.insert(
entity, entity,
@@ -189,6 +190,7 @@ impl Bundle for PlayerBundle
); );
world.player_tags.insert(entity, ()); world.player_tags.insert(entity, ());
world.state_machines.insert(entity, state_machine); world.state_machines.insert(entity, state_machine);
world.names.insert(entity, "Player".to_string());
let outer_angle = PI / 2.0 * 0.9; let outer_angle = PI / 2.0 * 0.9;
world.spotlights.insert( world.spotlights.insert(

View File

@@ -3,22 +3,6 @@ use kurbo::CubicBez;
#[derive(Clone)] #[derive(Clone)]
pub struct JumpComponent pub struct JumpComponent
{
pub jump_config: JumpConfig,
}
impl JumpComponent
{
pub fn new() -> Self
{
Self {
jump_config: JumpConfig::default(),
}
}
}
#[derive(Clone, Copy)]
pub struct JumpConfig
{ {
pub jump_height: f32, pub jump_height: f32,
pub jump_duration: f32, pub jump_duration: f32,
@@ -30,7 +14,7 @@ pub struct JumpConfig
pub jump_context: JumpContext, pub jump_context: JumpContext,
} }
impl Default for JumpConfig impl Default for JumpComponent
{ {
fn default() -> Self fn default() -> Self
{ {
@@ -56,22 +40,3 @@ pub struct JumpContext
pub origin_height: f32, pub origin_height: f32,
pub normal: Vec3, pub normal: Vec3,
} }
impl JumpContext
{
fn start(time: f32, current_height: f32, surface_normal: Vec3) -> Self
{
Self {
in_progress: false,
duration: 0.0,
execution_time: time,
origin_height: current_height,
normal: surface_normal,
}
}
pub fn stop(&mut self)
{
self.in_progress = false;
}
}

View File

@@ -3,22 +3,6 @@ use kurbo::CubicBez;
#[derive(Clone)] #[derive(Clone)]
pub struct MovementComponent pub struct MovementComponent
{
pub movement_config: MovementConfig,
}
impl MovementComponent
{
pub fn new() -> Self
{
Self {
movement_config: MovementConfig::new(),
}
}
}
#[derive(Clone)]
pub struct MovementConfig
{ {
pub walking_acceleration: f32, pub walking_acceleration: f32,
pub walking_acceleration_duration: f32, pub walking_acceleration_duration: f32,
@@ -29,7 +13,7 @@ pub struct MovementConfig
pub movement_context: MovementContext, pub movement_context: MovementContext,
} }
impl MovementConfig impl MovementComponent
{ {
pub fn new() -> Self pub fn new() -> Self
{ {

View File

@@ -90,7 +90,7 @@ impl State for PlayerFallingState
}; };
world.movements.with_mut(self.entity, |movement| { world.movements.with_mut(self.entity, |movement| {
movement.movement_config.movement_context.is_floored = is_grounded; movement.movement_context.is_floored = is_grounded;
}); });
} }
} }
@@ -116,7 +116,7 @@ impl State for PlayerIdleState
let current_velocity = *rigidbody.linvel(); let current_velocity = *rigidbody.linvel();
let idle_damping = world let idle_damping = world
.movements .movements
.with(self.entity, |m| m.movement_config.idle_damping) .with(self.entity, |m| m.idle_damping)
.unwrap_or(0.1); .unwrap_or(0.1);
let horizontal_velocity = Vec3::new(current_velocity.x, 0.0, current_velocity.z); let horizontal_velocity = Vec3::new(current_velocity.x, 0.0, current_velocity.z);
@@ -171,7 +171,7 @@ impl State for PlayerIdleState
}); });
world.movements.with_mut(self.entity, |movement| { world.movements.with_mut(self.entity, |movement| {
movement.movement_config.movement_context.is_floored = true; movement.movement_context.is_floored = true;
}); });
} }
} }
@@ -210,7 +210,7 @@ impl State for PlayerWalkingState
.inputs .inputs
.with(self.entity, |input| input.move_direction) .with(self.entity, |input| input.move_direction)
.unwrap_or(Vec3::ZERO); .unwrap_or(Vec3::ZERO);
(input, movement.movement_config.clone()) (input, movement.clone())
}) })
.unwrap(); .unwrap();
@@ -301,7 +301,7 @@ impl State for PlayerWalkingState
}); });
world.movements.with_mut(self.entity, |movement| { world.movements.with_mut(self.entity, |movement| {
movement.movement_config.movement_context.is_floored = terrain_height.is_some(); movement.movement_context.is_floored = terrain_height.is_some();
}); });
if movement_input.length_squared() > 0.1 if movement_input.length_squared() > 0.1
@@ -334,11 +334,11 @@ impl State for PlayerJumpingState
let current_position = world.transforms.get(self.entity).unwrap().position; let current_position = world.transforms.get(self.entity).unwrap().position;
world.jumps.with_mut(self.entity, |jump| { world.jumps.with_mut(self.entity, |jump| {
jump.jump_config.jump_context.in_progress = true; jump.jump_context.in_progress = true;
jump.jump_config.jump_context.execution_time = self.enter_time_stamp; jump.jump_context.execution_time = self.enter_time_stamp;
jump.jump_config.jump_context.origin_height = current_position.y; jump.jump_context.origin_height = current_position.y;
jump.jump_config.jump_context.duration = 0.0; jump.jump_context.duration = 0.0;
jump.jump_config.jump_context.normal = Vec3::Y; jump.jump_context.normal = Vec3::Y;
}); });
println!("entered jumping"); println!("entered jumping");
@@ -347,8 +347,8 @@ impl State for PlayerJumpingState
fn on_state_exit(&mut self, world: &mut World) fn on_state_exit(&mut self, world: &mut World)
{ {
world.jumps.with_mut(self.entity, |jump| { world.jumps.with_mut(self.entity, |jump| {
jump.jump_config.jump_context.in_progress = false; jump.jump_context.in_progress = false;
jump.jump_config.jump_context.duration = 0.0; jump.jump_context.duration = 0.0;
}); });
println!("exited jumping"); println!("exited jumping");
@@ -361,21 +361,21 @@ impl State for PlayerJumpingState
let current_time = Time::get_time_elapsed(); let current_time = Time::get_time_elapsed();
world.jumps.with_mut(self.entity, |jump| { world.jumps.with_mut(self.entity, |jump| {
jump.jump_config.jump_context.duration = jump.jump_context.duration =
current_time - jump.jump_config.jump_context.execution_time; current_time - jump.jump_context.execution_time;
}); });
let jump_config = world let jump = world
.jumps .jumps
.with_mut(self.entity, |jump| jump.jump_config.clone()) .with(self.entity, |jump| jump.clone())
.unwrap(); .unwrap();
let elapsed_time = jump_config.jump_context.duration; let elapsed_time = jump.jump_context.duration;
let normalized_time = (elapsed_time / jump_config.jump_duration).min(1.0); let normalized_time = (elapsed_time / jump.jump_duration).min(1.0);
let height_progress = jump_config.jump_curve.eval(normalized_time as f64).y as f32; let height_progress = jump.jump_curve.eval(normalized_time as f64).y as f32;
let origin_height = jump_config.jump_context.origin_height; let origin_height = jump.jump_context.origin_height;
let target_height = origin_height + height_progress * jump_config.jump_height; let target_height = origin_height + height_progress * jump.jump_height;
let current_translation = world let current_translation = world
.physics .physics
@@ -409,7 +409,7 @@ impl State for PlayerJumpingState
}); });
world.movements.with_mut(self.entity, |movement| { world.movements.with_mut(self.entity, |movement| {
movement.movement_config.movement_context.is_floored = false; movement.movement_context.is_floored = false;
}); });
} }
} }