pinball/assets/_Game/Scripts/Booster/BoosterBase.ts

50 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-05-15 00:42:31 -07:00
import { _decorator, Animation, AudioClip, CCFloat, CCString, Collider2D, Component, Contact2DType } from 'cc';
2024-03-27 04:00:23 -07:00
import GameEvent from '../Events/GameEvent';
2024-05-27 02:28:11 -07:00
import AudioManager from '../Manager/AudioManager';
2024-05-15 00:42:31 -07:00
import { EventManger } from '../Manager/EventManger';
2024-03-28 20:35:44 -07:00
import IPoolable from '../Pool/IPoolable';
2024-05-15 00:42:31 -07:00
import ObjectPool from '../Pool/ObjectPool';
2024-03-28 20:35:44 -07:00
import Utilities from '../Utilities';
2024-03-27 04:00:23 -07:00
const { ccclass, property } = _decorator;
@ccclass('BoosterBase')
2024-03-28 20:35:44 -07:00
export class BoosterBase extends Component implements IPoolable {
2024-03-27 04:00:23 -07:00
@property({ type: Collider2D, visible: true })
2024-03-28 20:35:44 -07:00
protected _collider: Collider2D;
2024-04-24 01:52:31 -07:00
@property({ type: AudioClip, visible: true })
protected _collectSound: AudioClip;
2024-03-28 20:35:44 -07:00
@property({ type: Animation, visible: true })
private _animation: Animation;
2024-05-07 03:20:37 -07:00
@property(CCString)
public readonly displayName: string = 'CHEESE';
2024-03-27 04:00:23 -07:00
@property(CCFloat)
protected time: number = 10;
2024-05-15 00:42:31 -07:00
private _enabled: boolean = true;
2024-03-27 04:00:23 -07:00
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
2024-05-15 00:42:31 -07:00
this._enabled = false;
2024-03-27 04:00:23 -07:00
}
private onContactBegin(self: Collider2D, other: Collider2D) {
2024-05-15 00:42:31 -07:00
if (!this._enabled) return;
2024-03-28 20:35:44 -07:00
this.boosterActive();
2024-05-15 00:42:31 -07:00
this._enabled = false;
2024-05-27 02:28:11 -07:00
AudioManager.playSfx(this._collectSound);
2024-03-27 04:00:23 -07:00
EventManger.instance.emit(GameEvent.ObjectRelease, this.node);
ObjectPool.release(this.node);
}
2024-03-28 20:35:44 -07:00
protected boosterActive?(): void;
async onGet() {
this._animation.play();
await Utilities.delay(this._animation.defaultClip.duration);
2024-05-15 00:42:31 -07:00
this._enabled = true;
2024-03-28 20:35:44 -07:00
}
2024-04-04 04:27:04 -07:00
onRelease() {
2024-05-15 00:42:31 -07:00
this._enabled = false;
2024-04-04 04:27:04 -07:00
}
2024-03-27 04:00:23 -07:00
}