| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- /**
- * 增强防围困机制测试脚本
- * 专门测试长距离振荡检测和干预功能
- *
- * 使用方法:
- * 1. 确保BallController.ts中testMode = true
- * 2. 在浏览器控制台运行此脚本
- * 3. 观察控制台输出和小球行为
- */
- // 测试配置
- const TEST_CONFIG = {
- // 测试持续时间(秒)
- testDuration: 60,
-
- // 监控间隔(毫秒)
- monitorInterval: 1000,
-
- // 预期的振荡检测触发次数
- expectedOscillationTriggers: 2,
-
- // 测试场景
- scenarios: [
- {
- name: "水平振荡测试",
- description: "小球在左右墙体间来回弹跳",
- expectedAxis: "horizontal"
- },
- {
- name: "垂直振荡测试",
- description: "小球在上下墙体间来回弹跳",
- expectedAxis: "vertical"
- },
- {
- name: "长距离方块振荡测试",
- description: "小球在距离较远的方块间来回弹跳",
- expectedAxis: "horizontal" // 或 "vertical"
- }
- ]
- };
- // 测试状态
- let testState = {
- startTime: 0,
- oscillationTriggers: 0,
- directionChanges: 0,
- lastPosition: null,
- positionHistory: [],
- testResults: []
- };
- /**
- * 开始增强防围困机制测试
- */
- function startEnhancedAntiTrapTest() {
- console.log("\n=== 增强防围困机制测试开始 ===");
- console.log("测试目标:验证长距离振荡检测和干预功能");
- console.log("测试时长:" + TEST_CONFIG.testDuration + "秒");
- console.log("\n请确保:");
- console.log("1. BallController中testMode = true");
- console.log("2. 小球已启动并开始运动");
- console.log("3. 场景中有适当的墙体或方块布局\n");
-
- // 重置测试状态
- testState.startTime = Date.now();
- testState.oscillationTriggers = 0;
- testState.directionChanges = 0;
- testState.lastPosition = null;
- testState.positionHistory = [];
- testState.testResults = [];
-
- // 开始监控
- startMonitoring();
-
- // 设置测试结束定时器
- setTimeout(() => {
- endTest();
- }, TEST_CONFIG.testDuration * 1000);
- }
- /**
- * 开始监控小球状态
- */
- function startMonitoring() {
- const monitorInterval = setInterval(() => {
- if (Date.now() - testState.startTime >= TEST_CONFIG.testDuration * 1000) {
- clearInterval(monitorInterval);
- return;
- }
-
- monitorBallState();
- }, TEST_CONFIG.monitorInterval);
-
- // 监听控制台输出(模拟)
- interceptConsoleLog();
- }
- /**
- * 监控小球状态
- */
- function monitorBallState() {
- // 这里需要根据实际的Cocos Creator API来获取小球状态
- // 以下是示例代码,需要根据实际情况调整
-
- try {
- // 获取小球节点(需要根据实际场景结构调整)
- const gameArea = cc.find('Canvas/GameLevelUI/GameArea');
- if (!gameArea) {
- console.log("[测试] 未找到GameArea节点");
- return;
- }
-
- const balls = gameArea.children.filter(child =>
- child.name === 'Ball' || child.name === 'AdditionalBall'
- );
-
- if (balls.length === 0) {
- console.log("[测试] 未找到小球节点");
- return;
- }
-
- const ball = balls[0];
- const currentPos = ball.getWorldPosition();
-
- // 记录位置历史
- testState.positionHistory.push({
- x: currentPos.x,
- y: currentPos.y,
- time: Date.now()
- });
-
- // 保持历史记录在合理范围内
- if (testState.positionHistory.length > 50) {
- testState.positionHistory.shift();
- }
-
- // 检测方向改变
- if (testState.lastPosition) {
- const deltaX = currentPos.x - testState.lastPosition.x;
- const deltaY = currentPos.y - testState.lastPosition.y;
-
- // 简单的方向改变检测
- if (Math.abs(deltaX) > 10 || Math.abs(deltaY) > 10) {
- testState.directionChanges++;
- }
- }
-
- testState.lastPosition = { x: currentPos.x, y: currentPos.y };
-
- // 分析运动模式
- analyzeMovementPattern();
-
- } catch (error) {
- console.log("[测试] 监控过程中出现错误:", error.message);
- }
- }
- /**
- * 分析运动模式
- */
- function analyzeMovementPattern() {
- if (testState.positionHistory.length < 10) {
- return;
- }
-
- const recent = testState.positionHistory.slice(-10);
- let minX = recent[0].x, maxX = recent[0].x;
- let minY = recent[0].y, maxY = recent[0].y;
-
- for (const pos of recent) {
- minX = Math.min(minX, pos.x);
- maxX = Math.max(maxX, pos.x);
- minY = Math.min(minY, pos.y);
- maxY = Math.max(maxY, pos.y);
- }
-
- const xRange = maxX - minX;
- const yRange = maxY - minY;
-
- // 检测振荡模式
- let detectedPattern = 'none';
- if (xRange > 100 && yRange < 50) {
- detectedPattern = 'horizontal';
- } else if (yRange > 100 && xRange < 50) {
- detectedPattern = 'vertical';
- }
-
- if (detectedPattern !== 'none') {
- console.log(`[测试] 检测到${detectedPattern === 'horizontal' ? '水平' : '垂直'}振荡模式`);
- console.log(`[测试] X范围: ${xRange.toFixed(1)}, Y范围: ${yRange.toFixed(1)}`);
- }
- }
- /**
- * 拦截控制台输出以统计防围困触发次数
- */
- function interceptConsoleLog() {
- const originalLog = console.log;
- console.log = function(...args) {
- const message = args.join(' ');
-
- // 检测振荡检测触发
- if (message.includes('[防围困] 检测到小球') && message.includes('振荡')) {
- testState.oscillationTriggers++;
- testState.testResults.push({
- time: Date.now() - testState.startTime,
- type: 'oscillation_detected',
- message: message
- });
- }
-
- // 检测增强防围困触发
- if (message.includes('[防围困] 触发增强防围困机制')) {
- testState.testResults.push({
- time: Date.now() - testState.startTime,
- type: 'enhanced_anti_trap_triggered',
- message: message
- });
- }
-
- // 检测冲量应用
- if (message.includes('[防围困]') && (message.includes('冲量') || message.includes('处理'))) {
- testState.testResults.push({
- time: Date.now() - testState.startTime,
- type: 'impulse_applied',
- message: message
- });
- }
-
- // 调用原始console.log
- originalLog.apply(console, args);
- };
- }
- /**
- * 结束测试并生成报告
- */
- function endTest() {
- console.log("\n=== 增强防围困机制测试结束 ===");
-
- const testDuration = (Date.now() - testState.startTime) / 1000;
-
- console.log("\n📊 测试统计:");
- console.log(`⏱️ 测试时长: ${testDuration.toFixed(1)}秒`);
- console.log(`🔄 方向改变次数: ${testState.directionChanges}`);
- console.log(`🎯 振荡检测触发次数: ${testState.oscillationTriggers}`);
- console.log(`📝 总事件记录: ${testState.testResults.length}`);
-
- console.log("\n📋 详细事件记录:");
- testState.testResults.forEach((result, index) => {
- const timeStr = (result.time / 1000).toFixed(1) + 's';
- console.log(`${index + 1}. [${timeStr}] ${result.type}: ${result.message}`);
- });
-
- // 生成测试结论
- generateTestConclusion();
- }
- /**
- * 生成测试结论
- */
- function generateTestConclusion() {
- console.log("\n🎯 测试结论:");
-
- const oscillationDetected = testState.oscillationTriggers > 0;
- const enhancedAntiTrapTriggered = testState.testResults.some(r => r.type === 'enhanced_anti_trap_triggered');
- const impulseApplied = testState.testResults.some(r => r.type === 'impulse_applied');
-
- if (oscillationDetected) {
- console.log("✅ 振荡检测功能正常工作");
- } else {
- console.log("❌ 未检测到振荡模式,可能需要调整参数或测试场景");
- }
-
- if (enhancedAntiTrapTriggered) {
- console.log("✅ 增强防围困机制成功触发");
- } else {
- console.log("❌ 增强防围困机制未触发");
- }
-
- if (impulseApplied) {
- console.log("✅ 冲量干预成功应用");
- } else {
- console.log("❌ 未检测到冲量干预");
- }
-
- // 总体评估
- const successCount = [oscillationDetected, enhancedAntiTrapTriggered, impulseApplied].filter(Boolean).length;
- const totalTests = 3;
- const successRate = (successCount / totalTests * 100).toFixed(1);
-
- console.log(`\n📈 总体成功率: ${successRate}% (${successCount}/${totalTests})`);
-
- if (successRate >= 66.7) {
- console.log("🎉 增强防围困机制测试通过!");
- } else {
- console.log("⚠️ 增强防围困机制需要进一步调优");
-
- console.log("\n🔧 建议调整:");
- if (!oscillationDetected) {
- console.log("- 降低directionChangeThreshold(当前4次,建议2-3次)");
- console.log("- 减小oscillationDistanceThreshold(当前100,建议50-80)");
- }
- if (!enhancedAntiTrapTriggered) {
- console.log("- 检查小球是否真的在振荡");
- console.log("- 确认testMode已启用");
- }
- if (!impulseApplied) {
- console.log("- 检查冲量计算逻辑");
- console.log("- 确认RigidBody2D组件正常工作");
- }
- }
- }
- /**
- * 快速测试函数(简化版)
- */
- function quickEnhancedAntiTrapTest() {
- console.log("🚀 快速增强防围困测试(30秒)");
- TEST_CONFIG.testDuration = 30;
- startEnhancedAntiTrapTest();
- }
- // 导出测试函数
- if (typeof window !== 'undefined') {
- window.startEnhancedAntiTrapTest = startEnhancedAntiTrapTest;
- window.quickEnhancedAntiTrapTest = quickEnhancedAntiTrapTest;
- }
- console.log("\n🔧 增强防围困机制测试脚本已加载");
- console.log("使用方法:");
- console.log("- startEnhancedAntiTrapTest() // 完整测试(60秒)");
- console.log("- quickEnhancedAntiTrapTest() // 快速测试(30秒)");
- console.log("\n⚠️ 注意:确保BallController中testMode = true");
|