diff options
| -rw-r--r-- | src/content/entities/entity_wizard.rs | 28 | ||||
| -rw-r--r-- | src/sim/entity/mod.rs | 12 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/content/entities/entity_wizard.rs b/src/content/entities/entity_wizard.rs index 3787e49..a74d9ff 100644 --- a/src/content/entities/entity_wizard.rs +++ b/src/content/entities/entity_wizard.rs @@ -14,6 +14,12 @@ use crate::{ }, }; +#[derive(PartialEq, Eq)] +enum FacingDirection { + Left, + Right, +} + struct WizardEntityBehaviour { movement_input_x: f32, jump: f32, @@ -21,6 +27,9 @@ struct WizardEntityBehaviour { grounded: bool, acc_vel: Vec2, kinematic_controller: KinematicCharacterController, + + facing_direction: FacingDirection, + facing_strength: f32, } const JUMP_VELOCITY: f32 = 100.0; @@ -28,6 +37,15 @@ const ACCEL: f32 = 1000.0; const AIR_ACCEL: f32 = 350.0; const MAX_SPEED: f32 = 75.0; +impl WizardEntityBehaviour { + fn set_facing_dir(&mut self, update_ctx: &mut EntityUpdateCtx, dir: FacingDirection) { + if self.facing_direction != dir { + update_ctx.entity_data.cells.as_mut().unwrap().flip_x(); + self.facing_direction = dir; + } + } +} + impl EntityBehaviour for WizardEntityBehaviour { fn update(&mut self, _: &mut EntityUpdateCtx, ctx: &mut SimCtx, _: f32) { if ctx.input_manager.held(Input::Left) { @@ -167,6 +185,14 @@ impl EntityBehaviour for WizardEntityBehaviour { rb.set_linvel(movement.translation / delta_time, true); + self.facing_strength = (self.facing_strength + movement.translation.x).clamp(-2.0, 2.0); + + if self.facing_strength > 1.0 { + self.set_facing_dir(update_ctx, FacingDirection::Right); + } else if self.facing_strength < -1.0 { + self.set_facing_dir(update_ctx, FacingDirection::Left); + } + self.grounded = movement.grounded; } } @@ -193,6 +219,8 @@ pub fn entity_wizard_def(position: Vec2) -> EntityDef { grounded: false, acc_vel: Vec2::ZERO, kinematic_controller, + facing_direction: FacingDirection::Right, + facing_strength: 0.0, })), ) } diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs index 59479a2..61f9d84 100644 --- a/src/sim/entity/mod.rs +++ b/src/sim/entity/mod.rs @@ -34,6 +34,18 @@ impl EntityCells { .fold(0.0, |acc, cur| acc + cur.material.def().density as f32) * MASS_SCALING } + + pub fn flip_x(&mut self) { + let mut new_cells = Vec::with_capacity(self.cells.len()); + for y in 0..self.size.y { + for rx in 0..self.size.x { + let x = self.size.x - rx - 1; + new_cells.push(self.get_cell_at_local_position(IVec2::new(x, y))); + } + } + + self.cells = new_cells; + } } impl Marchable for EntityCells { |
