feat(ItemList) 添加周围物品列表功能

This commit is contained in:
chicrobot committed 2026-01-06 01:13:17 +08:00
1 parent 215e04384c
commit 0ed8c2f6ae
7 files changed
+1349 -14

No files matched your search

+25 -5
View File
@@ -65,7 +65,12 @@ export default class Item extends Phaser.GameObjects.Sprite {
});
// 绑定点击事件
this.on('pointerdown', () => {
this.on('pointerdown', (pointer) => {
// 检查鼠标是否在UI元素上,如果是则不处理
if (this.scene.inputSystem && this.scene.inputSystem.isMouseOnUI && this.scene.inputSystem.isMouseOnUI(pointer)) {
return;
}
// 添加视觉反馈:缩放物品
this.scene.tweens.add({
targets: this,
@@ -101,6 +106,12 @@ export default class Item extends Phaser.GameObjects.Sprite {
* @param {Object} user - 拾取物品的单位
*/
pickup(user) {
// 检查是否在交互范围内
if (!this.isInInteractionRange(user)) {
console.log('距离太远,无法拾取物品');
return;
}
console.log(`${user.name} 拾取了 ${this.name}`);
// 检查用户是否有背包
@@ -109,13 +120,22 @@ export default class Item extends Phaser.GameObjects.Sprite {
const success = user.inventory.addItem(this);
if (success) {
this.inInventory = true;
this.visible = false; // 在场景中隐藏物品
this.scene.events.emit('item-picked-up', this, user);
// 保存场景引用,避免在destroy后访问undefined
const scene = this.scene;
// 如果场景中有背包UI,更新UI
if (this.scene.inventoryUI) {
this.scene.inventoryUI.updateUI();
if (scene && scene.inventoryUI) {
scene.inventoryUI.updateUI();
}
// 发射物品拾取事件
if (scene && scene.events) {
scene.events.emit('item-picked-up', this, user);
}
// 成功拾取后从地图上删除物品,避免潜在bug
this.destroy();
} else {
console.log('背包空间不足');
}