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

116 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-06-10 23:38:06 -07:00
import {
_decorator,
Animation,
AudioClip,
CCFloat,
CCString,
Collider2D,
Component,
Contact2DType,
Node,
Sprite,
2024-06-12 04:48:19 -07:00
UIOpacity,
2024-06-10 23:38:06 -07:00
Vec3,
} from 'cc';
import BoosterType from '../Enum/BoosterType';
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-06-10 23:38:06 -07:00
import { GameManager } from '../Manager/GameManager';
2024-03-28 20:35:44 -07:00
import IPoolable from '../Pool/IPoolable';
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-06-12 04:48:19 -07:00
@property(UIOpacity)
private uiOpacity: UIOpacity;
2024-06-10 23:38:06 -07:00
@property(Sprite)
2024-06-12 04:48:19 -07:00
protected sprite: Sprite;
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)
2024-06-10 23:38:06 -07:00
protected duration: number = 10;
2024-03-27 04:00:23 -07:00
2024-06-10 23:38:06 -07:00
public readonly type: BoosterType;
2024-07-03 02:59:16 -07:00
protected timer: number = 0;
2024-06-10 23:38:06 -07:00
private _activeCollider: boolean = true;
private _active: boolean = false;
public get active() {
return this._active;
}
2024-05-15 00:42:31 -07:00
2024-06-12 04:48:19 -07:00
public set activeCollider(value: boolean) {
this._activeCollider = value;
}
2024-03-27 04:00:23 -07:00
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
2024-06-10 23:38:06 -07:00
this._activeCollider = false;
2024-03-27 04:00:23 -07:00
}
private onContactBegin(self: Collider2D, other: Collider2D) {
2024-06-10 23:38:06 -07:00
if (!this._activeCollider) return;
this._activeCollider = 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);
2024-06-10 23:38:06 -07:00
GameManager.instance.addBooster(this);
}
public tick(dt: number) {
if (!this._active) return;
2024-07-03 02:59:16 -07:00
this.timer += dt;
if (this.timer >= this.duration) {
2024-06-10 23:38:06 -07:00
this._active = false;
}
}
public resetTime() {
2024-07-03 02:59:16 -07:00
this.timer = 0;
2024-03-27 04:00:23 -07:00
}
2024-06-10 23:38:06 -07:00
public collect(collector: Node) {
this._active = true;
2024-07-03 02:59:16 -07:00
this.timer = 0;
2024-06-10 23:38:06 -07:00
this.sprite.setNodeActive(false);
2024-06-12 04:48:19 -07:00
this.node.setParent(collector, false);
2024-06-10 23:38:06 -07:00
}
public end() {
if (!this.node.releaseToPool()) {
this.node.destroy();
}
}
public dispose() {
if (!this.node.releaseToPool()) {
this.node.destroy();
}
}
2024-03-28 20:35:44 -07:00
2024-06-12 04:48:19 -07:00
public hide() {
if (this.uiOpacity) {
this.uiOpacity.opacity = 0;
}
}
2024-03-28 20:35:44 -07:00
async onGet() {
this._animation.play();
2024-06-09 05:12:08 -07:00
await Utils.delay(this._animation.defaultClip.duration);
2024-06-10 23:38:06 -07:00
this._activeCollider = true;
2024-03-28 20:35:44 -07:00
}
2024-06-10 23:38:06 -07:00
2024-04-04 04:27:04 -07:00
onRelease() {
2024-06-12 04:48:19 -07:00
this.sprite.setNodeActive(true);
2024-06-10 23:38:06 -07:00
this._activeCollider = false;
2024-06-12 04:48:19 -07:00
if (this.uiOpacity) {
this.uiOpacity.opacity = 255;
}
2024-04-04 04:27:04 -07:00
}
2024-03-27 04:00:23 -07:00
}