mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 20:41:29 +08:00
feat(item) 优化移动及添加测试场景
This commit is contained in:
1 parent
2f7210f47e
commit
b9a5180293
15 files changed
+1428
-62
No files matched your search
@@ -18,6 +18,10 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
this.isSelected = false;
|
||||
this.selectedIndicator = null;
|
||||
this.createSelectedIndicator();
|
||||
|
||||
// 目标地点显示
|
||||
this.targetIndicator = null;
|
||||
this.pathGraphics = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +39,30 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
this.selectedIndicator.setScrollFactor(1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建目标地点指示器
|
||||
*/
|
||||
createTargetIndicator() {
|
||||
if (!this.targetIndicator) {
|
||||
// 创建目标地点指示器(红色圆点)
|
||||
this.targetIndicator = this.scene.add.circle(0, 0, 8, 0xff0000, 1);
|
||||
this.targetIndicator.setOrigin(0.5, 0.5);
|
||||
this.targetIndicator.setScrollFactor(1, 1);
|
||||
this.targetIndicator.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建寻路路径图形
|
||||
*/
|
||||
createPathGraphics() {
|
||||
if (!this.pathGraphics) {
|
||||
// 创建图形对象用于绘制路径
|
||||
this.pathGraphics = this.scene.add.graphics();
|
||||
this.pathGraphics.setScrollFactor(1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单位选中状态
|
||||
* @param {boolean} isSelected
|
||||
@@ -44,6 +72,41 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
if (this.selectedIndicator) {
|
||||
this.selectedIndicator.setVisible(isSelected);
|
||||
}
|
||||
|
||||
// 只有选中时才显示目标地点和路径
|
||||
this.updateTargetVisibility();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新目标地点和路径的可见性
|
||||
*/
|
||||
updateTargetVisibility() {
|
||||
if (this.targetIndicator) {
|
||||
this.targetIndicator.setVisible(this.isSelected && this.target !== null);
|
||||
}
|
||||
if (this.pathGraphics) {
|
||||
this.pathGraphics.clear();
|
||||
// 只有选中且有目标时才绘制路径
|
||||
if (this.isSelected && this.target !== null) {
|
||||
this.drawPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制寻路路径
|
||||
*/
|
||||
drawPath() {
|
||||
if (!this.pathGraphics || !this.target) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pathGraphics.clear();
|
||||
this.pathGraphics.lineStyle(2, 0x00ff00, 0.8);
|
||||
this.pathGraphics.beginPath();
|
||||
this.pathGraphics.moveTo(this.x, this.y);
|
||||
this.pathGraphics.lineTo(this.target.x, this.target.y);
|
||||
this.pathGraphics.stroke();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,6 +123,18 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
const clampedY = Math.max(worldBounds.y, Math.min(worldBounds.bottom, y));
|
||||
|
||||
this.target = { x: clampedX, y: clampedY };
|
||||
|
||||
// 创建目标指示器和路径图形(如果不存在)
|
||||
this.createTargetIndicator();
|
||||
this.createPathGraphics();
|
||||
|
||||
// 更新目标指示器位置
|
||||
if (this.targetIndicator) {
|
||||
this.targetIndicator.setPosition(this.target.x, this.target.y);
|
||||
}
|
||||
|
||||
// 更新目标可见性和路径绘制
|
||||
this.updateTargetVisibility();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,6 +143,14 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
clearTarget() {
|
||||
this.target = null;
|
||||
this.setVelocity(0, 0);
|
||||
|
||||
// 隐藏目标指示器和清除路径
|
||||
if (this.targetIndicator) {
|
||||
this.targetIndicator.setVisible(false);
|
||||
}
|
||||
if (this.pathGraphics) {
|
||||
this.pathGraphics.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,6 +182,11 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
|
||||
if (this.selectedIndicator && this.isSelected) {
|
||||
this.selectedIndicator.setPosition(this.x, this.y);
|
||||
}
|
||||
|
||||
// 更新路径绘制(单位位置变化时)
|
||||
if (this.isSelected && this.target !== null) {
|
||||
this.drawPath();
|
||||
}
|
||||
}
|
||||
|
||||
updateMovement() {
|
||||
|
||||
Reference in new issue
Block a user