arm movement

This commit is contained in:
oughtum 2024-08-16 23:07:59 +01:00
parent f3e161d48c
commit f16e42a8d0
4 changed files with 49 additions and 19 deletions

View file

@ -1,7 +1,9 @@
class_name Arm extends Node3D
@export var action: StringName
@onready var camera: Camera3D = $"../../Camera3D"
@onready var target: Marker3D = $Target
@export var action: StringName
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@ -13,10 +15,17 @@ func _process(delta: float) -> void:
pass
func _arm_enabled(arm):
print_debug("enabled!")
pass
func _arm_disabled(arm):
print_debug("disabled!")
pass
func update_target_pos() -> void:
var mouse_pos: Vector2 = camera.get_window().get_mouse_position()
var origin: Vector3 = camera.project_ray_origin(mouse_pos)
var dir: Vector3 = origin + (camera.project_ray_normal(mouse_pos) * 100)
var world_space: PhysicsDirectSpaceState3D = camera.get_window().world_3d.direct_space_state
var ray_query = PhysicsRayQueryParameters3D.create(origin, dir)
var collision: Dictionary = world_space.intersect_ray(ray_query)
if collision == null: return
var arm_target_pos: Vector3 = collision["position"]
target.global_position = arm_target_pos

View file

@ -1,21 +1,25 @@
class_name Body extends Node3D
@export var arms: Array[Arm]
@export var pull_strength: float = 5.0
signal arm_enabled(arm)
signal arm_disabled(arm)
var active_arm: Arm
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
for arm in arms:
arm_enabled.connect(arm._arm_enabled)
arm_disabled.connect(arm._arm_disabled)
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
func _unhandled_input(event: InputEvent) -> void:
for arm in arms:
if Input.is_action_just_pressed(arm.action):
arm_enabled.emit(arm)
if Input.is_action_pressed(arm.action):
active_arm = arm
return
if Input.is_action_just_released(arm.action):
arm_disabled.emit(arm)
active_arm = null
return
func _physics_process(delta: float) -> void:
if active_arm == null: return
active_arm.update_target_pos()