TinyAdventure/systems/BuildSystem.gd

91 lines
3.1 KiB
GDScript
Raw Normal View History

2024-09-08 22:06:34 +02:00
class_name BuildSystem
extends Node
const CAMERA_SPEED = 4.0
2024-09-10 22:26:26 +02:00
@onready var build_preview = %BuildPreview
2024-09-08 22:06:34 +02:00
@onready var player = %Player
2024-09-10 22:26:26 +02:00
@onready var built_structures = %BuiltStructures
@onready var camera:Camera3D = %Camera
2024-09-10 22:26:26 +02:00
@export var build_item:Item = null
2024-09-08 22:06:34 +02:00
var camera_velocity:Vector3 = Vector3.ZERO
var is_active:bool = false
2024-09-08 22:06:34 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
2024-09-10 22:26:26 +02:00
func update_build_preview_item() -> void:
for child in build_preview.get_children():
if build_item == null or child.scene_file_path != build_item.scene.resource_path:
child.get_parent().remove_child(child)
child.queue_free()
if build_preview.get_child_count() == 0 and build_item != null:
build_preview.add_child(build_item.scene.instantiate())
func move_build_camera(delta):
var input_dir = Input.get_vector("walk_left", "walk_right", "walk_forward", "walk_back")
var direction = (camera.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
camera_velocity.x = direction.x * CAMERA_SPEED
camera_velocity.z = direction.z * CAMERA_SPEED
else:
camera_velocity.x = move_toward(camera_velocity.x, 0, CAMERA_SPEED)
camera_velocity.z = move_toward(camera_velocity.z, 0, CAMERA_SPEED)
camera_velocity.y = 0
camera.global_position = camera.global_position + camera_velocity * delta
func update_build_preview():
var mouse_view_coords = get_viewport().get_mouse_position() # / get_viewport().get_visible_rect().size
var mouse_ray_origin = camera.project_ray_origin(mouse_view_coords)
var mouse_ray_normal = camera.project_ray_normal(mouse_view_coords)
var world_space_state = camera.get_world_3d().direct_space_state
var ray_query:PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.create(mouse_ray_origin, mouse_ray_origin + mouse_ray_normal * 100)
var ray_result:Dictionary = world_space_state.intersect_ray(ray_query)
if not ray_result.is_empty() and Vector3.UP.dot(ray_result["normal"]) > 0.9:
var build_location:Vector3 = ray_result["position"]
build_location = Vector3(roundf(build_location.x * 4), build_location.y, roundf(build_location.z * 4)) * 0.25
build_preview.global_position = build_location
2024-09-10 22:26:26 +02:00
func _physics_process(_delta):
if not is_active:
return
2024-09-10 22:26:26 +02:00
update_build_preview_item()
#move_build_camera(delta)
update_build_preview()
2024-09-10 22:26:26 +02:00
2024-09-27 18:09:30 +02:00
func _unhandled_input(event: InputEvent) -> void:
if not is_active:
build_preview.hide()
return
build_preview.show()
2024-09-27 18:09:30 +02:00
if event.is_action_pressed("rotate_clockwise"):
build_preview.global_basis = build_preview.basis.rotated(Vector3.UP, deg_to_rad(45))
2024-09-27 18:09:30 +02:00
get_viewport().set_input_as_handled()
if event.is_action_pressed("rotate_counter_clockwise"):
build_preview.global_basis = build_preview.basis.rotated(Vector3.UP, -deg_to_rad(45))
2024-09-27 18:09:30 +02:00
get_viewport().set_input_as_handled()
if build_item != null and event.is_action_pressed("interaction"):
var new_structure:Node3D = build_item.scene.instantiate()
new_structure.transform = build_preview.transform
built_structures.add_child(new_structure)
get_viewport().set_input_as_handled()
return