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