47 lines
1.7 KiB
GDScript
47 lines
1.7 KiB
GDScript
extends Node2D
|
|
class_name InteractionWheel
|
|
|
|
signal closed
|
|
|
|
var interactions: Array[Interaction]
|
|
var grid_position: Vector2i
|
|
|
|
@onready var interaction_icons: Array[Node] = $Interactions.get_children()
|
|
@onready var cursor: Sprite2D = $Cursor
|
|
@onready var information_panel: Panel = $InformationPanel
|
|
@onready var interaction_label: Label = $InformationPanel/InteractionLabel
|
|
|
|
func initialize(interaction_location: Vector2i, interactions: Array[Interaction]) -> void:
|
|
grid_position = interaction_location
|
|
position = Grid.grid_to_world_center(interaction_location)
|
|
self.interactions = interactions
|
|
|
|
func _ready() -> void:
|
|
for i in range(interactions.size()):
|
|
interaction_icons[i].texture = interactions[i].image
|
|
interaction_icons[i].show()
|
|
|
|
func _process(delta: float) -> void:
|
|
var selection_direction = Input.get_vector("view_left", "view_right", "view_up", "view_down")
|
|
if selection_direction:
|
|
cursor.rotation = selection_direction.angle()
|
|
cursor.show()
|
|
var selection = _get_selection_index(cursor.rotation)
|
|
if selection < interactions.size():
|
|
information_panel.show()
|
|
interaction_label.text = interactions[selection].name
|
|
else:
|
|
information_panel.hide()
|
|
if Input.is_action_just_pressed("select") and cursor.visible:
|
|
var selection = _get_selection_index(cursor.rotation)
|
|
print("Selection: %s Rotation: %s" % [selection, cursor.rotation])
|
|
if selection < interactions.size():
|
|
var next_interactions = interactions[selection].interact_at(grid_position, get_tree().root)
|
|
if next_interactions.is_empty():
|
|
closed.emit()
|
|
queue_free()
|
|
|
|
func _get_selection_index(angle: float) -> int:
|
|
var adjusted_angle = fposmod(angle + (PI / 2.0), 2.0 * PI)
|
|
return floor(((int(floor(adjusted_angle / (PI / 8.0))) + 1) % 16) / 2.0)
|