Files
CINC/items/ItemManager.js

335 lines
8.3 KiB
JavaScript

// 物品管理器:负责加载、管理和创建物品
// 静态导入Item类
import Item from './Item.js';
export class ItemManager {
constructor(scene) {
this.scene = scene;
this.itemTemplates = new Map(); // 存储物品模板
this.activeItems = new Set(); // 存储当前场景中的物品实例
this.isDataLoaded = false; // 标记物品数据是否已加载
this.loadCallbacks = []; // 存储数据加载完成后的回调函数
// 加载物品数据
this.loadItemData();
}
/**
* 注册数据加载完成回调
* @param {Function} callback - 回调函数
*/
onDataLoaded(callback) {
if (this.isDataLoaded) {
callback();
} else {
this.loadCallbacks.push(callback);
}
}
/**
* 加载物品数据
*/
async loadItemData() {
try {
// 从JSON文件加载物品数据
const response = await fetch('./data/items.json');
const data = await response.json();
// 存储物品模板
data.items.forEach(itemData => {
this.itemTemplates.set(itemData.id, itemData);
});
console.log(`成功加载 ${this.itemTemplates.size} 种物品模板`);
// 设置数据已加载标记
this.isDataLoaded = true;
// 调用所有数据加载完成回调
this.loadCallbacks.forEach(callback => callback());
this.loadCallbacks = [];
} catch (error) {
console.error('加载物品数据失败:', error);
// 加载失败时,使用默认物品数据
this.loadDefaultItemData();
}
}
/**
* 加载默认物品数据(当JSON文件加载失败时使用)
*/
loadDefaultItemData() {
const defaultItems = [
{
id: "water_bottle",
name: "瓶装水",
description: "一瓶干净的饮用水,可以解渴。",
type: "food",
weight: 1.0,
volume: 1.0,
value: 5,
sprite: {
width: 16,
height: 16,
color: 0x0000ff
},
properties: {
drinkable: true,
nutrition: 0,
hydration: 50
}
},
{
id: "canned_food",
name: "罐头食品",
description: "一罐保质期很长的食品,可以提供营养。",
type: "food",
weight: 1.5,
volume: 1.0,
value: 10,
sprite: {
width: 16,
height: 16,
color: 0xff0000
},
properties: {
edible: true,
nutrition: 100,
hydration: 20
}
},
{
id: "crowbar",
name: "撬棍",
description: "多功能工具,可以撬开门锁或作为武器。",
type: "tool",
weight: 3.0,
volume: 2.0,
value: 20,
sprite: {
width: 16,
height: 16,
color: 0x808080
},
properties: {
tool_quality: 2,
weapon_damage: 15
}
},
{
id: "first_aid_kit",
name: "急救包",
description: "包含基本医疗用品,可以治疗伤口。",
type: "medical",
weight: 1.0,
volume: 1.5,
value: 30,
sprite: {
width: 16,
height: 16,
color: 0xffffff
},
properties: {
heal_amount: 50,
usable: true
}
},
{
id: "flashlight",
name: "手电筒",
description: "可以在黑暗中提供照明。",
type: "tool",
weight: 0.5,
volume: 0.5,
value: 15,
sprite: {
width: 16,
height: 16,
color: 0xffff00
},
properties: {
light_range: 10,
battery_life: 100
}
},
{
id: "knife",
name: "小刀",
description: "一把锋利的小刀,可以用作工具或武器。",
type: "weapon",
weight: 0.8,
volume: 1.0,
value: 12,
sprite: {
width: 16,
height: 16,
color: 0x404040
},
properties: {
weapon_damage: 10,
tool_quality: 1
}
}
];
defaultItems.forEach(itemData => {
this.itemTemplates.set(itemData.id, itemData);
});
console.log(`使用默认物品数据,加载了 ${this.itemTemplates.size} 种物品模板`);
// 设置数据已加载标记
this.isDataLoaded = true;
// 调用所有数据加载完成回调
this.loadCallbacks.forEach(callback => callback());
this.loadCallbacks = [];
}
/**
* 根据物品ID创建物品实例
* @param {string} itemId - 物品ID
* @param {number} x - 物品位置X坐标
* @param {number} y - 物品位置Y坐标
* @returns {Item|null} - 创建的物品实例,或null如果物品ID不存在
*/
createItem(itemId, x, y) {
const itemData = this.itemTemplates.get(itemId);
if (!itemData) {
console.error(`未知物品ID: ${itemId}`);
return null;
}
// 使用静态导入的Item类创建物品
const item = new Item(this.scene, x, y, itemData);
// 将物品添加到活动物品集合中
this.activeItems.add(item);
// 监听物品销毁事件,从集合中移除
item.on('destroy', () => {
this.activeItems.delete(item);
});
return item;
}
/**
* 在随机位置创建随机物品
* @returns {Item|null} - 创建的物品实例,或null如果没有物品模板
*/
createRandomItem() {
if (this.itemTemplates.size === 0) {
return null;
}
// 随机选择一个物品模板
const itemIds = Array.from(this.itemTemplates.keys());
const randomItemId = itemIds[Math.floor(Math.random() * itemIds.length)];
// 随机选择一个位置
const worldBounds = this.scene.physics.world.bounds;
const x = Phaser.Math.Between(worldBounds.x, worldBounds.right);
const y = Phaser.Math.Between(worldBounds.y, worldBounds.bottom);
// 创建物品
return this.createItem(randomItemId, x, y);
}
/**
* 根据物品类型创建物品
* @param {string} type - 物品类型
* @param {number} x - 物品位置X坐标
* @param {number} y - 物品位置Y坐标
* @returns {Item|null} - 创建的物品实例,或null如果没有对应类型的物品
*/
createItemByType(type, x, y) {
// 筛选出指定类型的物品
const typeItems = Array.from(this.itemTemplates.values()).filter(item => item.type === type);
if (typeItems.length === 0) {
console.error(`没有找到类型为 ${type} 的物品`);
return null;
}
// 随机选择一个该类型的物品
const randomItemData = typeItems[Math.floor(Math.random() * typeItems.length)];
// 使用静态导入的Item类创建物品
const item = new Item(this.scene, x, y, randomItemData);
// 将物品添加到活动物品集合中
this.activeItems.add(item);
// 监听物品销毁事件,从集合中移除
item.on('destroy', () => {
this.activeItems.delete(item);
});
return item;
}
/**
* 获取物品模板
* @param {string} itemId - 物品ID
* @returns {Object|null} - 物品模板数据,或null如果物品ID不存在
*/
getItemTemplate(itemId) {
return this.itemTemplates.get(itemId) || null;
}
/**
* 获取当前场景中的所有活动物品
* @returns {Array} - 活动物品数组
*/
getActiveItems() {
return Array.from(this.activeItems);
}
/**
* 清理所有物品
*/
clearAllItems() {
// 销毁所有活动物品
this.activeItems.forEach(item => {
item.destroy();
});
// 清空活动物品集合
this.activeItems.clear();
}
/**
* 获取所有物品模板
* @returns {Map} - 所有物品模板的Map对象
*/
getAllItemTemplates() {
return this.itemTemplates;
}
/**
* 获取当前场景中的所有物品实例
* @returns {Set} - 当前场景中的所有物品实例的Set对象
*/
getActiveItems() {
return this.activeItems;
}
/**
* 清理所有物品实例
*/
clearAllItems() {
this.activeItems.forEach(item => {
item.destroy();
});
this.activeItems.clear();
}
/**
* 销毁物品管理器
*/
destroy() {
this.clearAllItems();
this.itemTemplates.clear();
}
}