]> Untitled Git - frog-ninja.git/blob - player/player.gd
d08f491a1fbbe25220a7980c9fd3a899bfce495b
[frog-ninja.git] / player / player.gd
1 extends CharacterBody3D
2 class_name Player
3
4 ##########
5 # settings
6 ##########
7
8 @export_group("Movement")
9 @export var walk_speed := 8.0
10 @export var dash_length := 10.0
11
12 @export var air_speed := 3.0
13 @export var acceleration := 50.0
14 @export var rotation_speed := 10.0
15 @export var idle_timeout := 5.0
16 @export var hard_landing_limit := 10.0
17
18 @export_group("Physics")
19 @export var push_force := 5.0
20
21 @export_group("Camera")
22 @export_range(1.0, 10.0) var camera_distance := 2.0
23 @export_range(0.0, 1.0) var mouse_sensitivity := 0.15
24 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
25 @export_range(0.0, 1.0) var mouse_sensitivity_y := 0.5
26 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
27 @export_range(0.0, 10.0) var joystick_sensitivity_y := 2.0
28
29 ######
30 # init
31 ######
32
33 @onready var _camera_pivot: Node3D = %camera_pivot
34 @onready var _camera: Camera3D = %camera
35 @onready var _camera_spring: SpringArm3D = %spring
36 @onready var _skin: AnimatedSkin = %skin
37
38 var _last_movement_direction := rotation
39 var _floor_normal := Vector3.ONE
40 var _ground_slope_input := 0.0
41 var _camera_input_direction := Vector2.ZERO
42
43 enum {CAMERA_MOUSE_INPUT, CAMERA_JOYSTICK_INPUT}
44 var _camera_input_method := CAMERA_MOUSE_INPUT
45
46 var _idle_time: float = 0.0
47 var _player_speed: float = walk_speed
48
49
50 func _ready() -> void:
51         _camera_spring.spring_length = camera_distance
52         
53         
54
55 #######
56 # input
57 #######
58
59 func _unhandled_input(event: InputEvent) -> void:
60         # If user clicks on the window, capture the mouse and direct the camera with it
61         if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
62                 return
63
64                         #_camera_input_direction *= mouse_sensitivity
65         if event is InputEventMouseMotion:
66                 _camera_input_method = CAMERA_MOUSE_INPUT
67                 _camera_input_direction = event.screen_relative * mouse_sensitivity
68         elif event is InputEventJoypadMotion:
69                 # TODO: add these settings!
70                 _camera_input_method = CAMERA_JOYSTICK_INPUT
71                 _camera_input_direction = Input.get_vector("camera-left", "camera-right", "camera-up", "camera-down")
72                 _camera_input_direction *= Vector2(joystick_sensitivity_x, -joystick_sensitivity_y)
73
74
75 func _input(event: InputEvent):
76         if event.is_action_pressed("player-dash"):
77                 _skin.transition_dash()
78                 _player_dash()
79         elif event.is_action_pressed("player-slash"):
80                 _skin.sword_visible()
81                 _skin.transition_slash()
82         elif event.is_action_pressed("player-shoot"):
83                 _skin.gun_visible()
84                 _skin.transition_gunfire()
85                 
86
87 ##########
88 # movement
89 ##########
90
91 func _physics_process(delta: float) -> void:
92         _process_camera(delta)
93         _process_player(delta)
94
95
96 # Get the XZ input direction based on player's input relative to the camera
97 func _get_player_move_direction() -> Vector3:
98         var input_dir := Input.get_vector("player-left", "player-right", "player-forward", "player-backward")
99         var forward := _camera.global_basis.z
100         var right := _camera.global_basis.x
101         var move_direction := (forward * input_dir.y + right * input_dir.x).normalized()
102         move_direction.y = 0
103         return move_direction
104
105
106 func _process_camera(delta: float) -> void:
107         # vertical camera rotation
108         _camera_pivot.rotation.x += _camera_input_direction.y * mouse_sensitivity_y * delta
109         _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -PI / 6, PI / 3)
110
111         # horizontal camera rotation
112         _camera_pivot.rotation.y -= _camera_input_direction.x * mouse_sensitivity_x * delta
113
114         # reset mouse movement vector if mouse input
115         if _camera_input_method == CAMERA_MOUSE_INPUT:
116                 _camera_input_direction = Vector2.ZERO
117                 
118         # change spring length depending on player speed
119         _camera_spring.spring_length = lerp(
120                 _camera_spring.spring_length, camera_distance + velocity.length() / 4, delta 
121         ) 
122
123
124 func _process_player_on_floor(delta: float):
125         var move_direction := _get_player_move_direction()
126         
127         # if we're not stuck, then it's okay to set the velocity
128         _floor_normal = get_floor_normal()
129         _ground_slope_input = (PI / 2) - velocity.angle_to(_floor_normal)
130         velocity = velocity.move_toward(
131                 move_direction * (_player_speed + _ground_slope_input * _player_speed),
132                 acceleration * delta
133                 )
134         var movement_speed := Vector3(velocity.x, 0, velocity.z).length()
135         
136         # also, if we're moving, we're not idle
137         if move_direction.length() < 0.2:
138                 if velocity == Vector3.ZERO:
139                         _idle_time += delta
140         else:
141                 _last_movement_direction = move_direction
142                 _idle_time = 0.0
143
144         # if camera is unlocked, rotate whole skin to face movement direction
145         var skin_target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
146         _skin.global_rotation.y = lerp_angle(
147                 _skin.global_rotation.y,
148                 skin_target_angle,
149                 rotation_speed * delta
150                 )
151                 
152         # lean into player momentum just a little bit
153         _skin.rotation.z = lerp_angle(
154                 _skin.rotation.z,
155                 clamp(_last_movement_direction.signed_angle_to(velocity, Vector3.UP) * movement_speed * 0.08, -PI/4, PI/ 4),
156                 rotation_speed * delta * 0.25
157                 )
158         
159         # let skin know how fast player is moving along the ground
160         _skin.set_walking_speed(movement_speed)
161
162
163 func _process_player(delta: float) -> void:
164         if is_on_floor():
165                 _process_player_on_floor(delta)
166                 
167                 if _idle_time > idle_timeout:
168                         _skin.transition_move()
169                 else:
170                         _skin.transition_move()
171         else:
172                 _skin.transition_falling()
173                 velocity += get_gravity() * air_speed * delta
174
175         move_and_slide()
176
177 #########
178 # actions
179 #########
180
181 func _player_dash():
182         var move_direction := _get_player_move_direction()
183         if move_direction != Vector3.ZERO:
184                 var dash_local_pos = move_direction * dash_length
185                 # TODO: check if valid position, crop the vector to last valid position
186                 global_position += dash_local_pos