2024-06-09 05:12:08 -07:00
|
|
|
import { _decorator, Animation, CCInteger, Collider2D, Component, Contact2DType, ParticleSystem, Prefab } from 'cc';
|
2024-05-15 00:42:31 -07:00
|
|
|
import TimeConfig from '../Enum/TimeConfig';
|
|
|
|
import { CameraController } from '../Environments/CameraController';
|
2024-03-07 03:15:08 -08:00
|
|
|
import { GameManager } from '../Manager/GameManager';
|
2024-03-07 09:45:13 -08:00
|
|
|
import ObjectPool from '../Pool/ObjectPool';
|
2024-06-09 05:12:08 -07:00
|
|
|
import Utils from '../Utilities';
|
2024-03-10 03:12:55 -07:00
|
|
|
import { Ball } from './Ball';
|
2024-03-10 20:46:50 -07:00
|
|
|
const { ccclass, property } = _decorator;
|
2024-03-07 03:15:08 -08:00
|
|
|
|
|
|
|
@ccclass('Goal')
|
|
|
|
export class Goal extends Component {
|
|
|
|
@property({ type: Collider2D, visible: true })
|
|
|
|
private _collider: Collider2D;
|
2024-04-03 20:20:56 -07:00
|
|
|
@property({ type: Animation, visible: true })
|
|
|
|
private _animation: Animation;
|
2024-03-07 03:15:08 -08:00
|
|
|
@property({ type: CCInteger, visible: true })
|
|
|
|
private _score: number;
|
2024-03-10 20:46:50 -07:00
|
|
|
@property({ type: Prefab, visible: true })
|
|
|
|
private _goalFx: Prefab;
|
|
|
|
|
|
|
|
private _goalFxPool: ObjectPool;
|
2024-03-07 03:15:08 -08:00
|
|
|
|
|
|
|
protected onLoad(): void {
|
|
|
|
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
|
2024-03-10 20:46:50 -07:00
|
|
|
this._goalFxPool = new ObjectPool(this._goalFx, 5, false);
|
2024-03-07 03:15:08 -08:00
|
|
|
}
|
|
|
|
|
2024-03-08 03:07:41 -08:00
|
|
|
private async onContactBegin(selfCollider: Collider2D, otherCollider: Collider2D) {
|
|
|
|
const ball = otherCollider.getComponent(Ball);
|
|
|
|
if (ball) {
|
2024-03-10 03:12:55 -07:00
|
|
|
GameManager.instance.goal(this._score, ball.node.getWorldPosition());
|
2024-05-15 00:42:31 -07:00
|
|
|
ball.clearRigiState(false);
|
|
|
|
const fx = this._goalFxPool.get(ParticleSystem, GameManager.instance.topContainer);
|
2024-03-10 20:46:50 -07:00
|
|
|
const pos = ball.node.getWorldPosition();
|
|
|
|
pos.z = 10;
|
|
|
|
fx.node.setWorldPosition(pos);
|
|
|
|
fx.play();
|
2024-03-10 03:12:55 -07:00
|
|
|
ObjectPool.release(ball.node);
|
2024-03-28 20:35:44 -07:00
|
|
|
CameraController.instance.shake(0.5);
|
2024-04-03 20:20:56 -07:00
|
|
|
this.playAnimationGoal();
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.waitUntil(() => fx.isStopped);
|
2024-04-03 02:43:18 -07:00
|
|
|
this._goalFxPool.release(fx);
|
2024-03-08 03:07:41 -08:00
|
|
|
}
|
2024-03-07 03:15:08 -08:00
|
|
|
}
|
2024-04-03 20:20:56 -07:00
|
|
|
|
|
|
|
public async playAnimationGoal() {
|
|
|
|
this._animation.play(this._animation.clips[1].name);
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.delay(TimeConfig.DelayGoal);
|
2024-04-03 20:20:56 -07:00
|
|
|
this._animation.play();
|
|
|
|
}
|
2024-03-07 03:15:08 -08:00
|
|
|
}
|