bazar_alea/client/scenes/player.gd
2023-10-22 20:42:13 +02:00

67 lines
2.2 KiB
GDScript

extends CharacterBody3D
const SPEED = 30.0
const JUMP_VELOCITY = 14.5
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@export var event_disabled:bool = true
# How fast the player moves in meters per second.
@export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75
var target_velocity = Vector3.ZERO
var direction = Vector3.FORWARD
var h_sensitivity:float = 0.1
var v_sensitivity:float = 0.1
var camrot_h:float = 0.0
var camrot_v:float = 0.0
var h_acceleration:float = 10.0
var v_acceleration:float = 10.0
var input_view_camera_move_player_follow_mouse:bool = false
func _input(event):
if event_disabled:
return
if event is InputEventMouseButton:
if Input.is_action_pressed("ui_rotate_player"):
input_view_camera_move_player_follow_mouse = true
else:
input_view_camera_move_player_follow_mouse = false
if event is InputEventMouseMotion and input_view_camera_move_player_follow_mouse:
camrot_h += -event.relative.x * h_sensitivity
camrot_v += -event.relative.y * v_sensitivity
func _physics_process(delta:float):
if event_disabled:
return
var h_rot:float = $h.global_transform.basis.get_euler().y
# Add the gravity.
if not is_on_floor():
velocity.y -= 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("ui_left", "ui_right", "ui_up", "ui_down").rotated(-h_rot)
direction = (transform.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)
$h.rotation_degrees.y = lerp($h.rotation_degrees.y, camrot_h, delta * h_acceleration)
$h/v.rotation_degrees.x = lerp($h/v.rotation_degrees.x, camrot_v, delta * v_acceleration)
move_and_slide()
func set_enable_event(state:bool):
event_disabled = ! state