mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 23:31:29 +08:00
feat(inventory) 添加网格背包系统
This commit is contained in:
1 parent
b9a5180293
commit
ae26209a24
8 files changed
+1857
-43
No files matched your search
+51
-5
@@ -3,7 +3,7 @@
|
||||
export default class Item extends Phaser.GameObjects.Sprite {
|
||||
constructor(scene, x, y, itemData) {
|
||||
// 从itemData中提取精灵属性
|
||||
const { id, name, description, type, weight, volume, value, sprite, properties } = itemData;
|
||||
const { id, name, description, type, weight, volume, value, gridSize, sprite, properties } = itemData;
|
||||
|
||||
// 创建一个简单的图形纹理作为物品精灵
|
||||
if (!scene.textures.exists(id)) {
|
||||
@@ -36,15 +36,22 @@ export default class Item extends Phaser.GameObjects.Sprite {
|
||||
super(scene, x, y, id);
|
||||
|
||||
// 设置物品属性
|
||||
this.id = id;
|
||||
this.itemTypeId = id; // 物品类型ID,用于识别物品类型
|
||||
this.id = `${id}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; // 生成唯一ID
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.type = type;
|
||||
this.weight = weight;
|
||||
this.volume = volume;
|
||||
this.value = value;
|
||||
this.gridSize = gridSize || { width: 1, height: 1 }; // 默认1x1网格
|
||||
this.properties = properties;
|
||||
|
||||
// 背包相关属性
|
||||
this.position = null; // 在背包中的位置
|
||||
this.positionInInventory = null; // 在背包中的位置(保持向后兼容)
|
||||
this.inInventory = false;
|
||||
|
||||
// 添加到场景中
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this, true); // 静态物体,不响应物理碰撞
|
||||
@@ -95,9 +102,47 @@ export default class Item extends Phaser.GameObjects.Sprite {
|
||||
*/
|
||||
pickup(user) {
|
||||
console.log(`${user.name} 拾取了 ${this.name}`);
|
||||
// 这里可以添加物品到背包的逻辑
|
||||
// 暂时简单移除物品
|
||||
this.destroy();
|
||||
|
||||
// 检查用户是否有背包
|
||||
if (user.inventory) {
|
||||
// 尝试将物品添加到背包
|
||||
const success = user.inventory.addItem(this);
|
||||
if (success) {
|
||||
this.inInventory = true;
|
||||
this.visible = false; // 在场景中隐藏物品
|
||||
this.scene.events.emit('item-picked-up', this, user);
|
||||
} else {
|
||||
console.log('背包空间不足');
|
||||
}
|
||||
} else {
|
||||
// 如果没有背包系统,简单移除物品
|
||||
this.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从背包中使用物品
|
||||
* @param {Object} user - 使用物品的单位
|
||||
*/
|
||||
useFromInventory(user) {
|
||||
// 使用物品
|
||||
this.use(user);
|
||||
|
||||
// 从背包中移除物品
|
||||
if (user.inventory) {
|
||||
user.inventory.removeItem(this.id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建物品实例(静态方法)
|
||||
* @param {Object} itemData - 物品数据
|
||||
* @returns {Item} 物品实例
|
||||
*/
|
||||
static createFromData(itemData) {
|
||||
// 这个方法需要在具体场景中实现
|
||||
// 因为需要场景上下文来创建精灵
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,6 +243,7 @@ export default class Item extends Phaser.GameObjects.Sprite {
|
||||
weight: this.weight,
|
||||
volume: this.volume,
|
||||
value: this.value,
|
||||
gridSize: this.gridSize,
|
||||
properties: this.properties
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user