Files
SpaceBots/Minigames/Components/dragComponent.gd

89 lines
2.7 KiB
GDScript3
Raw Normal View History

2026-01-21 23:40:20 +01:00
extends Node2D
class_name DragComponent
@export var MouseIntComp: MouseInteractionComponent ##Where the player klicks to drag the component
@export var intBox: InteractBoxMinigame ##Used to determine if this component interacts with another component
@export var canDrag: bool = true ##If true the component can be moved by dragging it
@export var singleDrag: bool = true ##If true the component can only be moved when its the only dragged component
@export var resetWhenLetGo: bool = false ##If the player stops dragging the component returns to its home pos
@export var lockWhenatGoal: bool = false ##If the component reaches a goal, it cannot be dragged anymore
@export var openLockOnClick: bool = false ##If the component is clicked (Impulse) it will stop being locked at its goal (THIS REQUIRES LOCK WHEN AT GOAL TO BE TRUE)
@export var goalID: int = 0 ##What goals this component can interact with (All goals of the same ID)
@export var homePos: Vector2 = Vector2(0,0) ##If this is 0,0 it gets set to the current position on ready
@export var goalPos: Vector2 = Vector2(0,0) ##Position of goal where to component can be dragged to stay there
var leftMouseClicked: bool = false
var atGoal: bool = false
var goalSocket: GoalBox
var mousePos: Vector2 = Vector2(0,0)
func _ready() -> void:
if intBox:
intBox.area_entered.connect(areaEntered)
intBox.area_exited.connect(areaExited)
if MouseIntComp:
MouseIntComp.stickyHold = true
MouseIntComp.actionPressed.connect(interactionClicked)
if !homePos:
homePos = self.position
else:
self.position = homePos
func _input(event: InputEvent) -> void:
if event is InputEventMouse:
mousePos = event.position
func _process(_delta: float) -> void:
if atGoal and lockWhenatGoal:
self.position = goalPos
canDrag = false
MouseIntComp.monitorable = false
return
else:
MouseIntComp.monitorable = true
if openLockOnClick and !atGoal:
lockWhenatGoal = true
if !canDrag: return
if MouseIntComp.actionHeld:
self.position = mousePos
elif atGoal:
self.position = goalPos
elif resetWhenLetGo:
self.position = homePos
func interactionClicked() -> void:
if openLockOnClick and atGoal:
lockWhenatGoal = false
canDrag = true
func areaEntered(area: Area2D) -> void:
if area is GoalBox:
if area.goalID == goalID:
goalPos = area.position
atGoal = true
goalSocket = area
if area is MouseInteractionComponent and singleDrag and canDrag:
MouseIntComp.actionHeld = false
self.position = homePos
func areaExited(area: Area2D) -> void:
if area is GoalBox:
if area.goalID == goalID:
atGoal = false
if area == goalSocket:
goalSocket = null
func adaptCoordinateScale(input: Vector2) -> Vector2:
return input / Vector2(1152,648) * Vector2(DisplayServer.window_get_size())