2024-07-20 13:50:12 -06:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
2024-07-20 15:27:33 -06:00
|
|
|
const TARGET_FPS: float = 60.0
|
|
|
|
const BASE_SPEED: float = 5.0
|
2024-07-23 00:25:13 -06:00
|
|
|
const BOOST_FORCE: float = 20.0
|
|
|
|
const JUMP_FORCE: float = 8.0
|
2024-07-20 15:27:33 -06:00
|
|
|
const FRICTION: float = 0.3
|
|
|
|
const AIR_DRAG: float = 0.03
|
|
|
|
const INPUT_SENSITIVITY: float = 0.7
|
2024-07-22 15:23:32 -06:00
|
|
|
const TURN_SENSITIVITY: float = 0.04
|
2024-07-20 13:50:12 -06:00
|
|
|
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
|
|
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
|
2024-07-22 19:37:26 -06:00
|
|
|
@onready var camera_root: ThirdPersonCamera = $CameraRoot
|
2024-07-20 15:27:33 -06:00
|
|
|
@onready var mesh: Node3D = $Mesh
|
2024-07-22 15:23:32 -06:00
|
|
|
@onready var animation_tree: AnimationTree = $Mesh/Mech/AnimationTree
|
2024-07-23 00:25:13 -06:00
|
|
|
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
2024-07-20 15:27:33 -06:00
|
|
|
|
2024-07-22 19:37:26 -06:00
|
|
|
@onready var _crosshair: Crosshair = get_tree().get_first_node_in_group("CrosshairGroup")
|
2024-07-23 00:25:13 -06:00
|
|
|
@onready var _overlay: OverlayEffects = get_tree().get_first_node_in_group("OverlayEffectsGroup")
|
2024-07-23 14:23:45 -06:00
|
|
|
@onready var _heat_meter: ProgressBar = get_tree().get_first_node_in_group("HeatMeterGroup")
|
|
|
|
@onready var _damage_meter: ProgressBar = get_tree().get_first_node_in_group("DamageMeterGroup")
|
|
|
|
@onready
|
|
|
|
var _structure_meter: StructureMeter = get_tree().get_first_node_in_group("StructureMeterGroup")
|
2024-07-23 00:25:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
func is_boosting() -> bool:
|
|
|
|
return animation_tree["parameters/boost/active"]
|
2024-07-22 19:37:26 -06:00
|
|
|
|
2024-07-20 13:50:12 -06:00
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2024-07-20 15:27:33 -06:00
|
|
|
var delta_factor: float = delta * TARGET_FPS
|
|
|
|
|
2024-07-23 14:23:45 -06:00
|
|
|
# DEBUG BLOCK {{{
|
|
|
|
if Input.is_action_just_pressed("ui_left"):
|
|
|
|
_structure_meter.structure_count -= 1
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_right"):
|
|
|
|
_structure_meter.structure_count += 1
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_up"):
|
|
|
|
_heat_meter.value += 1
|
|
|
|
_damage_meter.value -= 1
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_down"):
|
|
|
|
_heat_meter.value -= 1
|
|
|
|
_damage_meter.value += 1
|
|
|
|
# DEBUG BLOCK }}}
|
|
|
|
|
2024-07-24 11:22:56 -06:00
|
|
|
var effective_gravity: float = gravity
|
|
|
|
|
2024-07-23 00:25:13 -06:00
|
|
|
if is_boosting():
|
|
|
|
_crosshair.jostle(Vector2(randf_range(-3, 3), randf_range(-3, 3)))
|
2024-07-24 11:22:56 -06:00
|
|
|
effective_gravity *= 0.1
|
|
|
|
|
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity.y -= effective_gravity * delta
|
|
|
|
animation_tree["parameters/jump_state/transition_request"] = "in_air"
|
|
|
|
animation_tree["parameters/anim_state/transition_request"] = "air"
|
|
|
|
elif animation_tree["parameters/anim_state/current_state"] == "air":
|
|
|
|
# If not on floor but still in jump state, transition back to landing
|
|
|
|
animation_tree["parameters/jump_state/transition_request"] = "end"
|
2024-07-20 13:50:12 -06:00
|
|
|
|
|
|
|
# Handle jump.
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
2024-07-20 15:27:33 -06:00
|
|
|
velocity.y = JUMP_FORCE
|
2024-07-23 00:25:13 -06:00
|
|
|
if is_boosting():
|
|
|
|
# Jump cancels boost
|
|
|
|
animation_tree["parameters/boost/request"] = 2 # Abort
|
2024-07-20 13:50:12 -06:00
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
2024-07-20 15:27:33 -06:00
|
|
|
var input_dir: Vector2 = Input.get_vector("left", "right", "forward", "backward")
|
|
|
|
if input_dir:
|
2024-07-23 00:25:13 -06:00
|
|
|
# Transform movement based on camera angle
|
2024-07-20 15:27:33 -06:00
|
|
|
var movement: Vector3 = (
|
2024-07-22 17:37:09 -06:00
|
|
|
(
|
|
|
|
camera_root.global_transform.basis
|
|
|
|
* Vector3(input_dir.x, 0.0, input_dir.y)
|
|
|
|
* Vector3(-1.0, 0.0, -1.0)
|
|
|
|
)
|
2024-07-22 15:23:32 -06:00
|
|
|
. normalized()
|
|
|
|
)
|
2024-07-23 00:25:13 -06:00
|
|
|
if not is_boosting():
|
|
|
|
velocity.x = lerpf(
|
|
|
|
velocity.x, movement.x * BASE_SPEED, delta_factor * INPUT_SENSITIVITY
|
|
|
|
)
|
|
|
|
velocity.z = lerpf(
|
|
|
|
velocity.z, movement.z * BASE_SPEED, delta_factor * INPUT_SENSITIVITY
|
|
|
|
)
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("boost"):
|
|
|
|
velocity.x += movement.x * BOOST_FORCE
|
|
|
|
velocity.z += movement.z * BOOST_FORCE
|
|
|
|
velocity.y = 0.0
|
|
|
|
mesh.rotation.y = atan2(velocity.x, velocity.z)
|
|
|
|
animation_tree["parameters/boost/request"] = 1
|
|
|
|
_overlay.play_boost()
|
|
|
|
animation_player.play("boost_fov")
|
|
|
|
animation_player.seek(0)
|
2024-07-20 15:27:33 -06:00
|
|
|
|
2024-07-23 00:25:13 -06:00
|
|
|
# Slowly turn mesh towards camera vector when moving on ground
|
2024-07-22 16:45:54 -06:00
|
|
|
if is_on_floor():
|
|
|
|
mesh.rotation.y = lerp_angle(
|
|
|
|
mesh.rotation.y, camera_root.rotation.y, delta_factor * TURN_SENSITIVITY
|
|
|
|
)
|
2024-07-22 17:08:06 -06:00
|
|
|
|
|
|
|
animation_tree["parameters/walk_space/blend_position"] = lerp(
|
|
|
|
animation_tree["parameters/walk_space/blend_position"],
|
|
|
|
input_dir,
|
|
|
|
delta_factor * TURN_SENSITIVITY
|
|
|
|
)
|
2024-07-20 15:27:33 -06:00
|
|
|
|
2024-07-23 00:25:13 -06:00
|
|
|
var drag: float = FRICTION if (is_on_floor() and not is_boosting()) else AIR_DRAG
|
2024-07-20 15:27:33 -06:00
|
|
|
velocity.x = lerpf(velocity.x, 0.0, delta_factor * drag)
|
|
|
|
velocity.z = lerpf(velocity.z, 0.0, delta_factor * drag)
|
2024-07-20 13:50:12 -06:00
|
|
|
|
|
|
|
move_and_slide()
|
2024-07-22 19:37:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
func _on_mech_stomp() -> void:
|
2024-07-22 20:48:04 -06:00
|
|
|
_crosshair.jostle(Vector2(randf_range(-1, 1), randf_range(-1, 5)))
|
2024-07-22 19:37:26 -06:00
|
|
|
# camera_root.jostle(randf_range(-2, 2))
|