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