ChronoChamber/Scripts/Player.gd

63 lines
2 KiB
GDScript3
Raw Normal View History

extends CharacterBody3D
2024-08-10 16:00:46 +10:00
@export var camera : Camera3D
@export var mouse_sensitivity : float = 0.003 # TODO: this is sketchy check that its framerate independant pls
@export var raycast : RayCast3D
2024-08-10 17:12:20 +10:00
signal object_clicked(object : Node3D)
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
2024-08-10 16:00:46 +10:00
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle jump.
2024-08-10 16:00:46 +10:00
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
2024-08-10 16:00:46 +10:00
#free the mouse for debugging
if Input.is_action_just_pressed("esc"):
if (Input.mouse_mode == Input.MOUSE_MODE_CAPTURED):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
else:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
# interact with stuff
if Input.is_action_just_pressed("interact"):
2024-08-10 17:12:20 +10:00
if "name" in raycast.get_collider():
print(raycast.get_collider().name)
object_clicked.emit(raycast.get_collider())
else:
print("womp womp")
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
2024-08-10 16:00:46 +10:00
var input_dir = Input.get_vector("mov_left", "mov_right", "mov_up", "mov_down")
var 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)
move_and_slide()
2024-08-10 16:00:46 +10:00
func _input(event):
# camera control
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
rotate_y(-event.relative.x * mouse_sensitivity)
camera.rotate_x(-event.relative.y * mouse_sensitivity)
camera.rotation.x = clampf(camera.rotation.x, -deg_to_rad(70), deg_to_rad(70))