Skip to content

Commit ce2496b

Browse files
committed
More crouching cleanup
1 parent f4a8cb4 commit ce2496b

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

entities/player.zig

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const math = delve.math;
2323
const interpolation = delve.utils.interpolation;
2424

2525
pub var jump_acceleration: f32 = 20.0;
26+
pub var eye_height_mod: f32 = 0.35;
2627

2728
var rand = std.rand.DefaultPrng.init(0);
2829

@@ -114,26 +115,35 @@ pub const PlayerController = struct {
114115
const starting_pos = self.owner.getPosition();
115116
const was_crouched = self.is_crouched;
116117
const crouching_size = c.size.mul(math.Vec3.new(1.0, self.crouch_size_mod, 1.0));
117-
const crouching_size_diff = self.standing_size.sub(crouching_size);
118+
const crouching_size_diff = self.standing_size.sub(crouching_size).scale(0.5);
118119

119120
const is_crouched_pressed = delve.platform.input.isKeyPressed(.C);
120121
if (!self.is_crouched and is_crouched_pressed) {
121122
self.is_crouched = true;
122123
}
123124

125+
var char_comp = self.owner.getComponent(character.CharacterMovementComponent);
126+
if (char_comp == null)
127+
return;
128+
129+
const eye_height_diff = (self.standing_size.y * eye_height_mod) - (crouching_size.y * eye_height_mod);
130+
const eye_adj: math.Vec3 = if (char_comp.?.state.on_ground) math.Vec3.zero else math.Vec3.new(0, eye_height_diff, 0).add(crouching_size_diff);
131+
124132
if (self.is_crouched and !was_crouched) {
125133
// crouch down!
126-
self.owner.setPosition(starting_pos.add(crouching_size_diff.scale(-0.5)));
127134

128-
if (self.owner.getComponent(character.CharacterMovementComponent)) |char| {
129-
char.state.step_lerp_startheight = starting_pos.y;
130-
char.state.step_lerp_timer = 0.0;
135+
const new_pos = starting_pos.add(crouching_size_diff.scale(-1));
136+
self.owner.setPosition(new_pos.add(eye_adj));
137+
138+
if (char_comp.?.state.on_ground) {
139+
char_comp.?.state.step_lerp_startheight = starting_pos.y;
140+
char_comp.?.state.step_lerp_timer = 0.0;
131141
}
132142
}
133143

134144
if (!is_crouched_pressed and self.is_crouched) {
135145
// check if we can stand up!
136-
const check_position = starting_pos.add(crouching_size_diff.scale(0.5));
146+
const check_position = starting_pos.add(crouching_size_diff).add(eye_adj.scale(-1));
137147
const move = collision.MoveInfo{
138148
.pos = check_position,
139149
.vel = math.Vec3.zero,
@@ -151,9 +161,9 @@ pub const PlayerController = struct {
151161
self.is_crouched = false;
152162
self.owner.setPosition(check_position);
153163

154-
if (self.owner.getComponent(character.CharacterMovementComponent)) |char| {
155-
char.state.step_lerp_startheight = starting_pos.y;
156-
char.state.step_lerp_timer = 0.0;
164+
if (char_comp.?.state.on_ground) {
165+
char_comp.?.state.step_lerp_startheight = starting_pos.y;
166+
char_comp.?.state.step_lerp_timer = 0.0;
157167
}
158168
}
159169
}
@@ -216,7 +226,7 @@ pub const PlayerController = struct {
216226
const cam_diff = self.camera.position.y - movement_component.state.pos.y;
217227

218228
// add eye height
219-
self.camera.position.y += movement_component.state.size.y * 0.35;
229+
self.camera.position.y += movement_component.state.size.y * eye_height_mod;
220230

221231
calcScreenShake(self, delta);
222232
calcWeaponLag(self, cam_diff);

0 commit comments

Comments
 (0)