Files
etazhi/player.gd
T
2026-11-01 16:00:46 +03:00

62 lines
2.2 KiB
GDScript

extends Node3D
var _rotation_target_x : Vector3
var _rotation_target_y : Vector3
@export var _cam : Camera3D
@export var _ray : RayCast3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
_rotation_target_y = self.rotation
_rotation_target_x = _cam.rotation
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
# TODO: Interpolation.
# TODO: Hiding/Popping animation.
func _physics_process(_delta: float) -> void:
var wmark := get_parent_node_3d().get_node("WallMark") as Sprite3D
var fmark := get_parent_node_3d().get_node("FloorMark") as Sprite3D
# Walls.
if _ray.is_colliding() and (_ray.get_collider() as StaticBody3D).get_collision_layer_value(3):
var normflat := Vector3(_ray.get_collision_normal().x, 0, _ray.get_collision_normal().z)
var posflat := Vector3(_ray.get_collision_point().x, 0, _ray.get_collision_point().z)
wmark.look_at_from_position(posflat, posflat + normflat, Vector3.UP)
wmark.position = posflat + normflat
wmark.show()
else:
wmark.hide()
# Floor.
if _ray.is_colliding() and (_ray.get_collider() as StaticBody3D).get_collision_layer_value(2):
var posflat := Vector3(_ray.get_collision_point().x, 0, _ray.get_collision_point().z)
fmark.position = posflat
fmark.show()
else:
fmark.hide()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
self.rotation.y = lerp_angle(_rotation_target_y.y, _rotation_target_y.y, 1.0 - exp(-30 * delta))
_cam.rotation.x = lerp_angle(_cam.rotation.x, _rotation_target_x.x, 1.0 - exp(-20 * delta))
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
var motion := event as InputEventMouseMotion
var preserve := rotation
rotation = _rotation_target_y
self.rotate_y(deg_to_rad(motion.relative.x) * -0.25)
_rotation_target_y = rotation
rotation = preserve
preserve = _cam.rotation
_cam.rotate_x(deg_to_rad(motion.relative.y) * -0.2)
var camera_rot := _cam.rotation_degrees
camera_rot.x = clamp(camera_rot.x, -30, 30)
_cam.rotation_degrees = camera_rot
_rotation_target_x = _cam.rotation
_cam.rotation = preserve