113 lines
3.3 KiB
GDScript
113 lines
3.3 KiB
GDScript
extends CharacterBody3D
|
|
|
|
# Local coords to axis
|
|
# Yaw = Y
|
|
# Roll = Z
|
|
# Pitch = X
|
|
|
|
@export_category("Parameters")
|
|
@export var flight_modes: Array = ["normal", "acro"]
|
|
@export var armed: bool = false
|
|
@export var throttle_speed: float = 0.0
|
|
@export var rotation_speed: float = 1.0
|
|
@export var acceleration: float = 10.0
|
|
@export var max_speed: float = 10.0
|
|
@export var max_rotation_speed: float = 10.0
|
|
@export var camera_tilt_angle: float = 0.0
|
|
@export var gravity: float = 9.8
|
|
@export_category("Node References")
|
|
@export var camera: Camera3D
|
|
@export var anim_player: AnimationPlayer
|
|
|
|
var animation_initalized: bool = false
|
|
var using_joy_controller: bool = true
|
|
|
|
|
|
func _ready():
|
|
pass
|
|
|
|
|
|
func handle_throttle(throttle_input: float, delta):
|
|
if using_joy_controller:
|
|
if throttle_input > 0:
|
|
throttle_speed = throttle_input * 100
|
|
var desired_vel = velocity.y + throttle_speed * 0.5
|
|
velocity.y = lerp(velocity.y, desired_vel, delta)
|
|
else:
|
|
pass
|
|
|
|
# Speed scale adjusted for animation
|
|
anim_player.speed_scale = throttle_input * 4
|
|
anim_player.speed_scale = clampf(anim_player.speed_scale, 1.0, 4.0)
|
|
|
|
velocity.x = clampf(velocity.x, -max_speed, max_speed)
|
|
velocity.y = clampf(velocity.y, -100.0, max_speed)
|
|
velocity.z = clampf(velocity.z, -max_speed, max_speed)
|
|
|
|
#print("anim speed: ", anim_player.speed_scale) # DEBUG
|
|
#print("throttle speed: ", throttle_speed) # DEBUG
|
|
|
|
|
|
func handle_yaw(yaw_input: float, delta):
|
|
#rotate_y(rotation_speed * yaw_input * delta)
|
|
#global_rotate(Vector3.UP, yaw_input * rotation_speed * delta)
|
|
transform.basis = transform.basis.rotated(Vector3.UP, yaw_input * delta)
|
|
|
|
|
|
func handle_roll(roll_input: float, delta):
|
|
if !is_on_floor():
|
|
transform.basis = transform.basis.rotated(Vector3.BACK, roll_input * delta)
|
|
|
|
|
|
|
|
func handle_pitch(pitch_input: float, delta):
|
|
if !is_on_floor():
|
|
transform.basis = transform.basis.rotated(Vector3.RIGHT, pitch_input * delta)
|
|
|
|
|
|
func handle_arming():
|
|
if !armed:
|
|
if !animation_initalized:
|
|
anim_player.play("spin_props")
|
|
animation_initalized = true
|
|
elif animation_initalized:
|
|
anim_player.play()
|
|
armed = !armed
|
|
print("Drone armed")
|
|
elif armed:
|
|
anim_player.pause()
|
|
armed = !armed
|
|
print("Drone disarmed")
|
|
|
|
|
|
func _physics_process(delta):
|
|
# Gravity
|
|
if !is_on_floor():
|
|
velocity.y -= gravity * delta
|
|
else:
|
|
global_rotation.x = lerpf(global_rotation.x, 0.0, delta * 20)
|
|
global_rotation.z = lerpf(global_rotation.z, 0.0, delta * 20)
|
|
|
|
if Input.is_action_just_pressed("arm"):
|
|
handle_arming()
|
|
|
|
if armed:
|
|
if Input.is_action_pressed("throttle_down") or Input.is_action_pressed("throttle_up"):
|
|
var throttle_input = Input.get_axis("throttle_down", "throttle_up")
|
|
handle_throttle(throttle_input, delta)
|
|
|
|
if Input.is_action_pressed("yaw_left") or Input.is_action_pressed("yaw_right"):
|
|
var yaw_input = Input.get_axis("yaw_right", "yaw_left")
|
|
handle_yaw(yaw_input, delta)
|
|
|
|
if Input.is_action_pressed("roll_left") or Input.is_action_pressed("roll_right"):
|
|
var roll_input = Input.get_axis("roll_left", "roll_right")
|
|
handle_roll(roll_input, delta)
|
|
|
|
if Input.is_action_pressed("pitch_backward") or Input.is_action_pressed("pitch_forward"):
|
|
var pitch_input = Input.get_axis("pitch_backward", "pitch_forward")
|
|
handle_pitch(pitch_input, delta)
|
|
|
|
print("velocity: ", velocity)
|
|
move_and_slide()
|