Untested PickupableItem

This commit is contained in:
Alexey 2025-07-31 15:33:31 +03:00
commit 63e87e86d5
19 changed files with 200 additions and 17 deletions

View file

@ -0,0 +1,13 @@
extends Resource
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

View file

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

View file

@ -0,0 +1,26 @@
extends Sprite3D
class_name InteractiveObject
var player: Player = null
func _ready() -> void:
pass
func _process(_delta: float) -> void:
if player == null:
return
if Input.is_action_just_pressed('interact'):
_interact(player)
func _on_body_entered(body: Node3D) -> void:
if body is Player:
player = body
func _on_body_exited(body: Node3D) -> void:
if body is Player:
player = null
## Invoked when interact action was used when player is in object's area
func _interact(_player: Player) -> void:
pass

View file

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

View file

@ -0,0 +1,13 @@
extends InteractiveObject
class_name PickupableItem
@export var content: BasePickupableResource
func _ready() -> void:
if content.texture != null:
texture = content.texture
func _interact(_player: Player) -> void:
if content._apply(_player):
queue_free()

View file

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

View file

@ -0,0 +1,27 @@
extends BasePickupableResource
class_name WeaponPickupableResource
## Weapon that will be added on interaction
@export var weapon: PackedScene
func _apply(player: Player) -> bool:
var slot = player.weapons.first_free_slot()
# TODO: Implement proper swapping behavior when old or new weapon uses several slots
if slot == null:
slot = player.weapons.current_slot
var set_data = slot.set_weapon(weapon)
set_data.old_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 == ""

View file

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

View file

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

View file

@ -1,5 +1,7 @@
extends CharacterBody3D
class_name Player
@export var speed = 100.0
@export var fall_acceleration = 75.0
@export var vertical_sensivity = 0.005

View file

@ -27,3 +27,13 @@ func select_slot(index: int):
current_slot = slots[index]
current_weapon = current_slot.weapon
slot_selected.emit()
func first_free_slot() -> WeaponSlot:
for slot in slots:
if not slot.has_weapon:
return slot
return null
func refresh_current_slot() -> void:
var index = slots.find(current_slot)
select_slot(index)

View file

@ -5,6 +5,17 @@ class_name WeaponSlot
var has_weapon = false
var weapon: Weapon
## Contains data about swapped weapons
class WeaponSetData:
## New Weapon node, may be null
var new_weapon: Weapon
## Old weapon id, may be empty string
var old_weapon: StringName
func _init(_new_weapon: Weapon, _old_weapon: StringName):
new_weapon = _new_weapon
old_weapon = _old_weapon
func _ready():
has_weapon = get_child_count() > 0
@ -12,3 +23,20 @@ func _ready():
var child = get_child(0)
assert(child is Weapon)
weapon = child as Weapon
func set_weapon(new_weapon: PackedScene) -> WeaponSetData:
var weapon_inst = null
if new_weapon != null:
weapon_inst = new_weapon.instantiate() as Weapon
add_child(weapon_inst)
var old_weapon_id = drop_weapon()
return WeaponSetData.new(weapon_inst, old_weapon_id)
func drop_weapon() -> StringName:
var id = ""
if has_weapon:
id = weapon.id
weapon.queue_free()
_ready()
return id

View file

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