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

52 lines
2.0 KiB
TypeScript

import { _decorator, Animation, CCInteger, Collider2D, Component, Contact2DType, ParticleSystem, Prefab } from 'cc';
import TimeConfig from '../Enum/TimeConfig';
import { CameraController } from '../Environments/CameraController';
import { GameManager } from '../Manager/GameManager';
import ObjectPool from '../Pool/ObjectPool';
import Utils from '../Utilities';
import { Ball } from './Ball';
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;
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.clearRigiState(false);
const fx = this._goalFxPool.get(ParticleSystem, GameManager.instance.topContainer);
const pos = ball.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
fx.play();
ObjectPool.release(ball.node);
CameraController.instance.shake(0.5);
this.playAnimationGoal();
await Utils.waitUntil(() => fx.isStopped);
this._goalFxPool.release(fx);
}
}
public async playAnimationGoal() {
this._animation.play(this._animation.clips[1].name);
await Utils.delay(TimeConfig.DelayGoal);
this._animation.play();
}
}