| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624 |
- import { _decorator, Component, Node, Vec2, Vec3, UITransform, Collider2D, Contact2DType, IPhysics2DContact, RigidBody2D, Prefab, instantiate, find, CircleCollider2D, JsonAsset } from 'cc';
- import { PhysicsManager } from '../Core/PhysicsManager';
- import { WeaponBullet, BulletInitData } from './WeaponBullet';
- import { WeaponConfig } from '../Core/ConfigManager';
- import EventBus, { GameEvents } from '../Core/EventBus';
- import { PersistentSkillManager } from '../FourUI/SkillSystem/PersistentSkillManager';
- import { BallAni } from '../Animations/BallAni';
- import { BallControllerConfig } from '../Core/ConfigManager';
- const { ccclass, property } = _decorator;
- @ccclass('BallController')
- export class BallController extends Component {
- // 球的预制体
- @property({
- type: Prefab,
- tooltip: '拖拽Ball预制体到这里'
- })
- public ballPrefab: Prefab = null;
- // 已放置方块容器节点
- @property({
- type: Node,
- tooltip: '拖拽PlacedBlocks节点到这里(Canvas/GameLevelUI/PlacedBlocks)'
- })
- public placedBlocksContainer: Node = null;
- // 球控制器配置文件
- @property({
- type: JsonAsset,
- tooltip: '拖拽ballController.json配置文件到这里'
- })
- public ballControllerConfig: JsonAsset = null;
- // 球的移动速度(从配置文件加载)
- public baseSpeed: number = 60;
- public currentSpeed: number = 60;
-
- // 反弹随机偏移最大角度(弧度)(从配置文件加载)
- public maxReflectionRandomness: number = 0.2;
-
- // 当前活动的球
- private activeBall: Node = null;
- // 球的方向向量
- private direction: Vec2 = new Vec2();
-
- // GameArea区域边界
- private gameBounds = {
- left: 0,
- right: 0,
- top: 0,
- bottom: 0
- };
- // 球的半径
- private radius: number = 0;
- // 配置数据
- private config: BallControllerConfig = null;
- // 是否已初始化
- private initialized: boolean = false;
-
- // 子弹预制体
- @property({
- type: Prefab,
- tooltip: '拖拽子弹预制体到这里'
- })
- public bulletPrefab: Prefab = null;
- // 小球是否已开始运动
- private ballStarted: boolean = false;
- // 小球暂停时记录的速度
- private pausedVelocity: Vec2 = new Vec2();
- // 标记是否处于暂停状态
- private isPaused: boolean = false;
- // 在类字段区添加
- private blockFireCooldown: Map<string, number> = new Map();
- private FIRE_COOLDOWN = 0.05;
- // 防围困机制配置(从配置文件加载)
- public antiTrapTimeWindow: number = 5.0;
- public antiTrapHitThreshold: number = 5;
- public deflectionAttemptThreshold: number = 3;
- public antiTrapDeflectionMultiplier: number = 3.0;
- // 防围困机制状态
- private ballHitHistory: Map<string, number[]> = new Map(); // 记录每个球的撞击时间历史
- private ballPhaseThrough: Map<string, number> = new Map(); // 记录每个球的穿透结束时间
- private ballDeflectionAttempts: Map<string, number> = new Map(); // 记录每个球的偏移尝试次数
- // 带尾部特效的子弹容器预制体
- @property({
- type: Prefab,
- tooltip: '拖拽带尾部特效的子弹容器预制体到这里(例如 PelletContainer)'
- })
- public bulletContainerPrefab: Prefab = null;
- start() {
- // 如果没有指定placedBlocksContainer,尝试找到它
- if (!this.placedBlocksContainer) {
- this.placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
- if (!this.placedBlocksContainer) {
- // 找不到PlacedBlocks节点,某些功能可能无法正常工作
- }
- }
-
- // 加载配置
- this.loadConfig();
-
- // 只进行初始设置,不创建小球
- this.calculateGameBounds();
- // 监听游戏事件
- this.setupEventListeners();
-
- // 监听球速技能变化并更新球速
- this.updateBallSpeed();
- }
- /**
- * 设置事件监听器
- */
- // 加载配置
- private loadConfig() {
- if (this.ballControllerConfig && this.ballControllerConfig.json) {
- this.config = this.ballControllerConfig.json as BallControllerConfig;
- this.applyConfig();
- console.log('[BallController] ✅ 配置文件通过装饰器加载成功:', this.config);
- } else {
- console.warn('[BallController] ⚠️ 配置文件未设置,使用默认值');
- // 使用默认配置
- this.config = {
- baseSpeed: 60,
- maxReflectionRandomness: 0.2,
- antiTrapTimeWindow: 5.0,
- antiTrapHitThreshold: 5,
- deflectionAttemptThreshold: 3,
- antiTrapDeflectionMultiplier: 3.0,
- FIRE_COOLDOWN: 0.05,
- ballRadius: 10,
- gravityScale: 0,
- linearDamping: 0,
- angularDamping: 0,
- colliderGroup: 1,
- colliderTag: 1,
- friction: 0,
- restitution: 1,
- safeDistance: 50,
- edgeOffset: 20,
- sensor: false,
- maxAttempts: 50
- };
- this.applyConfig();
- }
- }
- // 应用配置
- private applyConfig() {
- if (!this.config) return;
-
- this.baseSpeed = this.config.baseSpeed;
- this.currentSpeed = this.config.baseSpeed;
- this.maxReflectionRandomness = this.config.maxReflectionRandomness;
- this.antiTrapTimeWindow = this.config.antiTrapTimeWindow;
- this.antiTrapHitThreshold = this.config.antiTrapHitThreshold;
- this.deflectionAttemptThreshold = this.config.deflectionAttemptThreshold;
- this.antiTrapDeflectionMultiplier = this.config.antiTrapDeflectionMultiplier;
- this.FIRE_COOLDOWN = this.config.FIRE_COOLDOWN;
-
- console.log('BallController配置已应用:', this.config);
- }
- // 从配置中获取参数值的辅助方法
- private getConfigValue<T>(key: keyof BallControllerConfig, defaultValue: T): T {
- return this.config && this.config[key] !== undefined ? this.config[key] as T : defaultValue;
- }
- // 配置参数的getter方法
- private get ballRadius(): number {
- return this.getConfigValue('ballRadius', 10);
- }
- private get gravityScale(): number {
- return this.getConfigValue('gravityScale', 0);
- }
- private get linearDamping(): number {
- return this.getConfigValue('linearDamping', 0);
- }
- private get angularDamping(): number {
- return this.getConfigValue('angularDamping', 0);
- }
- private get colliderGroup(): number {
- return this.getConfigValue('colliderGroup', 2);
- }
- private get colliderTag(): number {
- return this.getConfigValue('colliderTag', 1);
- }
- private get friction(): number {
- return this.getConfigValue('friction', 0);
- }
- private get restitution(): number {
- return this.getConfigValue('restitution', 1);
- }
- private get safeDistance(): number {
- return this.getConfigValue('safeDistance', 50);
- }
- private get edgeOffset(): number {
- return this.getConfigValue('edgeOffset', 20);
- }
- private get sensor(): boolean {
- return this.getConfigValue('sensor', false);
- }
- private get maxAttempts(): number {
- return this.getConfigValue('maxAttempts', 50);
- }
- private setupEventListeners() {
- const eventBus = EventBus.getInstance();
-
- // 监听暂停事件
- eventBus.on(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
-
- // 监听恢复事件
- eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
-
- // 监听重置球控制器事件
- eventBus.on(GameEvents.RESET_BALL_CONTROLLER, this.onResetBallControllerEvent, this);
-
- // 监听球创建事件
- eventBus.on(GameEvents.BALL_CREATE, this.onBallCreateEvent, this);
-
- // 监听球启动事件
- eventBus.on(GameEvents.BALL_START, this.onBallStartEvent, this);
-
- // 监听创建额外球事件
- eventBus.on(GameEvents.BALL_CREATE_ADDITIONAL, this.onBallCreateAdditionalEvent, this);
-
- // 监听子弹发射检查事件
- eventBus.on(GameEvents.BALL_FIRE_BULLET, this.onBallFireBulletEvent, this);
- }
- /**
- * 处理游戏暂停事件
- */
- private onGamePauseEvent() {
- console.log('[BallController] 接收到游戏暂停事件,暂停小球运动');
- this.pauseBall();
- }
- /**
- * 处理游戏恢复事件
- */
- private onGameResumeEvent() {
- console.log('[BallController] 接收到游戏恢复事件,恢复小球运动');
- this.resumeBall();
- }
-
- /**
- * 处理重置球控制器事件
- */
- private onResetBallControllerEvent() {
- console.log('[BallController] 接收到重置球控制器事件');
- this.resetBallController();
- }
-
- /**
- * 处理球创建事件
- */
- private onBallCreateEvent() {
- console.log('[BallController] 接收到球创建事件');
- this.createBall();
- }
-
- /**
- * 处理球启动事件
- */
- private onBallStartEvent() {
- console.log('[BallController] 接收到球启动事件');
- this.startBall();
- }
-
- /**
- * 处理创建额外球事件
- */
- private onBallCreateAdditionalEvent() {
- console.log('[BallController] 接收到创建额外球事件');
- this.createAdditionalBall();
- }
-
- /**
- * 处理子弹发射检查事件
- */
- private onBallFireBulletEvent(blockNode: Node, fireWorldPos: Vec3) {
- // 如果游戏暂停,则不允许发射子弹
- if (this.isPaused) {
- console.log('[BallController] 游戏暂停中,阻止子弹发射');
- return;
- }
-
- // 如果游戏未暂停,则继续执行子弹发射逻辑
- const weaponConfig: WeaponConfig | null = (blockNode as any)['weaponConfig'] || null;
- this.createAndFireBullet(fireWorldPos, weaponConfig);
- }
- // 计算游戏边界(使用GameArea节点)
- calculateGameBounds() {
- // 获取GameArea节点
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (!gameArea) {
- return;
- }
- const gameAreaUI = gameArea.getComponent(UITransform);
- if (!gameAreaUI) {
- return;
- }
- // 获取GameArea的尺寸
- const areaWidth = gameAreaUI.width;
- const areaHeight = gameAreaUI.height;
-
- // 获取GameArea的世界坐标位置
- const worldPos = gameArea.worldPosition;
- // 计算GameArea的世界坐标边界
- this.gameBounds.left = worldPos.x - areaWidth / 2;
- this.gameBounds.right = worldPos.x + areaWidth / 2;
- this.gameBounds.bottom = worldPos.y - areaHeight / 2;
- this.gameBounds.top = worldPos.y + areaHeight / 2;
-
- }
- // 创建小球
- createBall() {
- if (!this.ballPrefab) {
- console.error('[BallController] ballPrefab 未设置,无法创建小球');
- return;
- }
- // 实例化小球
- this.activeBall = instantiate(this.ballPrefab);
-
- if (!this.activeBall) {
- console.error('[BallController] 小球实例化失败');
- return;
- }
-
- // 将小球添加到GameArea中
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- gameArea.addChild(this.activeBall);
- } else {
- console.warn('[BallController] 未找到GameArea,将小球添加到当前节点');
- this.node.addChild(this.activeBall);
- }
- // 随机位置小球
- this.positionBallRandomly();
- // 设置球的半径
- const transform = this.activeBall.getComponent(UITransform);
- if (transform) {
- this.radius = transform.width / 2;
- } else {
- this.radius = 25; // 默认半径
- }
- // 确保有碰撞组件
- this.setupCollider();
- // 注意:不在这里初始化方向,等待 startBall() 调用
- this.initialized = true;
- }
-
- // 创建额外的小球(不替换现有的小球)
- public createAdditionalBall() {
- if (!this.ballPrefab) {
- console.error('无法创建额外小球:ballPrefab 未设置');
- return;
- }
- // 实例化新的小球
- const newBall = instantiate(this.ballPrefab);
- newBall.name = 'AdditionalBall';
-
- // 将小球添加到GameArea中
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- gameArea.addChild(newBall);
- } else {
- this.node.addChild(newBall);
- }
- // 随机位置小球
- this.positionAdditionalBall(newBall);
- // 设置球的碰撞组件
- this.setupBallCollider(newBall);
-
- // 设置初始方向和速度
- this.initializeBallDirection(newBall);
-
- console.log('创建了额外的小球');
- return newBall;
- }
- // 为额外小球设置随机位置
- private positionAdditionalBall(ball: Node) {
- if (!ball) return;
- const transform = ball.getComponent(UITransform);
- const ballRadius = transform ? transform.width / 2 : this.ballRadius;
-
- // 计算可生成的范围(考虑小球半径,避免生成在边缘)
- const minX = this.gameBounds.left + ballRadius + this.edgeOffset;
- const maxX = this.gameBounds.right - ballRadius - this.edgeOffset;
- const minY = this.gameBounds.bottom + ballRadius + this.edgeOffset;
- const maxY = this.gameBounds.top - ballRadius - this.edgeOffset;
- // 获取GameArea节点
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (!gameArea) {
- return;
- }
-
- // 随机生成位置
- const randomX = Math.random() * (maxX - minX) + minX;
- const randomY = Math.random() * (maxY - minY) + minY;
-
- // 将世界坐标转换为相对于GameArea的本地坐标
- const localPos = gameArea.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(randomX, randomY, 0));
- ball.position = localPos;
- }
- // 为额外小球设置碰撞组件
- private setupBallCollider(ball: Node) {
- // 确保有碰撞组件
- let collider = ball.getComponent(CircleCollider2D);
- if (!collider) {
- collider = ball.addComponent(CircleCollider2D);
- }
-
- // 设置碰撞属性(使用配置值)
- collider.radius = this.ballRadius;
- collider.group = this.colliderGroup;
- collider.tag = this.colliderTag;
- collider.sensor = this.sensor;
- collider.friction = this.friction;
- collider.restitution = this.restitution;
-
- // 添加刚体组件
- let rigidBody = ball.getComponent(RigidBody2D);
- if (!rigidBody) {
- rigidBody = ball.addComponent(RigidBody2D);
- }
-
- // 设置刚体属性(使用配置值)
- rigidBody.type = 2; // 2 = 动态刚体
- rigidBody.allowSleep = false;
- rigidBody.gravityScale = this.gravityScale;
- rigidBody.linearDamping = this.linearDamping;
- rigidBody.angularDamping = this.angularDamping;
- rigidBody.fixedRotation = true;
- rigidBody.enabledContactListener = true; // 启用碰撞监听
-
- // 注意:不需要为每个小球单独添加碰撞回调,
- // 因为我们使用的是全局物理系统回调
- }
- // 为额外小球初始化方向和速度
- private initializeBallDirection(ball: Node) {
- // 随机初始方向
- const angle = Math.random() * Math.PI * 2; // 0-2π之间的随机角度
- const direction = new Vec2(Math.cos(angle), Math.sin(angle)).normalize();
-
- // 设置初始速度
- const rigidBody = ball.getComponent(RigidBody2D);
- if (rigidBody) {
- rigidBody.linearVelocity = new Vec2(
- direction.x * this.currentSpeed,
- direction.y * this.currentSpeed
- );
- }
- }
-
-
- // 检查所有已放置方块的碰撞体组件
- private checkBlockColliders() {
-
- if (!this.placedBlocksContainer) {
- return;
- }
-
- if (!this.placedBlocksContainer.isValid) {
- return;
- }
-
- const blocks = [];
- for (let i = 0; i < this.placedBlocksContainer.children.length; i++) {
- const block = this.placedBlocksContainer.children[i];
- if (block.name.includes('Block') || block.getChildByName('B1')) {
- blocks.push(block);
- }
- }
-
- let fixedCount = 0;
-
- for (let i = 0; i < blocks.length; i++) {
- const block = blocks[i];
-
- // 检查方块本身的碰撞体
- const blockCollider = block.getComponent(Collider2D);
- if (blockCollider) {
- // 🔧 自动修复碰撞组设置
- if (blockCollider.group !== 2) {
- blockCollider.group = 2; // 设置为Block组
- fixedCount++;
- }
-
- // 确保不是传感器
- if (blockCollider.sensor) {
- blockCollider.sensor = false;
- }
- }
-
- // 检查B1子节点的碰撞体
- const b1Node = block.getChildByName('B1');
- if (b1Node) {
- const b1Collider = b1Node.getComponent(Collider2D);
- if (b1Collider) {
- // 🔧 修复B1子节点的碰撞设置
- if (b1Collider.group !== 2) {
- b1Collider.group = 2; // 设置为Block组
- fixedCount++;
- }
-
- // 确保B1不是传感器(需要实际碰撞)
- if (b1Collider.sensor) {
- b1Collider.sensor = false;
- }
- }
- }
-
- // 检查Weapon子节点
- const weaponNode = this.findWeaponNode(block);
- if (weaponNode) {
- // 武器节点存在
- }
- }
- }
- // 随机位置小球
- positionBallRandomly() {
- if (!this.activeBall) return;
- const transform = this.activeBall.getComponent(UITransform);
- const ballRadius = transform ? transform.width / 2 : this.ballRadius;
-
- // 计算可生成的范围(考虑小球半径,避免生成在边缘)
- const minX = this.gameBounds.left + ballRadius + this.edgeOffset; // 额外偏移,避免生成在边缘
- const maxX = this.gameBounds.right - ballRadius - this.edgeOffset;
- const minY = this.gameBounds.bottom + ballRadius + this.edgeOffset;
- const maxY = this.gameBounds.top - ballRadius - this.edgeOffset;
- // 获取GameArea节点
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (!gameArea) {
- return;
- }
-
- // 查找PlacedBlocks节点,它包含所有放置的方块
- if (!this.placedBlocksContainer) {
- this.setRandomPositionDefault(minX, maxX, minY, maxY);
- return;
- }
-
- if (!this.placedBlocksContainer.isValid) {
- this.setRandomPositionDefault(minX, maxX, minY, maxY);
- return;
- }
-
- // 获取所有已放置的方块
- const placedBlocks = [];
- for (let i = 0; i < this.placedBlocksContainer.children.length; i++) {
- const block = this.placedBlocksContainer.children[i];
- // 检查是否是方块节点(通常以Block命名或有特定标识)
- if (block.name.includes('Block') || block.getChildByName('B1')) {
- placedBlocks.push(block);
- }
- }
-
- // 如果没有方块,使用默认随机位置
- if (placedBlocks.length === 0) {
- this.setRandomPositionDefault(minX, maxX, minY, maxY);
- return;
- }
-
- // 尝试找到一个不与任何方块重叠的位置
- let validPosition = false;
- let attempts = 0;
- const maxAttempts = this.maxAttempts; // 从配置中获取最大尝试次数
- let randomX, randomY;
-
- while (!validPosition && attempts < maxAttempts) {
- // 随机生成位置
- randomX = Math.random() * (maxX - minX) + minX;
- randomY = Math.random() * (maxY - minY) + minY;
-
- // 检查是否与任何方块重叠
- let overlapping = false;
- for (const block of placedBlocks) {
- // 获取方块的世界坐标
- const blockWorldPos = block.worldPosition;
-
- // 计算小球与方块的距离
- const distance = Math.sqrt(
- Math.pow(randomX - blockWorldPos.x, 2) +
- Math.pow(randomY - blockWorldPos.y, 2)
- );
-
- // 获取方块的尺寸
- const blockTransform = block.getComponent(UITransform);
- const blockSize = blockTransform ?
- Math.max(blockTransform.width, blockTransform.height) / 2 : this.safeDistance;
-
- // 如果距离小于小球半径+方块尺寸的一半+安全距离,认为重叠
- if (distance < ballRadius + blockSize + this.safeDistance) {
- overlapping = true;
- break;
- }
- }
-
- // 如果没有重叠,找到了有效位置
- if (!overlapping) {
- validPosition = true;
- }
-
- attempts++;
- }
-
- // 如果找不到有效位置,使用默认位置(游戏区域底部中心)
- if (!validPosition) {
- randomX = (this.gameBounds.left + this.gameBounds.right) / 2;
- randomY = this.gameBounds.bottom + ballRadius + this.safeDistance; // 底部上方安全距离单位
- }
-
- // 将世界坐标转换为相对于GameArea的本地坐标
- const localPos = gameArea.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(randomX, randomY, 0));
- this.activeBall.position = localPos;
- }
- // 设置默认随机位置
- setRandomPositionDefault(minX, maxX, minY, maxY) {
- // 随机生成位置
- const randomX = Math.random() * (maxX - minX) + minX;
- const randomY = Math.random() * (maxY - minY) + minY;
- // 将世界坐标转换为相对于GameArea的本地坐标
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- const localPos = gameArea.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(randomX, randomY, 0));
- this.activeBall.position = localPos;
- } else {
- // 直接设置位置(不太准确,但作为后备)
- this.activeBall.position = new Vec3(randomX - this.gameBounds.left, randomY - this.gameBounds.bottom, 0);
- }
- }
- // 设置碰撞组件
- setupCollider() {
- if (!this.activeBall) return;
- // 确保小球有刚体组件
- let rigidBody = this.activeBall.getComponent(RigidBody2D);
- if (!rigidBody) {
- rigidBody = this.activeBall.addComponent(RigidBody2D);
- rigidBody.type = 2; // Dynamic
- rigidBody.gravityScale = this.gravityScale; // 从配置获取重力缩放
- rigidBody.enabledContactListener = true; // 启用碰撞监听
- rigidBody.fixedRotation = true; // 固定旋转
- rigidBody.allowSleep = false; // 不允许休眠
- rigidBody.linearDamping = this.linearDamping; // 从配置获取线性阻尼
- rigidBody.angularDamping = this.angularDamping; // 从配置获取角阻尼
- } else {
- // 确保已有的刚体组件设置正确
- rigidBody.enabledContactListener = true;
- rigidBody.gravityScale = this.gravityScale;
- rigidBody.linearDamping = this.linearDamping; // 从配置获取线性阻尼
- rigidBody.angularDamping = this.angularDamping; // 从配置获取角阻尼
- rigidBody.allowSleep = false; // 不允许休眠
- }
- // 确保小球有碰撞组件
- let collider = this.activeBall.getComponent(CircleCollider2D);
- if (!collider) {
- collider = this.activeBall.addComponent(CircleCollider2D);
- collider.radius = this.radius || this.ballRadius; // 使用已计算的半径或配置值
- collider.tag = this.colliderTag; // 从配置获取小球标签
- collider.group = this.colliderGroup; // 从配置获取碰撞组
- collider.sensor = this.sensor; // 从配置获取传感器设置
- collider.friction = this.friction; // 从配置获取摩擦系数
- collider.restitution = this.restitution; // 从配置获取恢复系数
- } else {
- // 确保已有的碰撞组件设置正确
- collider.sensor = this.sensor;
- collider.restitution = this.restitution;
- collider.group = this.colliderGroup; // 从配置获取碰撞组
- collider.tag = this.colliderTag; // 从配置获取标签
- }
- // === 使用全局回调监听器 ===
- const physics = PhysicsManager.getInstance()?.getSystem();
- if (physics) {
- // 先移除旧监听,避免重复注册
- physics.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- physics.off(Contact2DType.END_CONTACT, this.onEndContact, this);
- physics.off(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
- physics.off(Contact2DType.POST_SOLVE, this.onPostSolve, this);
- physics.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- physics.on(Contact2DType.END_CONTACT, this.onEndContact, this);
- physics.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
- physics.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
- }
- }
- // 碰撞回调 - 处理小球与方块以及小球之间的碰撞
- onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // 检查是否为小球之间的碰撞
- if (selfCollider.group === 1 && otherCollider.group === 1) {
- // 小球之间的碰撞 - 防止速度改变
- this.handleBallToBallCollision(selfCollider, otherCollider, contact);
- return;
- }
-
- // 判断哪个是小球,哪个是碰撞对象
- let ballNode: Node = null;
- let otherNode: Node = null;
-
- // 检查self是否为小球(组1)
- if (selfCollider.group === 1) {
- ballNode = selfCollider.node;
- otherNode = otherCollider.node;
- }
- // 检查other是否为小球(组1)
- else if (otherCollider.group === 1) {
- ballNode = otherCollider.node;
- otherNode = selfCollider.node;
- }
-
- // 如果没有找到小球,跳过处理
- if (!ballNode || !otherNode) {
- return;
- }
- // 检查是否为方块碰撞
- const nodeName = otherNode.name;
- const nodePath = this.getNodePath(otherNode);
- const hasWeaponChild = otherNode.getChildByName('Weapon') !== null;
- const isBlock =
- nodeName.includes('Block') ||
- nodePath.includes('Block') ||
- hasWeaponChild;
- // 如果是方块碰撞,检查防围困机制
- if (isBlock) {
- const ballId = ballNode.uuid;
- const currentTime = performance.now() / 1000; // 转换为秒
- // 检查是否处于穿透状态
- const phaseThroughEndTime = this.ballPhaseThrough.get(ballId);
- if (phaseThroughEndTime && currentTime < phaseThroughEndTime) {
- // 处于穿透状态,忽略碰撞但仍播放特效
- const ballAni = BallAni.getInstance();
- if (ballAni) {
- let contactPos: Vec3 = null;
- if (contact && (contact as any).getWorldManifold) {
- const wm = (contact as any).getWorldManifold();
- if (wm && wm.points && wm.points.length > 0) {
- contactPos = new Vec3(wm.points[0].x, wm.points[0].y, 0);
- }
- }
- if (!contactPos) {
- contactPos = otherNode.worldPosition.clone();
- }
- ballAni.playImpactEffect(contactPos);
- }
- // 禁用碰撞
- if (contact) {
- contact.disabled = true;
- }
- return;
- }
- // 记录撞击历史
- if (!this.ballHitHistory.has(ballId)) {
- this.ballHitHistory.set(ballId, []);
- }
- const hitHistory = this.ballHitHistory.get(ballId);
- hitHistory.push(currentTime);
- // 清理过期的撞击记录
- const timeThreshold = currentTime - this.antiTrapTimeWindow;
- while (hitHistory.length > 0 && hitHistory[0] < timeThreshold) {
- hitHistory.shift();
- }
- // 检查是否达到防围困条件
- if (hitHistory.length >= this.antiTrapHitThreshold) {
- // 获取当前偏移尝试次数
- const deflectionAttempts = this.ballDeflectionAttempts.get(ballId) || 0;
-
- if (deflectionAttempts < this.deflectionAttemptThreshold) {
- // 优先使用偏移方式
- this.ballDeflectionAttempts.set(ballId, deflectionAttempts + 1);
-
- // 应用增强偏移反弹
- const rigidBody = ballNode.getComponent(RigidBody2D);
- if (rigidBody && contact) {
- // 获取碰撞法线
- let normal = new Vec2(0, 1);
- if ((contact as any).getWorldManifold) {
- const wm = (contact as any).getWorldManifold();
- if (wm && wm.normal) {
- normal = new Vec2(wm.normal.x, wm.normal.y);
- }
- }
-
- // 计算增强偏移反弹方向
- const currentVelocity = rigidBody.linearVelocity;
- const currentDirection = new Vec2(currentVelocity.x, currentVelocity.y).normalize();
- const newDirection = this.calculateAntiTrapReflection(currentDirection, normal);
-
- // 应用新的速度方向
- const speed = currentVelocity.length();
- rigidBody.linearVelocity = new Vec2(newDirection.x * speed, newDirection.y * speed);
-
- console.log(`Ball ${ballId} applied anti-trap deflection (attempt ${deflectionAttempts + 1}/${this.deflectionAttemptThreshold})`);
- }
-
- // 清空撞击历史,给偏移一个机会
- hitHistory.length = 0;
- } else {
- // 偏移尝试次数已达上限,使用穿透
- this.ballPhaseThrough.set(ballId, currentTime + 0.5);
- // 重置偏移尝试次数
- this.ballDeflectionAttempts.set(ballId, 0);
- // 清空撞击历史
- hitHistory.length = 0;
- // 禁用当前碰撞
- if (contact) {
- contact.disabled = true;
- }
- console.log(`Ball ${ballId} entered phase-through mode after ${this.deflectionAttemptThreshold} deflection attempts failed`);
- return;
- }
- }
- }
-
- // 计算碰撞世界坐标
- let contactPos: Vec3 = null;
- if (contact && (contact as any).getWorldManifold) {
- const wm = (contact as any).getWorldManifold();
- if (wm && wm.points && wm.points.length > 0) {
- contactPos = new Vec3(wm.points[0].x, wm.points[0].y, 0);
- }
- }
- if (!contactPos) {
- contactPos = otherNode.worldPosition.clone();
- }
- // 播放撞击特效 - 对所有碰撞都播放特效
- const ballAni = BallAni.getInstance();
- if (ballAni) {
- ballAni.playImpactEffect(contactPos);
- }
-
- if (isBlock) {
- // 播放方块撞击动画
- if (ballAni) {
- ballAni.playBlockHitAnimation(otherNode);
- }
- // 通过事件检查是否可以发射子弹
- const eventBus = EventBus.getInstance();
- let canFire = true;
-
- // 发送检查事件,如果有监听器返回false则不发射
- eventBus.emit(GameEvents.BALL_FIRE_BULLET, { canFire: (value: boolean) => { canFire = value; } });
-
- if (canFire) {
- const now = performance.now();
- const lastTime = this.blockFireCooldown.get(otherNode.uuid) || 0;
- if (now - lastTime > this.FIRE_COOLDOWN * 1000) {
- this.blockFireCooldown.set(otherNode.uuid, now);
- this.fireBulletAt(otherNode, contactPos);
- }
- }
- }
- }
- /**
- * 处理小球之间的碰撞 - 保持恒定速度
- */
- private handleBallToBallCollision(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- const ball1 = selfCollider.node;
- const ball2 = otherCollider.node;
-
- const rigidBody1 = ball1.getComponent(RigidBody2D);
- const rigidBody2 = ball2.getComponent(RigidBody2D);
-
- if (!rigidBody1 || !rigidBody2) {
- return;
- }
-
- // 计算碰撞位置并播放撞击特效
- let contactPos: Vec3 = null;
- if (contact && (contact as any).getWorldManifold) {
- const wm = (contact as any).getWorldManifold();
- if (wm && wm.points && wm.points.length > 0) {
- contactPos = new Vec3(wm.points[0].x, wm.points[0].y, 0);
- }
- }
- if (!contactPos) {
- // 使用两个小球位置的中点作为碰撞位置
- const pos1 = ball1.worldPosition;
- const pos2 = ball2.worldPosition;
- contactPos = new Vec3(
- (pos1.x + pos2.x) / 2,
- (pos1.y + pos2.y) / 2,
- 0
- );
- }
-
- // 播放撞击特效
- const ballAni = BallAni.getInstance();
- if (ballAni) {
- ballAni.playImpactEffect(contactPos);
- }
-
- // 获取碰撞前的速度
- const velocity1 = rigidBody1.linearVelocity.clone();
- const velocity2 = rigidBody2.linearVelocity.clone();
-
- // 计算速度大小
- const speed1 = Math.sqrt(velocity1.x * velocity1.x + velocity1.y * velocity1.y);
- const speed2 = Math.sqrt(velocity2.x * velocity2.x + velocity2.y * velocity2.y);
-
- // 延迟一帧后恢复正确的速度,避免物理引擎的速度改变
- this.scheduleOnce(() => {
- if (ball1.isValid && rigidBody1.isValid) {
- const currentVel1 = rigidBody1.linearVelocity;
- const currentSpeed1 = Math.sqrt(currentVel1.x * currentVel1.x + currentVel1.y * currentVel1.y);
-
- // 如果速度发生了显著变化,恢复到目标速度
- if (Math.abs(currentSpeed1 - this.currentSpeed) > 5) {
- const normalizedVel1 = currentVel1.clone().normalize();
- rigidBody1.linearVelocity = new Vec2(
- normalizedVel1.x * this.currentSpeed,
- normalizedVel1.y * this.currentSpeed
- );
- }
- }
-
- if (ball2.isValid && rigidBody2.isValid) {
- const currentVel2 = rigidBody2.linearVelocity;
- const currentSpeed2 = Math.sqrt(currentVel2.x * currentVel2.x + currentVel2.y * currentVel2.y);
-
- // 如果速度发生了显著变化,恢复到目标速度
- if (Math.abs(currentSpeed2 - this.currentSpeed) > 5) {
- const normalizedVel2 = currentVel2.clone().normalize();
- rigidBody2.linearVelocity = new Vec2(
- normalizedVel2.x * this.currentSpeed,
- normalizedVel2.y * this.currentSpeed
- );
- }
- }
- }, 0.016); // 约一帧的时间
- }
-
- // 碰撞结束事件 - 简化版本
- onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // Debug log removed
- }
-
- // 碰撞预处理事件 - 简化版本
- onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // console.log('⚙️ 碰撞预处理:', otherCollider.node.name);
- }
-
- // 碰撞后处理事件 - 简化版本
- onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // console.log('✅ 碰撞后处理:', otherCollider.node.name);
- }
- // 计算反射向量
- calculateReflection(direction: Vec2, normal: Vec2): Vec2 {
- // 使用反射公式: R = V - 2(V·N)N
- const dot = direction.x * normal.x + direction.y * normal.y;
- const reflection = new Vec2(
- direction.x - 2 * dot * normal.x,
- direction.y - 2 * dot * normal.y
- );
- reflection.normalize();
-
- // 添加一些随机性,避免重复的反弹路径
- const randomAngle = (Math.random() - 0.5) * this.maxReflectionRandomness; // 随机角度
- const cos = Math.cos(randomAngle);
- const sin = Math.sin(randomAngle);
-
- // 应用随机旋转
- const randomizedReflection = new Vec2(
- reflection.x * cos - reflection.y * sin,
- reflection.x * sin + reflection.y * cos
- );
-
- // 确保反射方向不会太接近水平或垂直方向
- // 这有助于避免球在水平或垂直方向上来回反弹
- const minAngleFromAxis = 0.1; // 约5.7度
-
- // 检查是否接近水平方向
- if (Math.abs(randomizedReflection.y) < minAngleFromAxis) {
- // 调整y分量,使其远离水平方向
- const sign = randomizedReflection.y >= 0 ? 1 : -1;
- randomizedReflection.y = sign * (minAngleFromAxis + Math.random() * 0.1);
- // 重新归一化
- randomizedReflection.normalize();
- }
-
- // 检查是否接近垂直方向
- if (Math.abs(randomizedReflection.x) < minAngleFromAxis) {
- // 调整x分量,使其远离垂直方向
- const sign = randomizedReflection.x >= 0 ? 1 : -1;
- randomizedReflection.x = sign * (minAngleFromAxis + Math.random() * 0.1);
- // 重新归一化
- randomizedReflection.normalize();
- }
-
- randomizedReflection.normalize();
- // Debug log removed
-
- return randomizedReflection;
- }
- /**
- * 计算防围困增强偏移反弹方向
- * @param direction 当前方向
- * @param normal 碰撞法线
- * @returns 增强偏移后的反弹方向
- */
- calculateAntiTrapReflection(direction: Vec2, normal: Vec2): Vec2 {
- // 使用反射公式: R = V - 2(V·N)N
- const dot = direction.x * normal.x + direction.y * normal.y;
- const reflection = new Vec2(
- direction.x - 2 * dot * normal.x,
- direction.y - 2 * dot * normal.y
- );
- reflection.normalize();
-
- // 应用更强的随机偏移来帮助脱困
- const enhancedRandomness = this.maxReflectionRandomness * this.antiTrapDeflectionMultiplier;
- const randomAngle = (Math.random() - 0.5) * enhancedRandomness;
- const cos = Math.cos(randomAngle);
- const sin = Math.sin(randomAngle);
-
- // 应用随机旋转
- const randomizedReflection = new Vec2(
- reflection.x * cos - reflection.y * sin,
- reflection.x * sin + reflection.y * cos
- );
-
- // 更强的轴向偏移避免
- const minAngleFromAxis = 0.3; // 约17度,比普通反弹更大的偏移
-
- // 检查是否接近水平方向
- if (Math.abs(randomizedReflection.y) < minAngleFromAxis) {
- const sign = randomizedReflection.y >= 0 ? 1 : -1;
- randomizedReflection.y = sign * (minAngleFromAxis + Math.random() * 0.2);
- randomizedReflection.normalize();
- }
-
- // 检查是否接近垂直方向
- if (Math.abs(randomizedReflection.x) < minAngleFromAxis) {
- const sign = randomizedReflection.x >= 0 ? 1 : -1;
- randomizedReflection.x = sign * (minAngleFromAxis + Math.random() * 0.2);
- randomizedReflection.normalize();
- }
-
- randomizedReflection.normalize();
- console.log(`Applied anti-trap enhanced deflection with angle: ${randomAngle}`);
-
- return randomizedReflection;
- }
- /**
- * 从方块武器发射子弹攻击敌人 - 重构版本
- * 现在直接创建子弹实例并使用BulletController的实例方法
- * @param blockNode 激活的方块节点
- */
- fireBullet(blockNode: Node) {
- // Debug logs removed
-
- // 检查子弹预制体是否存在
- if (!this.bulletPrefab) {
- return;
- }
-
- // 查找方块中的Weapon节点
- const weaponNode = this.findWeaponNode(blockNode);
- let firePosition: Vec3;
-
- if (!weaponNode) {
- firePosition = blockNode.worldPosition;
- } else {
- // 获取武器的世界坐标作为发射位置
- try {
- firePosition = weaponNode.worldPosition;
- } catch (error) {
- // 备用方案:使用方块坐标
- firePosition = blockNode.worldPosition;
- }
- }
-
- // 通过事件系统发射子弹,这样可以被暂停检查拦截
- EventBus.getInstance().emit(GameEvents.BALL_FIRE_BULLET, blockNode, firePosition);
- }
-
- /**
- * 创建并发射子弹 - 使用新的WeaponBullet系统
- * @param firePosition 发射位置(世界坐标)
- * @param weaponConfig 武器配置
- */
- private createAndFireBullet(firePosition: Vec3, weaponConfig: WeaponConfig | null) {
-
- // 确保武器配置加载
- WeaponBullet.loadWeaponsData().then(() => {
- // 默认使用毛豆射手配置,后续可以根据方块类型动态选择
- const defaultWeaponId = 'pea_shooter';
- const finalConfig = weaponConfig || WeaponBullet.getWeaponConfig(defaultWeaponId);
-
- if (!finalConfig) {
- return;
- }
-
- // 创建子弹初始化数据
- const initData: BulletInitData = {
- weaponId: finalConfig.id,
- firePosition: firePosition,
- autoTarget: true,
- weaponConfig: finalConfig
- };
-
- // 验证初始化数据
- if (!WeaponBullet.validateInitData(initData)) {
- return;
- }
-
- // 判断是否为多发子弹(散射/连发等)
- const countCfg = finalConfig.bulletConfig.count;
- const isMultiShot = countCfg && countCfg.type !== 'single' && countCfg.amount > 1;
- // 查找GameArea(子弹统一添加到此节点)
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (!gameArea) {
- return;
- }
- // === 根据武器配置选择合适的预制体 ===
- const trailEffect = finalConfig.bulletConfig?.visual?.trailEffect;
- // 尖胡萝卜特殊处理:使用PelletContainer预制体但激活Spine节点
- const isSharpCarrot = finalConfig.id === 'sharp_carrot';
- // 当trailEffect为true或者是尖胡萝卜时都使用容器预制体
- const needsContainerPrefab = Boolean(trailEffect) || isSharpCarrot;
- const prefabToUse: Prefab = (needsContainerPrefab && this.bulletContainerPrefab) ? this.bulletContainerPrefab : this.bulletPrefab;
- if (!prefabToUse) {
- return; // 如果没有可用的预制体则直接退出
- }
- if (isMultiShot) {
- // 使用批量创建逻辑
- const bullets = WeaponBullet.createBullets(initData, prefabToUse as any);
- bullets.forEach(b => {
- gameArea.addChild(b);
- });
- } else {
- // 单发逻辑(沿用原流程)
- const bullet = instantiate(prefabToUse);
- if (!bullet) {
- return;
- }
- gameArea.addChild(bullet);
- let weaponBullet = bullet.getComponent(WeaponBullet);
- if (!weaponBullet) {
- weaponBullet = bullet.addComponent(WeaponBullet);
- }
- weaponBullet.init(initData);
- }
-
- }).catch(error => {
- // 武器配置加载失败
- });
- }
-
- // 递归查找Weapon节点
- private findWeaponNode(node: Node): Node | null {
- // logs removed
-
- // 先检查当前节点是否有Weapon子节点
- const weaponNode = node.getChildByName('Weapon');
- if (weaponNode) {
- return weaponNode;
- }
-
- // 如果没有,递归检查所有子节点
- for (let i = 0; i < node.children.length; i++) {
- const child = node.children[i];
- const foundWeapon = this.findWeaponNode(child);
- if (foundWeapon) {
- return foundWeapon;
- }
- }
-
- // 如果都没找到,返回null
- return null;
- }
- // 获取节点的完整路径
- private getNodePath(node: Node): string {
- let path = node.name;
- let current = node;
-
- while (current.parent) {
- current = current.parent;
- path = current.name + '/' + path;
- }
-
- return path;
- }
- update(dt: number) {
- // 只有当小球已启动时才执行运动逻辑
- if (!this.ballStarted || !this.initialized) {
- return;
- }
-
- // 维持所有小球的恒定速度
- this.maintainAllBallsSpeed();
-
- // 清理过期的防围困状态
- this.cleanupExpiredAntiTrapStates();
-
- // 定期检查小球是否接近方块但没有触发碰撞(调试用)
- if (this.activeBall && this.activeBall.isValid) {
- this.debugCheckNearBlocks();
- }
- }
-
- /**
- * 维持所有小球的恒定速度
- */
- private maintainAllBallsSpeed() {
- // 维持主小球速度
- if (this.activeBall && this.activeBall.isValid) {
- this.maintainBallSpeed(this.activeBall);
- }
-
- // 维持额外小球的速度
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- const additionalBalls = gameArea.children.filter(child =>
- child.name === 'AdditionalBall' && child.isValid
- );
-
- for (const ball of additionalBalls) {
- this.maintainBallSpeed(ball);
- }
- }
- }
-
- /**
- * 维持单个小球的恒定速度
- */
- private maintainBallSpeed(ball: Node) {
- const rigidBody = ball.getComponent(RigidBody2D);
- if (!rigidBody) return;
-
- // 获取当前速度
- const currentVelocity = rigidBody.linearVelocity;
- const speed = Math.sqrt(currentVelocity.x * currentVelocity.x + currentVelocity.y * currentVelocity.y);
-
- // 如果速度过低或过高,重新设置速度以维持恒定运动
- if (speed < this.currentSpeed * 0.85 || speed > this.currentSpeed * 1.15) {
- // 保持当前方向,但调整速度大小
- if (speed > 0.1) {
- const normalizedVelocity = currentVelocity.clone().normalize();
- rigidBody.linearVelocity = new Vec2(
- normalizedVelocity.x * this.currentSpeed,
- normalizedVelocity.y * this.currentSpeed
- );
- } else {
- // 如果速度几乎为0,给一个随机方向
- const angle = Math.random() * Math.PI * 2;
- rigidBody.linearVelocity = new Vec2(
- Math.cos(angle) * this.currentSpeed,
- Math.sin(angle) * this.currentSpeed
- );
- }
- }
-
- // 更新主小球的方向向量(用于其他逻辑)
- if (ball === this.activeBall && speed > 1.0) {
- this.direction.x = currentVelocity.x / speed;
- this.direction.y = currentVelocity.y / speed;
- }
- }
-
- // 调试方法:检查小球是否接近方块但没有触发物理碰撞
- private debugCheckCounter = 0;
- private debugCheckNearBlocks() {
- this.debugCheckCounter++;
- // 每60帧(约1秒)检查一次
- if (this.debugCheckCounter % 60 !== 0) return;
-
- const ballPos = this.activeBall.worldPosition;
- if (!this.placedBlocksContainer || !this.placedBlocksContainer.isValid) return;
-
- let nearestDistance = Infinity;
- let nearestBlock = null;
-
- for (let i = 0; i < this.placedBlocksContainer.children.length; i++) {
- const block = this.placedBlocksContainer.children[i];
- if (block.name.includes('Block') || block.getChildByName('B1')) {
- const blockPos = block.worldPosition;
- const distance = Math.sqrt(
- Math.pow(ballPos.x - blockPos.x, 2) +
- Math.pow(ballPos.y - blockPos.y, 2)
- );
-
- if (distance < nearestDistance) {
- nearestDistance = distance;
- nearestBlock = block;
- }
- }
- }
-
- if (nearestBlock && nearestDistance < 100) {
- // 检查小球的碰撞体状态
- const ballCollider = this.activeBall.getComponent(Collider2D);
- if (ballCollider) {
- if (ballCollider instanceof CircleCollider2D) {
- // 小球碰撞半径检查
- }
- }
-
- // 检查小球的刚体状态
- const ballRigidBody = this.activeBall.getComponent(RigidBody2D);
- if (ballRigidBody) {
- // 刚体状态检查
- }
-
- // 检查最近的方块碰撞体
- const blockCollider = nearestBlock.getComponent(Collider2D);
- if (blockCollider) {
- // 方块碰撞体检查
- }
- }
- }
- // 初始化方向
- initializeDirection() {
- // 随机初始方向
- const angle = Math.random() * Math.PI * 2; // 0-2π之间的随机角度
- this.direction.x = Math.cos(angle);
- this.direction.y = Math.sin(angle);
- this.direction.normalize();
-
- // 设置初始速度
- if (this.activeBall) {
- const rigidBody = this.activeBall.getComponent(RigidBody2D);
- if (rigidBody) {
- rigidBody.linearVelocity = new Vec2(
- this.direction.x * this.currentSpeed,
- this.direction.y * this.currentSpeed
- );
- }
- }
- }
- // 初始化球的参数 - 公开方法,供GameManager调用
- public initialize() {
- this.calculateGameBounds();
- this.createBall();
-
- // 检查方块碰撞体(延迟执行,确保方块已放置)
- this.scheduleOnce(() => {
- this.checkBlockColliders();
- }, 0.5);
- }
-
- // 启动小球 - 公开方法,在确定按钮点击后调用
- public startBall() {
- // 检查ballPrefab是否设置
- if (!this.ballPrefab) {
- console.error('[BallController] ballPrefab 未设置,无法创建小球');
- return;
- }
-
- // 如果还没有初始化,先初始化
- if (!this.initialized) {
- this.initialize();
- }
-
- // 确保小球存在且有效
- if (!this.activeBall || !this.activeBall.isValid) {
- this.createBall();
- }
-
- // 检查小球是否成功创建
- if (!this.activeBall) {
- console.error('[BallController] 小球创建失败');
- return;
- }
-
- // 重新定位小球,避免与方块重叠
- this.positionBallRandomly();
-
- // 确保物理组件设置正确
- this.setupCollider();
-
- // 初始化运动方向并开始运动
- this.initializeDirection();
-
- // 设置运动状态
- this.ballStarted = true;
-
- console.log('[BallController] 小球启动完成');
- }
- /**
- * 暂停小球运动:记录当前速度并停止刚体
- */
- public pauseBall() {
- if (this.isPaused) return;
- this.isPaused = true;
- this.ballStarted = false;
- if (this.activeBall && this.activeBall.isValid) {
- const rb = this.activeBall.getComponent(RigidBody2D);
- if (rb) {
- this.pausedVelocity = rb.linearVelocity.clone();
- rb.linearVelocity = new Vec2(0, 0);
- rb.sleep();
- }
- }
- }
- /**
- * 恢复小球运动:恢复暂停前的速度
- */
- public resumeBall() {
- if (!this.isPaused) return;
- this.isPaused = false;
- this.ballStarted = true;
- console.log('恢复小球运动');
- if (this.activeBall && this.activeBall.isValid) {
- const rb = this.activeBall.getComponent(RigidBody2D);
- if (rb) {
- rb.wakeUp();
- const hasPrevVelocity = this.pausedVelocity && (this.pausedVelocity.x !== 0 || this.pausedVelocity.y !== 0);
- if (hasPrevVelocity) {
- rb.linearVelocity = this.pausedVelocity.clone();
- } else {
- // 若没有记录速度,则重新初始化方向
- this.initializeDirection();
- }
- }
- }
- }
- /**
- * 从给定世界坐标发射子弹
- */
- private fireBulletAt(blockNode: Node, fireWorldPos: Vec3) {
-
- // 检查子弹预制体是否存在
- if (!this.bulletPrefab) {
- return;
- }
-
- // 直接使用碰撞世界坐标作为发射点
- const weaponConfig: WeaponConfig | null = (blockNode as any)['weaponConfig'] || null;
- this.createAndFireBullet(fireWorldPos, weaponConfig);
- }
- /**
- * 重置球控制器状态 - 游戏重置时调用
- */
- public resetBallController() {
- console.log('[BallController] 重置球控制器状态');
-
- // 停止球的运动
- this.ballStarted = false;
- this.isPaused = false;
-
- // 清理暂停状态
- this.pausedVelocity = new Vec2();
-
- // 销毁当前活动的球
- if (this.activeBall && this.activeBall.isValid) {
- console.log('[BallController] 销毁当前活动的球');
- this.activeBall.destroy();
- }
- this.activeBall = null;
-
- // 销毁所有额外创建的球
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- const additionalBalls = gameArea.children.filter(child =>
- child.name === 'AdditionalBall' && child.isValid
- );
-
- for (const ball of additionalBalls) {
- console.log('[BallController] 销毁额外球:', ball.name);
- ball.destroy();
- }
-
- if (additionalBalls.length > 0) {
- console.log(`[BallController] 已清理 ${additionalBalls.length} 个额外球`);
- }
- }
-
- // 重置初始化状态
- this.initialized = false;
-
- // 清理冷却时间
- this.blockFireCooldown.clear();
-
- // 清理防围困状态数据
- this.ballHitHistory.clear();
- this.ballPhaseThrough.clear();
- this.ballDeflectionAttempts.clear();
-
- // 重置球速
- this.updateBallSpeed();
-
- console.log('[BallController] 球控制器重置完成');
- }
- /**
- * 清理过期的防围困状态
- */
- private cleanupExpiredAntiTrapStates() {
- const currentTime = performance.now() / 1000;
-
- // 清理过期的穿透状态
- for (const [ballId, endTime] of this.ballPhaseThrough.entries()) {
- if (currentTime >= endTime) {
- this.ballPhaseThrough.delete(ballId);
- }
- }
-
- // 检查并重置长时间未频繁撞击的球的偏移尝试次数
- for (const [ballId, hitHistory] of this.ballHitHistory.entries()) {
- // 如果撞击历史为空或最近一次撞击超过时间窗口的一半,重置偏移尝试次数
- if (hitHistory.length === 0 ||
- (hitHistory.length > 0 && currentTime - hitHistory[hitHistory.length - 1] > this.antiTrapTimeWindow / 2)) {
- if (this.ballDeflectionAttempts.has(ballId)) {
- this.ballDeflectionAttempts.set(ballId, 0);
- }
- }
- }
- }
-
- /**
- * 清理单个小球的防围困状态数据
- * @param ballId 小球的唯一标识符
- */
- public cleanupBallAntiTrapState(ballId: string) {
- this.ballHitHistory.delete(ballId);
- this.ballPhaseThrough.delete(ballId);
- this.ballDeflectionAttempts.delete(ballId);
- console.log(`Cleaned up anti-trap state for ball ${ballId}`);
- }
- onDestroy() {
- // 清理事件监听
- const eventBus = EventBus.getInstance();
- eventBus.off(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
- eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
- eventBus.off(GameEvents.RESET_BALL_CONTROLLER, this.onResetBallControllerEvent, this);
-
- // 清理防围困状态数据
- this.ballHitHistory.clear();
- this.ballPhaseThrough.clear();
- }
- private updateBallSpeed() {
- const skillManager = PersistentSkillManager.getInstance();
- if (skillManager) {
- this.currentSpeed = skillManager.applyBallSpeedBonus(this.baseSpeed);
- } else {
- this.currentSpeed = this.baseSpeed;
- }
- }
- }
|