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-06-09 05:12:08 -07:00
|
|
|
import Utils 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)
|
2024-04-26 02:15:53 -07:00
|
|
|
public readonly displayName: string = 'CHEESE';
|
2024-03-27 04:00:23 -07:00
|
|
|
@property(CCFloat)
|
|
|
|
protected time: number = 10;
|
|
|
|
|
2024-06-09 05:12:08 -07:00
|
|
|
private active: boolean = true;
|
2024-05-15 00:42:31 -07:00
|
|
|
|
2024-03-27 04:00:23 -07:00
|
|
|
protected onLoad(): void {
|
|
|
|
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
|
2024-06-09 05:12:08 -07:00
|
|
|
this.active = false;
|
2024-03-27 04:00:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private onContactBegin(self: Collider2D, other: Collider2D) {
|
2024-06-09 05:12:08 -07:00
|
|
|
if (!this.active) return;
|
2024-03-28 20:35:44 -07:00
|
|
|
this.boosterActive();
|
2024-06-09 05:12:08 -07:00
|
|
|
this.active = 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();
|
2024-06-09 05:12:08 -07:00
|
|
|
await Utils.delay(this._animation.defaultClip.duration);
|
|
|
|
this.active = true;
|
2024-03-28 20:35:44 -07:00
|
|
|
}
|
2024-04-04 04:27:04 -07:00
|
|
|
onRelease() {
|
2024-06-09 05:12:08 -07:00
|
|
|
this.active = false;
|
2024-04-04 04:27:04 -07:00
|
|
|
}
|
2024-03-27 04:00:23 -07:00
|
|
|
}
|