ChronoChamber/Scripts/Player.gd
2024-08-10 21:26:51 +10:00

88 lines
2.6 KiB
GDScript

extends CharacterBody3D
@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
@export var held_object : Node3D
@export var has_object : bool = false
@export var hand : Node3D
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")
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.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
#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("drop"):
drop()
# interact with stuff
if Input.is_action_just_pressed("interact"):
if "name" in raycast.get_collider():
if "command_name" in raycast.get_collider():
print(raycast.get_collider().command_name)
#hand.add_child(held_object)
if(!has_object):
held_object = raycast.get_collider()
held_object.reparent(hand)
held_object.position = Vector3(0,0,0)
held_object.freeze = true
has_object = true
else:
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.
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()
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))
func drop():
if has_object:
held_object.reparent(get_tree().get_root())
held_object.freeze = false
has_object = false