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

66 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-03-10 20:46:50 -07:00
import {
_decorator,
AudioClip,
CCInteger,
Collider2D,
Component,
Contact2DType,
ParticleSystem,
Prefab,
2024-04-03 20:20:56 -07:00
Animation,
2024-03-10 20:46:50 -07:00
} from 'cc';
2024-03-07 03:15:08 -08:00
import { GameManager } from '../Manager/GameManager';
2024-03-10 03:12:55 -07:00
import Utilities from '../Utilities';
2024-03-07 09:45:13 -08:00
import ObjectPool from '../Pool/ObjectPool';
2024-03-10 03:12:55 -07:00
import { Ball } from './Ball';
2024-03-10 20:46:50 -07:00
import { SoundManager } from '../Manager/SoundManager';
2024-03-28 20:35:44 -07:00
import { CameraController } from '../Environments/CameraController';
2024-04-03 20:20:56 -07:00
import TimeConfig from '../Enum/TimeConfig';
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;
@property({ type: AudioClip, visible: true })
private _goalSound: AudioClip;
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-03-08 03:07:41 -08:00
ball.setActiveRigi(false);
2024-04-03 02:43:18 -07:00
const fx = this._goalFxPool.get(ParticleSystem, this.node);
2024-03-10 20:46:50 -07:00
const pos = ball.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
fx.play();
SoundManager.instance.playSfx(this._goalSound);
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-03-10 20:46:50 -07:00
await Utilities.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);
await Utilities.delay(TimeConfig.DelayGoal);
this._animation.play();
}
2024-03-07 03:15:08 -08:00
}