import { _decorator, AudioClip, CCInteger, Collider2D, Component, Contact2DType, ParticleSystem, Prefab, Animation, } from 'cc'; import { GameManager } from '../Manager/GameManager'; import Utilities from '../Utilities'; import ObjectPool from '../Pool/ObjectPool'; import { Ball } from './Ball'; import { SoundManager } from '../Manager/SoundManager'; import { CameraController } from '../Environments/CameraController'; import TimeConfig from '../Enum/TimeConfig'; const { ccclass, property } = _decorator; @ccclass('Goal') export class Goal extends Component { @property({ type: Collider2D, visible: true }) private _collider: Collider2D; @property({ type: Animation, visible: true }) private _animation: Animation; @property({ type: CCInteger, visible: true }) private _score: number; @property({ type: Prefab, visible: true }) private _goalFx: Prefab; @property({ type: AudioClip, visible: true }) private _goalSound: AudioClip; private _goalFxPool: ObjectPool; protected onLoad(): void { this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this); this._goalFxPool = new ObjectPool(this._goalFx, 5, false); } private async onContactBegin(selfCollider: Collider2D, otherCollider: Collider2D) { const ball = otherCollider.getComponent(Ball); if (ball) { GameManager.instance.goal(this._score, ball.node.getWorldPosition()); ball.setActiveRigi(false); const fx = this._goalFxPool.get(ParticleSystem, this.node); const pos = ball.node.getWorldPosition(); pos.z = 10; fx.node.setWorldPosition(pos); fx.play(); SoundManager.instance.playSfx(this._goalSound); ObjectPool.release(ball.node); CameraController.instance.shake(0.5); this.playAnimationGoal(); await Utilities.waitUntil(() => fx.isStopped); this._goalFxPool.release(fx); } } public async playAnimationGoal() { this._animation.play(this._animation.clips[1].name); await Utilities.delay(TimeConfig.DelayGoal); this._animation.play(); } }