74 lines
1.8 KiB
GDScript
74 lines
1.8 KiB
GDScript
extends Node2D
|
|
|
|
@export var player: Node:
|
|
set(val):
|
|
if val is Player:
|
|
player = val
|
|
else:
|
|
player = null
|
|
|
|
@export var camera: Camera2D
|
|
|
|
@export var available_commands: Array[PackedScene]
|
|
|
|
signal commands_changed(commands)
|
|
|
|
func _ready():
|
|
%stock.visible = false
|
|
%wheel.visible = false
|
|
|
|
if available_commands:
|
|
for command_scene in available_commands:
|
|
var command := command_scene.instantiate()
|
|
command.player = player
|
|
%stock.add_child(command)
|
|
|
|
func _show_command_wheel(pos: Vector2):
|
|
var wheel := %wheel as Node2D
|
|
wheel.position = pos
|
|
|
|
if camera:
|
|
camera.global_position = pos
|
|
|
|
for command in %wheel.get_children():
|
|
command.queue_free()
|
|
|
|
var inc := PI * 2.0 / %stock.get_child_count()
|
|
var idx := 0
|
|
for command in %stock.get_children():
|
|
# TODO only available
|
|
command = command.duplicate()
|
|
command.position = Vector2.from_angle(inc * idx) * 200.0
|
|
command.connect("clicked", _on_wheel_command_clicked.bind(command), CONNECT_ONE_SHOT)
|
|
var line := Line2D.new()
|
|
line.add_point(Vector2.ZERO)
|
|
line.add_point(command.position)
|
|
%wheel.add_child(line)
|
|
%wheel.add_child(command)
|
|
idx += 1
|
|
|
|
wheel.visible = true
|
|
%wheelanim.play("show")
|
|
|
|
func _on_wheel_command_clicked(command: Command):
|
|
%wheel.visible = false
|
|
command.apply(%active, %wheel.position)
|
|
command.scale *= 0.5
|
|
command.connect("clicked", _on_active_command_clicked.bind(command))
|
|
commands_changed.emit(%active.get_children())
|
|
|
|
func _on_active_command_clicked(command: Command):
|
|
command.queue_free()
|
|
commands_changed.emit(%active.get_children())
|
|
|
|
func background_clicked(pos):
|
|
if %wheel.visible:
|
|
%wheelanim.play("hide")
|
|
else:
|
|
if camera:
|
|
pos = get_viewport().canvas_transform.affine_inverse() * pos
|
|
_show_command_wheel(pos)
|
|
|
|
func get_active():
|
|
return %active.get_children()
|