diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-09-04 21:49:03 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-09-04 21:49:03 -0700 |
| commit | 67aee9ec7f3282cab20f6439f755588135a32af5 (patch) | |
| tree | a02b958bcdaf131040f58316cf101e68dd4989fb /src/content | |
| parent | 8a49bc98a8be81d38cfd11b88e269797048256e4 (diff) | |
character flipping
Diffstat (limited to 'src/content')
| -rw-r--r-- | src/content/entities/entity_wizard.rs | 28 |
1 files changed, 28 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, })), ) } |
