pinball/assets/_Game/Scripts/GamePlay/Goal.ts

56 lines
1.8 KiB
TypeScript

import {
_decorator,
AudioClip,
CCInteger,
Collider2D,
Component,
Contact2DType,
Node,
ParticleSystem,
Prefab,
} 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 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: 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(this.node, ParticleSystem);
const pos = ball.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
fx.play();
SoundManager.instance.playSfx(this._goalSound);
await Utilities.delay(TimeConfig.DelayPLay);
ObjectPool.release(ball.node);
await Utilities.waitUntil(() => fx.isStopped);
this._goalFxPool.release(fx.node);
}
}
}