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

46 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-04-03 02:43:18 -07:00
import { _decorator, CCFloat, Collider2D, Component, Contact2DType, Animation, AudioClip } from 'cc';
2024-03-27 04:00:23 -07:00
import ObjectPool from '../Pool/ObjectPool';
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
2024-03-28 20:35:44 -07:00
import IPoolable from '../Pool/IPoolable';
import Utilities from '../Utilities';
2024-04-03 02:43:18 -07:00
import { SoundManager } from '../Manager/SoundManager';
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-04-03 02:43:18 -07:00
2024-03-27 04:00:23 -07:00
@property(CCFloat)
protected time: number = 10;
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
2024-04-04 04:27:04 -07:00
this._collider.enabled = false;
2024-03-27 04:00:23 -07:00
}
private onContactBegin(self: Collider2D, other: Collider2D) {
2024-03-28 20:35:44 -07:00
this.boosterActive();
2024-04-04 04:27:04 -07:00
this._collider.enabled = false;
2024-04-24 01:52:31 -07:00
SoundManager.instance.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);
this._collider.enabled = true;
}
2024-04-04 04:27:04 -07:00
onRelease() {
this._collider.enabled = false;
}
2024-03-27 04:00:23 -07:00
}