]> Untitled Git - knight.git/blob - player/player.gd
f6b6c564fd09a213cfa0c63ff575aa412f7ab6ab
[knight.git] / player / player.gd
1 extends CharacterBody3D
2
3
4 @onready var _skin: KnightSkin = $skin
5
6
7 const SPEED = 5.0
8 const JUMP_VELOCITY = 4.5 * 3
9 const FALL_SPEED = 2.0
10
11
12 var _last_movement_direction := Vector3.FORWARD
13
14
15 func _input(event: InputEvent) -> void:
16         if event.is_action_released("ui_accept"):
17                 velocity.y = 0
18         elif event.is_action_pressed("player-attack"):
19                 _skin.attack()
20
21
22 func _process_player_input(_delta: float) -> void:
23         # Get the input direction and handle the movement/deceleration.
24         # As good practice, you should replace UI actions with custom gameplay actions.
25         var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
26         var direction := -(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
27         if direction:
28                 velocity.x = direction.x * SPEED
29                 velocity.z = direction.z * SPEED
30                 _last_movement_direction = direction
31         else:
32                 velocity.x = 0
33                 velocity.z = 0
34
35         # Handle jump.
36         if Input.is_action_just_pressed("ui_accept") and is_on_floor():
37                 velocity.y = JUMP_VELOCITY
38
39
40 func _process_player_skin(_delta: float) -> void:
41         _skin.global_rotation.y = Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
42         
43         if is_on_floor():
44                 _skin.move(velocity.length())
45         else:
46                 _skin.fall()
47
48
49 func _physics_process(delta: float) -> void:
50         # Add the gravity.
51         if not is_on_floor():
52                 velocity += get_gravity() * FALL_SPEED * delta
53
54         _process_player_input(delta)
55         _process_player_skin(delta)
56         
57         move_and_slide()