import { _decorator, Animation, AudioClip, CCFloat, CCString, Collider2D, Component, Contact2DType, Node, Sprite, UIOpacity, 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 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(UIOpacity) private uiOpacity: UIOpacity; @property(Sprite) protected sprite: Sprite; @property(CCString) public readonly displayName: string = 'CHEESE'; @property(CCFloat) protected duration: number = 10; public readonly type: BoosterType; protected timer: number = 0; private _activeCollider: boolean = true; private _active: boolean = false; public get active() { return this._active; } public set activeCollider(value: boolean) { this._activeCollider = value; } 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.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, false); } public end() { if (!this.node.releaseToPool()) { this.node.destroy(); } } public dispose() { if (!this.node.releaseToPool()) { this.node.destroy(); } } public hide() { if (this.uiOpacity) { this.uiOpacity.opacity = 0; } } async onGet() { this._animation.play(); await Utils.delay(this._animation.defaultClip.duration); this._activeCollider = true; } onRelease() { this.sprite.setNodeActive(true); this._activeCollider = false; if (this.uiOpacity) { this.uiOpacity.opacity = 255; } } }