import { _decorator, Animation, AudioClip, CCFloat, CCString, Collider2D, Component, Contact2DType, Node, Sprite, Vec3, } from 'cc'; import BoosterType from '../Enum/BoosterType'; import GameEvent from '../Events/GameEvent'; import AudioManager from '../Manager/AudioManager'; import { EventManger } from '../Manager/EventManger'; import { GameManager } from '../Manager/GameManager'; import IPoolable from '../Pool/IPoolable'; import ObjectPool from '../Pool/ObjectPool'; import Utils from '../Utilities'; const { ccclass, property } = _decorator; @ccclass('BoosterBase') export class BoosterBase extends Component implements IPoolable { @property({ type: Collider2D, visible: true }) protected _collider: Collider2D; @property({ type: AudioClip, visible: true }) protected _collectSound: AudioClip; @property({ type: Animation, visible: true }) private _animation: Animation; @property(Sprite) private sprite: Sprite; @property(CCString) public readonly displayName: string = 'CHEESE'; @property(CCFloat) protected duration: number = 10; public readonly type: BoosterType; private _timer: number = 0; private _activeCollider: boolean = true; private _active: boolean = false; public get active() { return this._active; } protected onLoad(): void { this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this); this._activeCollider = false; } private onContactBegin(self: Collider2D, other: Collider2D) { if (!this._activeCollider) return; this._activeCollider = false; AudioManager.playSfx(this._collectSound); EventManger.instance.emit(GameEvent.ObjectRelease, this.node); GameManager.instance.addBooster(this); } public tick(dt: number) { if (!this._active) return; this.node.setPosition(Vec3.ZERO); this._timer += dt; if (this._timer >= this.duration) { this._active = false; } } public resetTime() { this._timer = 0; } public collect(collector: Node) { this._active = true; this._timer = 0; this.sprite.setNodeActive(false); this.node.setParent(collector); } public end() { if (!this.node.releaseToPool()) { this.node.destroy(); } } public dispose() { if (!this.node.releaseToPool()) { this.node.destroy(); } } async onGet() { this._animation.play(); await Utils.delay(this._animation.defaultClip.duration); this._activeCollider = true; this.sprite.setNodeActive(true); } onRelease() { this._activeCollider = false; } }