mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 00:31:29 +08:00
refactor(SRC) 重构核心场景路径位置
This commit is contained in:
1 parent
942ba567f3
commit
cbbed1519e
18 files changed
+845
-473
No files matched your search
@@ -1,354 +0,0 @@
|
||||
// 移动逻辑测试场景
|
||||
// 本文件将当前移动逻辑拆分为不同的测试场景
|
||||
|
||||
// 测试场景1: 相对移动(键盘控制)
|
||||
// 测试内容:
|
||||
// 1. 键盘方向键输入检测
|
||||
// 2. 方向向量归一化
|
||||
// 3. 移动速度控制
|
||||
// 4. 停止移动逻辑
|
||||
function testRelativeMovement() {
|
||||
console.log("=== 测试场景1: 相对移动(键盘控制) ===");
|
||||
|
||||
// 模拟键盘输入
|
||||
const testCases = [
|
||||
{ dir: { x: 1, y: 0 }, description: "向右移动" },
|
||||
{ dir: { x: 0, y: 1 }, description: "向下移动" },
|
||||
{ dir: { x: -1, y: 0 }, description: "向左移动" },
|
||||
{ dir: { x: 0, y: -1 }, description: "向上移动" },
|
||||
{ dir: { x: 1, y: 1 }, description: "向右下对角线移动" },
|
||||
{ dir: { x: 0, y: 0 }, description: "停止移动" }
|
||||
];
|
||||
|
||||
testCases.forEach(testCase => {
|
||||
console.log(`测试: ${testCase.description}`);
|
||||
console.log(`输入方向: (${testCase.dir.x}, ${testCase.dir.y})`);
|
||||
|
||||
// 计算归一化向量
|
||||
const magnitude = Math.sqrt(testCase.dir.x * testCase.dir.x + testCase.dir.y * testCase.dir.y);
|
||||
let normalizedDir = { x: 0, y: 0 };
|
||||
if (magnitude > 0) {
|
||||
normalizedDir = {
|
||||
x: testCase.dir.x / magnitude,
|
||||
y: testCase.dir.y / magnitude
|
||||
};
|
||||
}
|
||||
|
||||
console.log(`归一化方向: (${normalizedDir.x.toFixed(2)}, ${normalizedDir.y.toFixed(2)})`);
|
||||
|
||||
// 计算速度向量
|
||||
const speed = 150;
|
||||
const velocity = {
|
||||
x: normalizedDir.x * speed,
|
||||
y: normalizedDir.y * speed
|
||||
};
|
||||
|
||||
console.log(`速度向量: (${velocity.x.toFixed(2)}, ${velocity.y.toFixed(2)})`);
|
||||
console.log("------------------------");
|
||||
});
|
||||
}
|
||||
|
||||
// 测试场景2: 目标点移动
|
||||
// 测试内容:
|
||||
// 1. 目标点设置
|
||||
// 2. 世界边界限制
|
||||
// 3. 移动路径计算
|
||||
// 4. 到达目标点检测
|
||||
function testMoveToPoint() {
|
||||
console.log("=== 测试场景2: 目标点移动 ===");
|
||||
|
||||
// 模拟世界边界
|
||||
const worldBounds = { x: 0, y: 0, width: 1000, height: 800 };
|
||||
|
||||
// 测试用例
|
||||
const testCases = [
|
||||
{
|
||||
unitPos: { x: 100, y: 100 },
|
||||
targetPos: { x: 500, y: 400 },
|
||||
description: "正常范围内移动"
|
||||
},
|
||||
{
|
||||
unitPos: { x: 100, y: 100 },
|
||||
targetPos: { x: 1200, y: 900 },
|
||||
description: "超出世界边界的目标点"
|
||||
},
|
||||
{
|
||||
unitPos: { x: 500, y: 400 },
|
||||
targetPos: { x: 505, y: 405 },
|
||||
description: "接近当前位置的目标点"
|
||||
}
|
||||
];
|
||||
|
||||
testCases.forEach(testCase => {
|
||||
console.log(`测试: ${testCase.description}`);
|
||||
console.log(`单位位置: (${testCase.unitPos.x}, ${testCase.unitPos.y})`);
|
||||
console.log(`目标位置: (${testCase.targetPos.x}, ${testCase.targetPos.y})`);
|
||||
|
||||
// 应用世界边界限制
|
||||
const clampedTarget = {
|
||||
x: Math.max(worldBounds.x, Math.min(worldBounds.width, testCase.targetPos.x)),
|
||||
y: Math.max(worldBounds.y, Math.min(worldBounds.height, testCase.targetPos.y))
|
||||
};
|
||||
|
||||
console.log(`限制后目标: (${clampedTarget.x}, ${clampedTarget.y})`);
|
||||
|
||||
// 计算距离和方向
|
||||
const dx = clampedTarget.x - testCase.unitPos.x;
|
||||
const dy = clampedTarget.y - testCase.unitPos.y;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
console.log(`距离: ${distance.toFixed(2)}`);
|
||||
|
||||
// 检查是否到达目标
|
||||
const arrived = distance < 5;
|
||||
console.log(`是否到达目标: ${arrived}`);
|
||||
|
||||
if (!arrived) {
|
||||
// 计算移动方向和速度
|
||||
const angle = Math.atan2(dy, dx);
|
||||
const speed = 150;
|
||||
const velocity = {
|
||||
x: Math.cos(angle) * speed,
|
||||
y: Math.sin(angle) * speed
|
||||
};
|
||||
console.log(`移动速度: (${velocity.x.toFixed(2)}, ${velocity.y.toFixed(2)})`);
|
||||
}
|
||||
|
||||
console.log("------------------------");
|
||||
});
|
||||
}
|
||||
|
||||
// 测试场景3: 单位选择与移动命令
|
||||
// 测试内容:
|
||||
// 1. 单个单位选择
|
||||
// 2. 多个单位选择
|
||||
// 3. 移动命令分发
|
||||
function testSelectionAndCommand() {
|
||||
console.log("=== 测试场景3: 单位选择与移动命令 ===");
|
||||
|
||||
// 模拟单位和选择状态
|
||||
const mockUnits = [
|
||||
{ id: 1, name: "单位1", moveRelative: () => {}, setTarget: () => {} },
|
||||
{ id: 2, name: "单位2", moveRelative: () => {}, setTarget: () => {} },
|
||||
{ id: 3, name: "单位3", moveRelative: () => {}, setTarget: () => {} }
|
||||
];
|
||||
|
||||
// 测试用例
|
||||
const testCases = [
|
||||
{
|
||||
selectedUnits: [],
|
||||
activeUnit: mockUnits[0],
|
||||
command: "move-relative",
|
||||
dir: { x: 1, y: 0 },
|
||||
description: "无选中单位,使用激活单位"
|
||||
},
|
||||
{
|
||||
selectedUnits: [mockUnits[0], mockUnits[1]],
|
||||
activeUnit: mockUnits[0],
|
||||
command: "move-to-point",
|
||||
target: { x: 500, y: 400 },
|
||||
description: "多个选中单位,执行目标点移动"
|
||||
},
|
||||
{
|
||||
selectedUnits: [mockUnits[2]],
|
||||
activeUnit: mockUnits[0],
|
||||
command: "move-relative",
|
||||
dir: { x: 0, y: -1 },
|
||||
description: "单个选中单位,执行相对移动"
|
||||
}
|
||||
];
|
||||
|
||||
testCases.forEach(testCase => {
|
||||
console.log(`测试: ${testCase.description}`);
|
||||
console.log(`激活单位: ${testCase.activeUnit.name}`);
|
||||
console.log(`选中单位: ${testCase.selectedUnits.map(u => u.name).join(", ")}`);
|
||||
|
||||
// 确定命令目标
|
||||
const targets = testCase.selectedUnits.length > 0 ? testCase.selectedUnits : [testCase.activeUnit];
|
||||
console.log(`命令目标: ${targets.map(u => u.name).join(", ")}`);
|
||||
|
||||
if (testCase.command === "move-relative") {
|
||||
console.log(`执行相对移动: (${testCase.dir.x}, ${testCase.dir.y})`);
|
||||
} else if (testCase.command === "move-to-point") {
|
||||
console.log(`执行目标点移动: (${testCase.target.x}, ${testCase.target.y})`);
|
||||
}
|
||||
|
||||
console.log("------------------------");
|
||||
});
|
||||
}
|
||||
|
||||
// 测试场景4: 虚拟摇杆控制
|
||||
// 测试内容:
|
||||
// 1. 摇杆激活检测
|
||||
// 2. 摇杆方向计算
|
||||
// 3. 归一化方向输出
|
||||
// 4. 停止移动处理
|
||||
function testVirtualJoystick() {
|
||||
console.log("=== 测试场景4: 虚拟摇杆控制 ===");
|
||||
|
||||
// 模拟摇杆配置
|
||||
const joystickConfig = { centerX: 100, centerY: 100, radius: 50 };
|
||||
|
||||
// 测试用例
|
||||
const testCases = [
|
||||
{
|
||||
pointerPos: { x: 120, y: 100 },
|
||||
description: "向右轻微拖动"
|
||||
},
|
||||
{
|
||||
pointerPos: { x: 160, y: 100 },
|
||||
description: "向右拖动超出摇杆范围"
|
||||
},
|
||||
{
|
||||
pointerPos: { x: 135, y: 135 },
|
||||
description: "向右下对角线拖动"
|
||||
},
|
||||
{
|
||||
pointerPos: { x: 100, y: 100 },
|
||||
description: "摇杆中心位置(停止)"
|
||||
}
|
||||
];
|
||||
|
||||
testCases.forEach(testCase => {
|
||||
console.log(`测试: ${testCase.description}`);
|
||||
console.log(`摇杆中心: (${joystickConfig.centerX}, ${joystickConfig.centerY})`);
|
||||
console.log(`指针位置: (${testCase.pointerPos.x}, ${testCase.pointerPos.y})`);
|
||||
|
||||
// 计算方向和距离
|
||||
const dx = testCase.pointerPos.x - joystickConfig.centerX;
|
||||
const dy = testCase.pointerPos.y - joystickConfig.centerY;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
console.log(`距离: ${distance.toFixed(2)}`);
|
||||
|
||||
// 限制在摇杆范围内
|
||||
let limitedDx = dx;
|
||||
let limitedDy = dy;
|
||||
if (distance > joystickConfig.radius) {
|
||||
const angle = Math.atan2(dy, dx);
|
||||
limitedDx = Math.cos(angle) * joystickConfig.radius;
|
||||
limitedDy = Math.sin(angle) * joystickConfig.radius;
|
||||
console.log(`限制后偏移: (${limitedDx.toFixed(2)}, ${limitedDy.toFixed(2)})`);
|
||||
}
|
||||
|
||||
// 计算归一化方向
|
||||
const dirX = limitedDx / joystickConfig.radius;
|
||||
const dirY = limitedDy / joystickConfig.radius;
|
||||
console.log(`归一化方向: (${dirX.toFixed(2)}, ${dirY.toFixed(2)})`);
|
||||
|
||||
console.log("------------------------");
|
||||
});
|
||||
}
|
||||
|
||||
// 测试场景5: 移动更新逻辑
|
||||
// 测试内容:
|
||||
// 1. 每帧移动更新
|
||||
// 2. 位置更新计算
|
||||
// 3. 目标到达检测
|
||||
function testMovementUpdate() {
|
||||
console.log("=== 测试场景5: 移动更新逻辑 ===");
|
||||
|
||||
// 模拟单位和时间增量
|
||||
const mockUnit = {
|
||||
x: 100,
|
||||
y: 100,
|
||||
target: { x: 500, y: 400 },
|
||||
setVelocity: (vx, vy) => { mockUnit.velocity = { x: vx, y: vy }; },
|
||||
setPosition: (x, y) => { mockUnit.x = x; mockUnit.y = y; }
|
||||
};
|
||||
|
||||
const speed = 150;
|
||||
const deltaTime = 1/60; // 60fps
|
||||
|
||||
console.log("初始状态:");
|
||||
console.log(`单位位置: (${mockUnit.x}, ${mockUnit.y})`);
|
||||
console.log(`目标位置: (${mockUnit.target.x}, ${mockUnit.target.y})`);
|
||||
|
||||
// 模拟几帧的移动更新
|
||||
for (let i = 0; i < 5; i++) {
|
||||
console.log(`\n第${i+1}帧更新:`);
|
||||
|
||||
const dx = mockUnit.target.x - mockUnit.x;
|
||||
const dy = mockUnit.target.y - mockUnit.y;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
console.log(`距离目标: ${distance.toFixed(2)}`);
|
||||
|
||||
if (distance < 5) {
|
||||
// 到达目标
|
||||
mockUnit.setPosition(mockUnit.target.x, mockUnit.target.y);
|
||||
mockUnit.setVelocity(0, 0);
|
||||
mockUnit.target = null;
|
||||
console.log("到达目标,停止移动");
|
||||
break;
|
||||
} else {
|
||||
// 继续移动
|
||||
const angle = Math.atan2(dy, dx);
|
||||
const velocity = {
|
||||
x: Math.cos(angle) * speed,
|
||||
y: Math.sin(angle) * speed
|
||||
};
|
||||
|
||||
mockUnit.setVelocity(velocity.x, velocity.y);
|
||||
|
||||
// 更新位置
|
||||
mockUnit.x += velocity.x * deltaTime;
|
||||
mockUnit.y += velocity.y * deltaTime;
|
||||
|
||||
console.log(`速度: (${velocity.x.toFixed(2)}, ${velocity.y.toFixed(2)})`);
|
||||
console.log(`新位置: (${mockUnit.x.toFixed(2)}, ${mockUnit.y.toFixed(2)})`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\n最终状态:");
|
||||
console.log(`单位位置: (${mockUnit.x}, ${mockUnit.y})`);
|
||||
console.log(`目标状态: ${mockUnit.target ? "有目标" : "无目标"}`);
|
||||
}
|
||||
|
||||
// 运行所有测试场景
|
||||
function runAllTests() {
|
||||
console.log("开始运行移动逻辑测试场景\n");
|
||||
|
||||
testRelativeMovement();
|
||||
console.log("\n" + "=".repeat(50) + "\n");
|
||||
|
||||
testMoveToPoint();
|
||||
console.log("\n" + "=".repeat(50) + "\n");
|
||||
|
||||
testSelectionAndCommand();
|
||||
console.log("\n" + "=".repeat(50) + "\n");
|
||||
|
||||
testVirtualJoystick();
|
||||
console.log("\n" + "=".repeat(50) + "\n");
|
||||
|
||||
testMovementUpdate();
|
||||
|
||||
console.log("\n" + "=".repeat(50));
|
||||
console.log("所有测试场景运行完成");
|
||||
}
|
||||
|
||||
// 导出测试函数
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = {
|
||||
testRelativeMovement,
|
||||
testMoveToPoint,
|
||||
testSelectionAndCommand,
|
||||
testVirtualJoystick,
|
||||
testMovementUpdate,
|
||||
runAllTests
|
||||
};
|
||||
// Node.js环境下自动运行测试
|
||||
runAllTests();
|
||||
} else {
|
||||
// 浏览器环境下自动运行测试
|
||||
window.movementTests = {
|
||||
runAllTests,
|
||||
testRelativeMovement,
|
||||
testMoveToPoint,
|
||||
testSelectionAndCommand,
|
||||
testVirtualJoystick,
|
||||
testMovementUpdate
|
||||
};
|
||||
// 浏览器环境下自动运行测试
|
||||
runAllTests();
|
||||
}
|
||||
Reference in new issue
Block a user