]> Untitled Git - frog-ninja.git/blob - player/moves/walk.gd
Separated the Visual from the Model
[frog-ninja.git] / player / moves / walk.gd
1 extends Move
2 class_name Walk
3
4
5 const skin_lean_limit := PI/8
6
7 @export var movement_speed := 10.0
8
9
10 func update(input: InputPacket, delta: float):
11         player.velocity = get_new_velocity_from_input(input, delta, movement_speed)
12         player.move_and_slide()
13         update_skin(delta)
14
15
16 func update_skin(delta: float):
17         # update skin rotation
18         var skin_target_angle := Vector3.BACK.signed_angle_to(
19                 player.last_movement_direction, 
20                 Vector3.UP
21                 )
22         player.skin.global_rotation.y = lerp_angle(
23                 player.skin.global_rotation.y,
24                 skin_target_angle,
25                 player.rotation_speed * delta
26         )
27         
28         # lean into player momentum just a little bit
29         player.skin.rotation.z = lerp_angle(
30                 player.skin.rotation.z,
31                 clamp(
32                         player.last_movement_direction.signed_angle_to(
33                                 player.velocity, Vector3.UP
34                                 ) * player.velocity.length() * 0.08, 
35                         -skin_lean_limit, skin_lean_limit
36                         ),
37                 player.rotation_speed * delta * 0.25
38                 )
39
40
41 func get_new_velocity_from_input(input: InputPacket, delta: float, speed: float) -> Vector3:
42         var movement_direction := input.camera_movement_direction
43         movement_direction.y = 0
44
45         # save off last movement direction 
46         if movement_direction.length() > 0.2:
47                 player.last_movement_direction = movement_direction
48         
49         # if we're not stuck, then it's okay to set the velocity
50         player.floor_normal = player.get_floor_normal()
51         player.ground_slope_input = (PI / 2) - player.velocity.angle_to(player.floor_normal)
52         var new_velocity = player.velocity.move_toward(
53                 movement_direction * (speed + player.ground_slope_input * speed),
54                 player.acceleration * delta
55                 )
56         
57         return new_velocity