OpenWorldVGDL/GameElements/player.gd

92 lines
3.0 KiB
GDScript3
Raw Normal View History

2025-01-17 13:42:05 +00:00
extends CharacterBody3D
@export var PlayerPhantomCam: PhantomCamera3D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@onready var MainCamera: Camera3D = get_viewport().get_camera_3d()
@export var mouse_sensitivity: float = 0.5
@export var min_pitch: float = -89.9
@export var max_pitch: float = 50
@export var min_yaw: float = 0
@export var max_yaw: float = 360
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(delta: float) -> void:
if DialogueState.start_play:
return
if Input.is_action_just_released('ui_cancel'):
if Input.mouse_mode != Input.MOUSE_MODE_VISIBLE:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta: float) -> void:
if DialogueState.start_play:
return
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
#var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
#Camera3D
var direction := (MainCamera.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if direction.length_squared() != 0:
animation_player.play("walk")
rotation.y = MainCamera.rotation.y + PI
else:
animation_player.play("idle")
move_and_slide()
func _unhandled_input(event: InputEvent) -> void:
if DialogueState.start_play:
return
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
_set_pcam_rotation(PlayerPhantomCam, event)
func _set_pcam_rotation(pcam: PhantomCamera3D, event: InputEvent) -> void:
if event is InputEventMouseMotion:
var pcam_rotation_degrees: Vector3
# Assigns the current 3D rotation of the SpringArm3D node - so it starts off where it is in the editor
pcam_rotation_degrees = pcam.get_third_person_rotation_degrees()
# Change the X rotation
pcam_rotation_degrees.x -= event.relative.y * mouse_sensitivity
# Clamp the rotation in the X axis so it go over or under the target
pcam_rotation_degrees.x = clampf(pcam_rotation_degrees.x, min_pitch, max_pitch)
# Change the Y rotation value
pcam_rotation_degrees.y -= event.relative.x * mouse_sensitivity
# Sets the rotation to fully loop around its target, but witout going below or exceeding 0 and 360 degrees respectively
pcam_rotation_degrees.y = wrapf(pcam_rotation_degrees.y, min_yaw, max_yaw)
# Change the SpringArm3D node's rotation and rotate around its target
pcam.set_third_person_rotation_degrees(pcam_rotation_degrees)