~kmdodrill/my-brother-lives-in-a-canyon

ref: 685f3dbc3868b2b049b54c68ee5e9f57a339bd50 my-brother-lives-in-a-canyon/src/Player.gd -rw-r--r-- 4.1 KiB
685f3dbc — Kenneth Dodrill Add footstep sounds when player walks 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
extends KinematicBody

class_name Player

export(int) var acceleration = 8
export(int) var deacceleration = 10
export(float) var gravity = 30.0
export(int) var walk_speed = 2
export(int) var sprint_speed = 6

onready var settings = get_tree().root.get_node("Settings")
onready var head = $Head
onready var camera = $Head/Camera
onready var mouth = $Head/Mouth
onready var radio: Radio = $Head/Radio
onready var head_lamp = $Head/HeadLamp
onready var feet: AudioStreamPlayer = $Feet
    
var mouse_sensitivity
var joypad_x_sensitivity
var joypad_y_sensitivity
var joypad_deadzone

const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
var velocity := Vector3()
var direction := Vector3()
var move_axis := Vector2()
var mouse_axis := Vector2()
var moving := false
var sprinting := false
var seconds = 0

func _process(delta) -> void:
    seconds += delta

    move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
    move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    
    walk(delta)
    
    if moving && seconds >= 1.0:
        feet.play_footsteps()
        seconds = 0
    else:
        feet.stop_footsteps()
    
    if Input.get_connected_joypads().size() > 0:
        handle_controller_look_input()

func _input(event) -> void:
    # Handle mouse look input.
    if event is InputEventMouseMotion:
        if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
            return
        if event.relative.length() < 0:
            return
        camera_rotation(event.relative, mouse_sensitivity, mouse_sensitivity)
        
func handle_controller_look_input() -> void:        
    # Get the x and y values for the right joystick.
    var x_axis = Input.get_joy_axis(0, JOY_AXIS_2)
    var y_axis = Input.get_joy_axis(0, JOY_AXIS_3)
    
    var joypad_axis = Vector2(x_axis, y_axis)
    if joypad_axis.length() < joypad_deadzone:
        joypad_axis = Vector2(0, 0)
    camera_rotation(joypad_axis, joypad_x_sensitivity, joypad_y_sensitivity)
        
func walk(delta: float) -> void:
    direction = Vector3()
    var aim: Basis = get_global_transform().basis
    if move_axis.x >= 0.5:
        direction -= aim.z
    if move_axis.x <= -0.5:
        direction += aim.z
    if move_axis.y <= -0.5:
        direction -= aim.x
    if move_axis.y >= 0.5:
        direction += aim.x
    direction.y = 0
    moving = direction != Vector3.ZERO
    direction = direction.normalized()
    
    velocity.y -= gravity * delta
    
    # Sprint
    var _speed: int
    if (Input.is_action_pressed("move_sprint") && move_axis.x >= 0.5):
        _speed = sprint_speed
        sprinting = true
    else:
        _speed = walk_speed
        sprinting = false
    
    # Acceleration and Deacceleration
    # where would the player go
    var _temp_vel: Vector3 = velocity
    _temp_vel.y = 0
    var _target: Vector3 = direction * _speed
    var _temp_accel: float
    if direction.dot(_temp_vel) > 0:
        _temp_accel = acceleration
    else:
        _temp_accel = deacceleration
    # interpolation
    _temp_vel = _temp_vel.linear_interpolate(_target, _temp_accel * delta)
    velocity.x = _temp_vel.x
    velocity.z = _temp_vel.z
    # clamping (to stop on slopes)
    if direction.dot(velocity) == 0:
        var _vel_clamp := 0.25
        if abs(velocity.x) < _vel_clamp:
            velocity.x = 0
        if abs(velocity.z) < _vel_clamp:
            velocity.z = 0
    
    # Move
    var movement_vector = move_and_slide_with_snap(velocity, Vector3.DOWN, Vector3.UP, true, 4, FLOOR_MAX_ANGLE)
    if is_on_wall():
        velocity = movement_vector
    else:
        velocity.y = movement_vector.y

func camera_rotation(axis_vector: Vector2, x_sensitivity: float, y_sensitivity: float) -> void:
    var horizontal: float = -axis_vector.x * (x_sensitivity / 100)
    var vertical: float = -axis_vector.y * (y_sensitivity / 100)
    
    rotate_y(deg2rad(horizontal))
    head.rotate_x(deg2rad(vertical))
    head.rotation_degrees.x = clamp(head.rotation_degrees.x, -90, 90)

func get_mouth():
    return mouth
    
func set_mouth(new_mouth):
    mouth = new_mouth