Files
CINC/items/Item.js
T

204 lines
5.5 KiB
JavaScript

// 物品类:表示游戏中的单个物品实体
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;
// 创建一个简单的图形纹理作为物品精灵
if (!scene.textures.exists(id)) {
const canvas = document.createElement('canvas');
canvas.width = sprite.width;
canvas.height = sprite.height;
const ctx = canvas.getContext('2d');
// 设置物品颜色
ctx.fillStyle = `#${sprite.color.toString(16).padStart(6, '0')}`;
ctx.fillRect(0, 0, sprite.width, sprite.height);
// 添加物品边框
ctx.strokeStyle = '#000000';
ctx.lineWidth = 1;
ctx.strokeRect(0, 0, sprite.width, sprite.height);
// 添加物品ID的第一个字符作为图标
ctx.fillStyle = '#ffffff';
ctx.font = `${sprite.width * 0.8}px Arial`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(id.charAt(0).toUpperCase(), sprite.width / 2, sprite.height / 2);
// 将canvas添加为纹理
scene.textures.addCanvas(id, canvas);
}
// 调用父类构造函数创建精灵
super(scene, x, y, id);
// 设置物品属性
this.id = id;
this.name = name;
this.description = description;
this.type = type;
this.weight = weight;
this.volume = volume;
this.value = value;
this.properties = properties;
// 添加到场景中
scene.add.existing(this);
scene.physics.add.existing(this, true); // 静态物体,不响应物理碰撞
// 设置物品的碰撞体
this.setSize(sprite.width, sprite.height);
// 设置物品的交互性
this.setInteractive({
useHandCursor: true
});
// 绑定点击事件
this.on('pointerdown', () => {
// 添加视觉反馈:缩放物品
this.scene.tweens.add({
targets: this,
scale: { from: 1, to: 1.2, duration: 100, yoyo: true, repeat: 1 }
});
this.onItemClicked();
});
}
/**
* 检查单位是否在交互距离内
* @param {Object} unit - 要检查的单位
* @returns {boolean} - 是否在交互距离内
*/
isInInteractionRange(unit) {
const maxDistance = 50; // 最大交互距离
const dx = this.x - unit.x;
const dy = this.y - unit.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance <= maxDistance;
}
/**
* 物品被点击时的处理函数
*/
onItemClicked() {
// 触发物品点击事件
this.scene.events.emit('item-clicked', this);
}
/**
* 拾取物品
* @param {Object} user - 拾取物品的单位
*/
pickup(user) {
console.log(`${user.name} 拾取了 ${this.name}`);
// 这里可以添加物品到背包的逻辑
// 暂时简单移除物品
this.destroy();
}
/**
* 使用物品
* @param {Object} user - 使用物品的单位
*/
use(user) {
// 根据物品类型和属性执行不同的使用逻辑
switch (this.type) {
case 'food':
this.useFood(user);
break;
case 'medical':
this.useMedical(user);
break;
case 'tool':
this.useTool(user);
break;
case 'weapon':
this.equipWeapon(user);
break;
default:
console.log(`无法使用物品:${this.name}`);
}
}
/**
* 使用食物物品
* @param {Object} user - 使用物品的单位
*/
useFood(user) {
if (this.properties.drinkable) {
// 处理饮水逻辑
console.log(`${user.name} 喝了 ${this.name}`);
if (user.hydration !== undefined) {
user.hydration = Math.min(100, user.hydration + this.properties.hydration);
}
} else if (this.properties.edible) {
// 处理进食逻辑
console.log(`${user.name} 吃了 ${this.name}`);
if (user.nutrition !== undefined) {
user.nutrition = Math.min(100, user.nutrition + this.properties.nutrition);
}
if (user.hydration !== undefined) {
user.hydration = Math.min(100, user.hydration + (this.properties.hydration || 0));
}
}
// 使用后移除物品
this.destroy();
}
/**
* 使用医疗物品
* @param {Object} user - 使用物品的单位
*/
useMedical(user) {
if (this.properties.heal_amount && user.health !== undefined) {
console.log(`${user.name} 使用了 ${this.name}`);
user.health = Math.min(100, user.health + this.properties.heal_amount);
// 使用后移除物品
this.destroy();
}
}
/**
* 使用工具物品
* @param {Object} user - 使用物品的单位
*/
useTool(user) {
console.log(`${user.name} 使用了工具:${this.name}`);
// 工具使用逻辑可以根据具体工具类型扩展
}
/**
* 装备武器
* @param {Object} user - 装备武器的单位
*/
equipWeapon(user) {
console.log(`${user.name} 装备了武器:${this.name}`);
// 装备武器逻辑
if (user.equipWeapon) {
user.equipWeapon(this);
}
// 装备后可以选择保留物品或移除,这里选择保留
}
/**
* 获取物品的详细信息
* @returns {Object} 物品信息对象
*/
getInfo() {
return {
id: this.id,
name: this.name,
description: this.description,
type: this.type,
weight: this.weight,
volume: this.volume,
value: this.value,
properties: this.properties
};
}
}