Finished basic PickupableItem

This commit is contained in:
Alexey 2025-08-01 13:11:26 +03:00
commit 88c56f8aed
21 changed files with 163 additions and 35 deletions

View file

@ -0,0 +1,35 @@
extends Node
## Dictionary, that holds resource id and path to it
var resource_registry: Dictionary = {}
const DEFAULT_WEAPON_RESOURCE_ID: StringName = "weapon_base"
## Recursively add resources to registry
func add_resource_path(_path: String) -> void:
var dir = DirAccess.open(_path)
if dir == null:
return
# Used to ignore last slash if it was provided in _path
var path = dir.get_current_dir()
for subdir in dir.get_directories():
var subdir_path = "%s/%s" % [ path, subdir ]
add_resource_path(subdir_path)
for filename in dir.get_files():
if !filename.ends_with('.tres'):
continue
var filepath = "%s/%s" % [ path, filename ]
var res: IdentifiedResource = ResourceLoader.load(filepath, 'IdentifiedResource')
resource_registry[res.id] = filepath
func get_resource(id: String) -> IdentifiedResource:
var path = resource_registry.get(id)
if path == null:
return null
var res: IdentifiedResource = ResourceLoader.load(path, 'IdentifiedResource')
return res
func _init() -> void:
add_resource_path('res://base/assets/resources')
assert(resource_registry.has(DEFAULT_WEAPON_RESOURCE_ID))

View file

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

View file

@ -0,0 +1,7 @@
extends Resource
## Basic resource, holding its string identifier
class_name IdentifiedResource
## Item ID, make sure it's unique
@export var id: StringName

View file

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

View file

@ -1,13 +1,10 @@
extends Resource
extends IdentifiedResource
class_name BasePickupableResource
## Item ID, make sure it's unique
@export var id: StringName
## Texture that will be used in node sprite
@export var texture: Texture2D
## Emitted when item picked up. Returns if PickupableItem should be freed
func _apply(_player: Player) -> bool:
return true
## Emitted when item picked up. Returns IdentifiedResource if it should be swapped
func _apply(_player: Player) -> IdentifiedResource:
return null

View file

@ -9,5 +9,8 @@ func _ready() -> void:
texture = content.texture
func _interact(_player: Player) -> void:
if content._apply(_player):
content = content._apply(_player)
if content == null:
queue_free()
return
_ready()

View file

@ -5,7 +5,7 @@ class_name WeaponPickupableResource
## Weapon that will be added on interaction
@export var weapon: PackedScene
func _apply(player: Player) -> bool:
func _apply(player: Player) -> IdentifiedResource:
var slot = player.weapons.first_free_slot()
# TODO: Implement proper swapping behavior when old or new weapon uses several slots
@ -13,15 +13,11 @@ func _apply(player: Player) -> bool:
slot = player.weapons.current_slot
var set_data = slot.set_weapon(weapon)
set_data.old_weapon.id = id
set_data.new_weapon.id = id
if slot == player.weapons.current_slot:
player.weapons.refresh_current_slot()
return update_by_id(set_data.new_weapon)
# TODO: implement proper updating
func update_by_id(new_id: StringName) -> bool:
id = new_id
return id == ""
var new_res = ResourceHandler.get_resource(set_data.old_weapon) as WeaponPickupableResource
print(new_res)
return new_res

View file

@ -17,10 +17,11 @@ class WeaponSetData:
old_weapon = _old_weapon
func _ready():
has_weapon = get_child_count() > 0
var child_count = get_child_count()
has_weapon = child_count > 0
if has_weapon:
var child = get_child(0)
var child = get_child(child_count - 1)
assert(child is Weapon)
weapon = child as Weapon

View file

@ -6,7 +6,7 @@ signal fired()
signal fire_failed()
signal fire_allowed()
var id: StringName
var id: StringName = ResourceHandler.DEFAULT_WEAPON_RESOURCE_ID
@onready var barrel = $"Barrel"
@export var uses_hands: Array[CommandQueue.Side]