2024-04-03 02:43:18 -07:00
|
|
|
import { _decorator, Collider2D, Component, Contact2DType, Vec2, Node, ParticleSystem, Prefab, AudioClip } from 'cc';
|
2024-03-10 03:12:55 -07:00
|
|
|
import { GameManager } from '../Manager/GameManager';
|
|
|
|
import Utilities from '../Utilities';
|
|
|
|
import { Ball } from './Ball';
|
2024-03-12 01:50:54 -07:00
|
|
|
import TimeConfig from '../Enum/TimeConfig';
|
2024-03-28 20:35:44 -07:00
|
|
|
import ObjectPool from '../Pool/ObjectPool';
|
|
|
|
import { CameraController } from '../Environments/CameraController';
|
2024-04-03 02:43:18 -07:00
|
|
|
import { SoundManager } from '../Manager/SoundManager';
|
2024-03-10 03:12:55 -07:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass('MultiBall')
|
|
|
|
export class MultiBall extends Component {
|
|
|
|
@property({ type: Collider2D, visible: true })
|
|
|
|
private _collider: Collider2D;
|
|
|
|
@property({ type: Node, visible: true })
|
|
|
|
private _portLeft;
|
|
|
|
@property({ type: Node, visible: true })
|
|
|
|
private _portRight;
|
2024-03-28 20:35:44 -07:00
|
|
|
@property({ type: Prefab, visible: true })
|
|
|
|
private _fx: Prefab;
|
2024-03-10 03:12:55 -07:00
|
|
|
|
2024-04-03 02:43:18 -07:00
|
|
|
@property({ type: AudioClip, visible: true })
|
|
|
|
private _soundFX: AudioClip;
|
|
|
|
|
2024-03-12 01:50:54 -07:00
|
|
|
private _originBall: Ball;
|
|
|
|
private _trigged = false;
|
2024-03-28 20:35:44 -07:00
|
|
|
private _fxPool: ObjectPool;
|
2024-03-10 03:12:55 -07:00
|
|
|
|
|
|
|
protected onLoad(): void {
|
2024-03-28 20:35:44 -07:00
|
|
|
this._fxPool = new ObjectPool(this._fx, 2, true);
|
2024-03-10 03:12:55 -07:00
|
|
|
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected lateUpdate(dt: number): void {
|
2024-03-12 01:50:54 -07:00
|
|
|
if (this._trigged) {
|
2024-03-28 20:35:44 -07:00
|
|
|
CameraController.instance.shake(0.2);
|
2024-03-12 01:50:54 -07:00
|
|
|
this._originBall.setActiveRigi(true);
|
|
|
|
const ball1 = this._originBall;
|
2024-04-03 02:43:18 -07:00
|
|
|
const ball2 = GameManager.instance.spawnBall(false, false);
|
2024-03-10 03:12:55 -07:00
|
|
|
ball1.node.setWorldPosition(this._portRight.worldPosition);
|
2024-03-28 20:35:44 -07:00
|
|
|
ball1.addForce(new Vec2(20, 0));
|
2024-03-10 03:12:55 -07:00
|
|
|
|
|
|
|
ball2.node.setWorldPosition(this._portLeft.worldPosition);
|
2024-03-28 20:35:44 -07:00
|
|
|
ball2.addForce(new Vec2(-20, 0));
|
2024-03-10 03:12:55 -07:00
|
|
|
|
2024-03-12 01:50:54 -07:00
|
|
|
this._trigged = false;
|
2024-03-10 03:12:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
|
2024-03-12 01:50:54 -07:00
|
|
|
if (this._trigged) return;
|
2024-03-10 03:12:55 -07:00
|
|
|
this._collider.enabled = false;
|
2024-03-12 01:50:54 -07:00
|
|
|
this._originBall = otherCollider.getComponent(Ball);
|
|
|
|
this._originBall.setActiveRigi(false);
|
|
|
|
this._trigged = true;
|
2024-04-03 02:43:18 -07:00
|
|
|
const fx = this._fxPool.get(ParticleSystem, this.node);
|
2024-03-28 20:35:44 -07:00
|
|
|
fx.node.setWorldPosition(this.node.worldPosition);
|
2024-04-03 02:43:18 -07:00
|
|
|
SoundManager.instance.playSfx(this._soundFX);
|
2024-03-12 01:50:54 -07:00
|
|
|
await Utilities.delay(TimeConfig.DelayMultiBall);
|
2024-03-10 03:12:55 -07:00
|
|
|
this._collider.enabled = true;
|
2024-03-28 20:35:44 -07:00
|
|
|
await Utilities.waitUntil(() => fx.isStopped);
|
2024-04-03 02:43:18 -07:00
|
|
|
this._fxPool.release(fx);
|
2024-03-10 03:12:55 -07:00
|
|
|
}
|
|
|
|
}
|