Skill
godot-tscn-format
Godot 4.x .tscn 场景文件纯文本格式规范与 AI 操作指南 — 文件结构、资源引用、 Transform 变换、属性值格式、信号连接,以及 AI 直接读写 .tscn 文件的操作方法。
Godot 4背包系统完整实现,包括数据层、逻辑层和UI层设计。支持物品管理、拖放交互、格子满了处理等常见功能。
$ npx -y skills add 925236118/AlphaAgent --skill godot-inventory-system --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/godot-inventory-systemContext preview
The summary Claude sees to decide when to auto-load this skill.
Godot 4背包系统完整实现,包括数据层、逻辑层和UI层设计。支持物品管理、拖放交互、格子满了处理等常见功能。
name: godot-inventory-system description: Godot 4背包系统完整实现,包括数据层、逻辑层和UI层设计。支持物品管理、拖放交互、格子满了处理等常见功能。
完整的背包系统实现,支持物品添加、移除、拖放排序、格子容量管理等功能。
背包系统
├── 数据层 (Data Layer)
│ ├── InventoryItem - 单个物品数据
│ └── InventorySlot - 格子数据(包含物品引用和数量)
├── 逻辑层 (Business Layer)
│ └── InventoryComponent - CRUD操作、堆叠逻辑
└── UI层 (Presentation Layer)
├── InventoryUI - 背包主面板
└── InventorySlotUI - 单个格子UI# inventory_item.gd
class_name InventoryItem
extends Resource
## 物品唯一标识符
@export var id: StringName
## 物品显示名称
@export var item_name: String
## 物品描述
@export_multiline var description: String
## 最大堆叠数量
@export var max_stack: int = 99
## 物品图标
@export var icon: Texture2D
## 物品 Prefab(可选,用于拖放到场景)
@export var item_scene: PackedScene
## 物品稀有度
@export_enum("Common", "Uncommon", "Rare", "Epic", "Legendary") var rarity: int = 0
## 是否可以丢弃
@export var can_drop: bool = true
## 是否可以堆叠
@export var can_stack: bool = true
## 物品价值(用于商店)
@export var value: int = 1# inventory_slot.gd
class_name InventorySlot
extends RefCounted
## 格子索引
var index: int
## 格子中的物品
var item: InventoryItem
## 物品数量
var quantity: int = 0
## 格子是否为空
var is_empty: bool:
get: return item == null or quantity <= 0
func can_add(new_item: InventoryItem, amount: int = 1) -> bool:
if is_empty:
return true
if item.id == new_item.id and item.can_stack:
return quantity + amount <= item.max_stack
return false
func add(new_item: InventoryItem, amount: int = 1) -> bool:
if not can_add(new_item, amount):
return false
if is_empty:
item = new_item
quantity = amount
else:
quantity += amount
return true
func remove(amount: int = 1) -> bool:
if is_empty or quantity < amount:
return false
quantity -= amount
if quantity <= 0:
clear()
return true
func clear() -> void:
item = null
quantity = 0# inventory_component.gd
class_name InventoryComponent
extends Node
## 背包容量
@export var capacity: int = 20
## 信号:物品添加成功
signal item_added(slot_index: int, item: InventoryItem, quantity: int)
## 信号:物品移除成功
signal item_removed(slot_index: int, item: InventoryItem, quantity: int)
## 信号:背包满了
signal inventory_full(item: InventoryItem)
## 信号:格子数据变化
signal slot_changed(slot_index: int)
## 背包格子数组
var slots: Array[InventorySlot] = []
func _ready() -> void:
_initialize_slots()
func _initialize_slots() -> void:
for i in capacity:
var slot := InventorySlot.new()
slot.index = i
slots.append(slot)
## 添加物品到背包
func add_item(item: InventoryItem, quantity: int = 1) -> bool:
# 优先堆叠到已有物品
var target_slot := _find_stackable_slot(item)
if target_slot != null:
var old_qty := target_slot.quantity
target_slot.add(item, quantity)
slot_changed.emit(target_slot.index)
item_added.emit(target_slot.index, item, target_slot.quantity - old_qty)
return true
# 找空格子
var empty_slot := _find_empty_slot()
if empty_slot != null:
empty_slot.add(item, quantity)
slot_changed.emit(empty_slot.index)
item_added.emit(empty_slot.index, item, quantity)
return true
# 背包满了
inventory_full.emit(item)
return false
## 移除物品
func remove_item(slot_index: int, quantity: int = 1) -> bool:
if slot_index < 0 or slot_index >= slots.size():
return false
var slot: InventorySlot = slots[slot_index]
if slot.is_empty:
return false
var item := slot.item
if slot.remove(quantity):
slot_changed.emit(slot_index)
item_removed.emit(slot_index, item, quantity)
return true
return false
## 交换两个格子的物品
func swap_slots(index1: int, index2: int) -> void:
if index1 < 0 or index1 >= slots.size() or index2 < 0 or index2 >= slots.size():
return
var temp_item := slots[index1].item
var temp_qty := slots[index1].quantity
slots[index1].item = slots[index2].item
slots[index1].quantity = slots[index2].quantity
slots[index2].item = temp_item
slots[index2].quantity = temp_qty
slot_changed.emit(index1)
slot_changed.emit(index2)
## 移动物品到另一个背包
func move_to(target: InventoryComponent, slot_index: int, quantity: int = -1) -> bool:
if slot_index < 0 or slot_index >= slots.size():
return false
var slot: InventorySlot = slots[slot_index]
if slot.is_empty:
return false
var transfer_qty := quantity if quantity > 0 else slot.quantity
if target.add_item(slot.item, transfer_qty):
remove_item(slot_index, transfer_qty)
return true
return false
## 查找可堆叠的格子
func _find_stackable_slot(item: InventoryItem) -> InventorySlot:
if not item.can_stack:
return null
for slot in slots:
if not slot.is_empty and slot.item.id == item.id:
if slot.quantity + 1 <= slot.item.max_stack:
return slot
return null
## 查找空格子
func _find_empty_slot() -> InventorySlot:
for slot in slots:
if slot.is_empty:
return slot
return null
## 获取指定物品的数量
func get_item_count(item_id: StringName) -> int:
var total := 0
for slot in slots:
if not slot.is_empty and slot.item.id == item_id:
total += slot.quantity
return total
## 清空背包
func clear() -> void:
for slot in slots:
slot.clear()
slot_changed.emit(slot.index)
## 保存背包数据
func get_save_data() -> Dictionary:
var data := []
for slot in slots:
if not slot.is_empty:
data.append({Godot 4.x .tscn 场景文件纯文本格式规范与 AI 操作指南 — 文件结构、资源引用、 Transform 变换、属性值格式、信号连接,以及 AI 直接读写 .tscn 文件的操作方法。
Godot 4 Buff系统完整实现,支持状态效果管理、持续时间、循环效果、层叠机制等。使用Resource+Component架构。