85 lines
2.5 KiB
GDScript
85 lines
2.5 KiB
GDScript
extends RayCast3D
|
|
|
|
var current_target: Node = null
|
|
var curTarget: Node = null
|
|
@export var mode = 0;
|
|
var rot : int = 0
|
|
|
|
func _process(delta: float) -> void:
|
|
if Input.is_key_pressed(KEY_E):
|
|
rot += 2
|
|
if Input.is_key_pressed(KEY_Q):
|
|
rot -= 2
|
|
|
|
if Input.is_key_pressed(KEY_1):
|
|
mode = 0
|
|
elif Input.is_key_pressed(KEY_2):
|
|
mode = 1
|
|
elif Input.is_key_pressed(KEY_3):
|
|
mode = 2
|
|
|
|
|
|
func spawnObjectByID(ObjectID:String, position:Vector3, rotation:int):
|
|
var segments = ObjectID.split(":")
|
|
var objectScene = load("res://WorldObjects/{namespace}/{id}.tscn".format({"namespace": segments[0], "id": segments[1]}))
|
|
|
|
var object = objectScene.instantiate()
|
|
object.global_position = position
|
|
object.rotation_degrees = Vector3(0, rotation, 0)
|
|
|
|
get_tree().current_scene.get_node("objects").add_child(object)
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if not MainGame.canPlaceBlock:
|
|
var is_left = event.button_index == MOUSE_BUTTON_LEFT
|
|
var is_right = event.button_index == MOUSE_BUTTON_RIGHT
|
|
|
|
if is_left or is_right:
|
|
if event.pressed:
|
|
if is_colliding():
|
|
var collider = get_collider()
|
|
var target = _find_interactable_node(collider)
|
|
if target:
|
|
current_target = target
|
|
if current_target.has_method("_interact_press"):
|
|
# If it's a right click, pass true for the alt interaction
|
|
current_target._interact_press(is_right)
|
|
else:
|
|
# Handle releasing the click
|
|
if current_target and current_target.has_method("_interact_release"):
|
|
current_target._interact_release()
|
|
current_target = null
|
|
elif MainGame.canPlaceBlock:
|
|
var is_left = event.button_index == MOUSE_BUTTON_LEFT
|
|
|
|
if is_left:
|
|
if event.pressed:
|
|
spawnObjectByID(MainGame.selectedBlockID, get_collision_point() + Vector3(0, 0.5, 0), rot)
|
|
if MainGame.currentHeldItemID == "base:hammer":
|
|
var is_left = event.button_index == MOUSE_BUTTON_LEFT
|
|
if is_left:
|
|
if event.pressed:
|
|
var collider = get_collider()
|
|
var target = _findBreakableNode(collider)
|
|
if target:
|
|
curTarget = target
|
|
if curTarget.has_method("_destroy"):
|
|
curTarget._destroy()
|
|
|
|
|
|
func _find_interactable_node(node: Node) -> Node:
|
|
while node != null:
|
|
if node.has_method("_interact_press"):
|
|
return node
|
|
node = node.get_parent()
|
|
return null
|
|
|
|
func _findBreakableNode(node: Node) -> Node:
|
|
while node != null:
|
|
if node.has_method("_destroy"):
|
|
return node
|
|
node = node.get_parent()
|
|
return null
|