]> Untitled Git - knight.git/blob - player/player.gd
Added head tilting, spike
[knight.git] / player / player.gd
1 class_name Player extends CharacterBody3D
2
3
4 # class variables
5 @export_category("Movement")
6 @export var _walk_speed := 4.0
7 @export var _jump_speed := 9.0
8 @export var _fall_speed := 2.0
9
10 @export_category("Camera")
11 @export var _camera_distance := 5.0
12 @export var _dof_blur_far_distance := 5.0
13 @export var _camera_pivot_rotate_y := 0.0
14
15
16 var _last_movement_direction := Vector3.RIGHT
17 @onready var _skin: KnightSkin = $skin
18 @onready var _camera_pivot: Node3D = $"camera-pivot"
19 @onready var _spring_arm: SpringArm3D = $"camera-pivot/SpringArm3D"
20 @onready var _camera: Camera3D = $"camera-pivot/SpringArm3D/Camera3D"
21 @onready var _camera_attr: CameraAttributes = _camera.attributes
22
23
24 # inherited functions
25 func _ready() -> void:
26         _spring_arm.spring_length = _camera_distance
27         _camera_attr.dof_blur_far_distance = _dof_blur_far_distance
28         _camera_pivot.rotate_y(_camera_pivot_rotate_y)
29
30
31 func _input(event: InputEvent) -> void:
32         if event.is_action_released("player-jump"):
33                 if velocity.y > 0:
34                         velocity.y = 0
35         elif event.is_action_pressed("player-attack"):
36                 if Input.is_action_pressed("player-up"):
37                         _skin.attack_up()
38                         _last_movement_direction = Vector3.FORWARD
39                 elif Input.is_action_pressed("player-down") and not is_on_floor():
40                         _skin.attack_down()
41                         _last_movement_direction = Vector3.FORWARD
42                 else:
43                         _skin.attack_neutral()
44         elif event.is_action_pressed("player-up"):
45                 _last_movement_direction = Vector3.FORWARD
46
47
48 func _physics_process(delta: float) -> void:
49         # Add the gravity.
50         if not is_on_floor():
51                 velocity += get_gravity() * _fall_speed * delta
52
53         _process_player_input(delta)
54         _process_player_skin(delta)
55         
56         move_and_slide()
57
58
59 # my functions
60 func _process_player_input(_delta: float) -> void:
61         # Handle jump.
62         if Input.is_action_just_pressed("player-jump") and is_on_floor():
63                 velocity.y = _jump_speed
64
65         # Get the input direction and handle the movement/deceleration.
66         # As good practice, you should replace UI actions with custom gameplay actions.
67         var input_dir := Input.get_vector("player-left", "player-right", "player-up", "player-down")
68         var direction := -(transform.basis * Vector3(input_dir.x, input_dir.y, 0)).normalized()
69         
70         _skin.head_tilt(direction.y)
71         
72         if direction.x:
73                 velocity.x = direction.x * _walk_speed
74                 _last_movement_direction = direction
75         else:
76                 velocity.x = 0
77
78
79 func _process_player_skin(_delta: float) -> void:
80         _skin.global_rotation.y = Vector3.BACK.signed_angle_to(_last_movement_direction - Vector3.BACK, Vector3.UP)
81         if is_on_floor():
82                 _skin.move(velocity.length())
83         else:
84                 _skin.fall()