import { _decorator, AudioClip, Collider2D, Component, Contact2DType, Node, ParticleSystem, Prefab, Vec2 } from 'cc'; import TimeConfig from '../Enum/TimeConfig'; import { CameraController } from '../Environments/CameraController'; import { GameManager } from '../Manager/GameManager'; import { SoundManager } from '../Manager/SoundManager'; import ObjectPool from '../Pool/ObjectPool'; import Utilities from '../Utilities'; import { Ball } from './Ball'; const { ccclass, property } = _decorator; @ccclass('MultiBall') export class MultiBall extends Component { @property({ type: Collider2D, visible: true }) private _collider: Collider2D; @property({ type: Node, visible: true }) private _portLeft; @property({ type: Node, visible: true }) private _portRight; @property({ type: Prefab, visible: true }) private _fx: Prefab; @property({ type: AudioClip, visible: true }) private _soundFX: AudioClip; private _originBall: Ball; private _trigged = false; private _fxPool: ObjectPool; private _enabled: boolean = true; protected onLoad(): void { this._fxPool = new ObjectPool(this._fx, 2, true); this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); } protected lateUpdate(dt: number): void { if (this._trigged) { CameraController.instance.shake(0.2); const ball1 = this._originBall; ball1.clearRigiState(false); const ball2 = GameManager.instance.spawnBall(false, false); ball2.clearRigiState(false); ball1.node.setWorldPosition(this._portRight.worldPosition); ball1.clearRigiState(true); ball1.addForce(new Vec2(20, 0)); ball2.node.setWorldPosition(this._portLeft.worldPosition); ball2.clearRigiState(true); ball2.addForce(new Vec2(-20, 0)); this._trigged = false; } } private async onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) { if (!this._enabled) return; if (this._trigged) return; this._enabled = false; this._originBall = otherCollider.getComponent(Ball); this._trigged = true; const fx = this._fxPool.get(ParticleSystem, GameManager.instance.topContainer); const pos = this.node.getWorldPosition(); pos.z = 10; fx.node.setWorldPosition(pos); SoundManager.instance.playSfx(this._soundFX); await Utilities.delay(TimeConfig.DelayMultiBall); this._enabled = true; await Utilities.waitUntil(() => fx.isStopped); this._fxPool.release(fx); } }