grunk/src/player/tool_mount.gd

49 lines
1.2 KiB
GDScript

class_name ToolMount extends Node3D
## Access controller for the player's active & available tools
@export var initial_tool: Tool
var _active: Tool
func _ready() -> void:
set_active(initial_tool)
## Returns the currently-active tool.
func get_active() -> Tool:
return _active
## Sets the given tool as active.
##
## The tool should be a child of this mount.
## If the tool is not unlocked and `force` is false, this will have no effect.
func set_active(tool: Tool, force: bool = false) -> void:
if not force and not tool.unlocked():
return
for c: Node3D in get_children():
c.visible = false
# TODO unequip animation?
_active = tool
_active.visible = true
_active.on_equip()
# TODO equip animation?
## Sets the active tool by index relative to the currently-active tool.
##
## Use this for "select next/prev tool" functions.
func set_active_relative(delta: int) -> void:
var active_idx := _active.get_index()
while true:
var new_idx := wrapi(active_idx + delta, 0, get_child_count())
var tool := get_child(new_idx) as Tool
if tool.unlocked():
set_active(tool)
return
# If the next tool is not unlocked, try the one after
# NOTE: this will loop forever if the player has no tools unlocked!
active_idx = new_idx