fx: input extracted to own node

This commit is contained in:
2026-03-15 17:08:40 +01:00
parent 0af538ff3f
commit f4e5d13cf8
5 changed files with 41 additions and 25 deletions

View File

@@ -23,8 +23,6 @@ var healthPoints: int = 100
var isAlive: bool = true
@onready var invulnerable_cooldown_timer: Timer = $InvulnerableCooldownTimer
var hasiframes: bool = false # only used for iframe after collision for now
var target = Vector2.ZERO # either vector2 or Node2D
var updating_target: bool = false # TODO: overhaul; make own node/script and actually expand logic properly
func _ready() -> void:
var screen_size = get_viewport_rect().size
@@ -37,27 +35,14 @@ func _ready() -> void:
sprite.play("default")
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
# start tracking
target = get_global_mouse_position()
updating_target = true
else:
# stop tracking
updating_target = false
func set_target(target) -> void:
$Input.target = target
func _physics_process(delta):
var pos
if typeof(target) == TYPE_VECTOR2:
pos = target
elif "position" in target:
pos = target.position
else:
return
var dir: Vector2 = position.direction_to(pos)
var target_pos = $Input.get_target_position()
var dir: Vector2 = position.direction_to(target_pos)
if position.distance_to(pos) > 2:
if position.distance_to(target_pos) > 2:
self.desired_rotation = atan2(dir.y, dir.x)
move_and_collide(speed * dir * delta)
position = GameManager.get_boundaried_position(position)
@@ -72,9 +57,6 @@ func _process(delta):
if self.rotation != self.desired_rotation:
self.rotation = lerp_angle(self.rotation, self.desired_rotation, clampf(4 * delta, 0, 1))
if typeof(target) == TYPE_VECTOR2 and updating_target:
target = get_global_mouse_position()
func try_attack() -> void:
if not can_attack:

View File

@@ -4,6 +4,7 @@
[ext_resource type="Texture2D" uid="uid://du1lb4nr47kvl" path="res://molecular/assets/player-sprite-attacking.png" id="2_rq1v6"]
[ext_resource type="Texture2D" uid="uid://bsd1qtw50esai" path="res://molecular/assets/player-sprite-attacking2.png" id="3_xiwfn"]
[ext_resource type="Texture2D" uid="uid://cd0iv6hsi3fad" path="res://molecular/assets/player-sprite.png" id="4_rq1v6"]
[ext_resource type="Script" uid="uid://dehus7ao5xv2l" path="res://molecular/player_input.gd" id="5_xiwfn"]
[sub_resource type="SpriteFrames" id="SpriteFrames_onrkg"]
animations = [{
@@ -49,6 +50,9 @@ animation = &"attacking"
rotation = -1.5732701
shape = SubResource("CapsuleShape2D_4flbx")
[node name="Input" type="Node" parent="." unique_id=2043215647]
script = ExtResource("5_xiwfn")
[node name="AttackArea" type="Area2D" parent="." unique_id=187975387]
position = Vector2(0, 56)
rotation = -1.5732701

View File

@@ -0,0 +1,29 @@
extends Node
var target = Vector2.ZERO # should be either Vector2 or Node2D
var updating: bool = false
var holding_lmb: bool = false
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
# start tracking
target = owner.get_global_mouse_position()
updating = true
else:
# stop tracking
updating = false
func _process(delta):
if typeof(target) == TYPE_VECTOR2 and updating:
target = owner.get_global_mouse_position()
func get_target_position() -> Vector2:
if typeof(target) == TYPE_VECTOR2 or typeof(target) == TYPE_VECTOR3:
return target
elif "position" in target:
return target.position
else:
return Vector2.ZERO
target = Vector2.ZERO # reset target to a Vector2

View File

@@ -0,0 +1 @@
uid://dehus7ao5xv2l