mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 00:31:29 +08:00
feat(inventory) 添加背包物品移动、拾取到背包、丢弃背包物品功能
This commit is contained in:
1 parent
ae26209a24
commit
35e10a2abc
6 files changed
+408
-115
No files matched your search
+108
-6
@@ -39,6 +39,12 @@ export class InputSystem {
|
||||
// 右移键:同时支持D键和方向右键
|
||||
right: [kb.addKey(Phaser.Input.Keyboard.KeyCodes.D), kb.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT)]
|
||||
};
|
||||
|
||||
// 添加F键快捷拾取功能
|
||||
this.fKey = kb.addKey(Phaser.Input.Keyboard.KeyCodes.F);
|
||||
this.fKey.on('down', () => {
|
||||
this.handleQuickPickup();
|
||||
});
|
||||
|
||||
// 禁用浏览器右键菜单,避免影响游戏操作
|
||||
scene.input.mouse.disableContextMenu();
|
||||
@@ -85,8 +91,8 @@ export class InputSystem {
|
||||
// 处理鼠标/触摸按下事件
|
||||
// @param {Object} pointer - 指针事件对象,包含鼠标/触摸的位置等信息
|
||||
onPointerDown(pointer) {
|
||||
// 检查鼠标是否在小地图、摇杆或侧边栏上,如果是则不处理
|
||||
if (this.isMouseOnMiniMap(pointer) || this.isMouseOnJoystick(pointer) || this.isMouseOnSidebar(pointer)) {
|
||||
// 检查鼠标是否在UI元素上,如果是则不处理
|
||||
if (this.isMouseOnUI(pointer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -157,8 +163,8 @@ export class InputSystem {
|
||||
// 处理鼠标/触摸移动事件
|
||||
// @param {Object} pointer - 指针事件对象
|
||||
onPointerMove(pointer) {
|
||||
// 检查鼠标是否在小地图、摇杆或侧边栏上,如果是则不处理
|
||||
if (this.isMouseOnMiniMap(pointer) || this.isMouseOnJoystick(pointer) || this.isMouseOnSidebar(pointer)) {
|
||||
// 检查鼠标是否在UI元素上,如果是则不处理
|
||||
if (this.isMouseOnUI(pointer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -193,8 +199,8 @@ export class InputSystem {
|
||||
// 处理鼠标/触摸释放事件
|
||||
// @param {Object} pointer - 指针事件对象
|
||||
onPointerUp(pointer) {
|
||||
// 检查鼠标是否在小地图、摇杆或侧边栏上,如果是则不处理
|
||||
if (this.isMouseOnMiniMap(pointer) || this.isMouseOnJoystick(pointer) || this.isMouseOnSidebar(pointer)) {
|
||||
// 检查鼠标是否在UI元素上,如果是则不处理
|
||||
if (this.isMouseOnUI(pointer)) {
|
||||
// 重置框选起始点和拖动状态
|
||||
this.selectionStart = null;
|
||||
this.dragStart = null;
|
||||
@@ -278,6 +284,18 @@ export class InputSystem {
|
||||
mouseY >= mapTop && mouseY <= mapBottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标是否在UI元素上
|
||||
* @param {Phaser.Input.Pointer} pointer - 指针事件对象
|
||||
* @returns {boolean} - 如果鼠标在UI元素上返回true,否则返回false
|
||||
*/
|
||||
isMouseOnUI(pointer) {
|
||||
return this.isMouseOnMiniMap(pointer) ||
|
||||
this.isMouseOnJoystick(pointer) ||
|
||||
this.isMouseOnSidebar(pointer) ||
|
||||
this.isMouseOnInventoryUI(pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标是否在虚拟摇杆上
|
||||
* @param {Phaser.Input.Pointer} pointer - 指针事件对象
|
||||
@@ -349,4 +367,88 @@ export class InputSystem {
|
||||
return mouseX >= bounds.x && mouseX <= bounds.right &&
|
||||
mouseY >= bounds.y && mouseY <= bounds.bottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标是否在背包UI上
|
||||
* @param {Phaser.Input.Pointer} pointer - 指针事件对象
|
||||
* @returns {boolean} - 如果鼠标在背包UI上返回true,否则返回false
|
||||
*/
|
||||
isMouseOnInventoryUI(pointer) {
|
||||
// 检查场景中是否有背包UI
|
||||
if (!this.scene.inventoryUI) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取背包UI实例
|
||||
const inventoryUI = this.scene.inventoryUI;
|
||||
// 获取鼠标在屏幕上的坐标
|
||||
const mouseX = pointer.x;
|
||||
const mouseY = pointer.y;
|
||||
|
||||
// 获取背包UI的边界
|
||||
const container = inventoryUI.container;
|
||||
if (!container) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const containerBounds = container.getBounds();
|
||||
|
||||
// 检查鼠标坐标是否在背包UI边界内
|
||||
return mouseX >= containerBounds.x && mouseX <= containerBounds.right &&
|
||||
mouseY >= containerBounds.y && mouseY <= containerBounds.bottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理F键快捷拾取功能
|
||||
*/
|
||||
handleQuickPickup() {
|
||||
console.log('handleQuickPickup: 尝试快捷拾取物品');
|
||||
|
||||
// 确保场景中有玩家和物品管理器
|
||||
if (!this.scene.player || !this.scene.itemManager) {
|
||||
console.log('handleQuickPickup: 缺少玩家或物品管理器');
|
||||
return;
|
||||
}
|
||||
|
||||
const player = this.scene.player;
|
||||
const itemManager = this.scene.itemManager;
|
||||
|
||||
// 获取所有活动物品
|
||||
const activeItems = itemManager.getActiveItems ? itemManager.getActiveItems() : [];
|
||||
if (activeItems.length === 0) {
|
||||
console.log('handleQuickPickup: 没有可拾取的物品');
|
||||
return;
|
||||
}
|
||||
|
||||
const pickupDistance = 100; // 最大拾取距离
|
||||
let closestItem = null;
|
||||
let minDistance = pickupDistance;
|
||||
|
||||
// 找到距离玩家最近且在拾取范围内的物品
|
||||
activeItems.forEach(item => {
|
||||
// 计算物品与玩家的距离
|
||||
const dx = item.x - player.x;
|
||||
const dy = item.y - player.y;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
// 如果物品在拾取范围内且距离更近,更新最近物品
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
closestItem = item;
|
||||
}
|
||||
});
|
||||
|
||||
// 如果找到最近物品,尝试拾取
|
||||
if (closestItem) {
|
||||
console.log(`handleQuickPickup: 拾取距离最近的物品: ${closestItem.name},距离: ${Math.round(minDistance)}`);
|
||||
closestItem.pickup(player);
|
||||
|
||||
// 更新背包UI,确保物品立即显示在背包中
|
||||
if (this.scene.inventoryUI) {
|
||||
this.scene.inventoryUI.updateUI();
|
||||
}
|
||||
} else {
|
||||
console.log('handleQuickPickup: 没有在拾取范围内的物品');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user