pinball/assets/_Game/Scripts/GamePlay/MutilBall.ts

127 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-06-07 01:45:52 -07:00
import {
_decorator,
AudioClip,
Collider2D,
Color,
Component,
Contact2DType,
Node,
ParticleSystem,
PolygonCollider2D,
Prefab,
Vec2,
} from 'cc';
2024-03-12 01:50:54 -07:00
import TimeConfig from '../Enum/TimeConfig';
2024-03-28 20:35:44 -07:00
import { CameraController } from '../Environments/CameraController';
2024-06-12 04:48:19 -07:00
import GameEvent from '../Events/GameEvent';
2024-06-16 19:17:36 -07:00
import registerGizmos from '../Gizmos/Decorator';
2024-06-07 01:45:52 -07:00
import Gizmos2D from '../Gizmos/Gizmos2D';
2024-05-27 02:19:31 -07:00
import AudioManager from '../Manager/AudioManager';
2024-06-12 04:48:19 -07:00
import { EventManger } from '../Manager/EventManger';
2024-05-15 00:42:31 -07:00
import { GameManager } from '../Manager/GameManager';
import ObjectPool from '../Pool/ObjectPool';
2024-06-09 05:12:08 -07:00
import Utils from '../Utilities';
2024-05-15 00:42:31 -07:00
import { Ball } from './Ball';
2024-03-10 03:12:55 -07:00
const { ccclass, property } = _decorator;
2024-06-07 22:39:41 -07:00
@registerGizmos
2024-03-10 03:12:55 -07:00
@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
private _colliderEnabled: boolean = true;
2024-05-15 00:42:31 -07:00
onDrawGizmos(): void {
2024-06-07 04:27:38 -07:00
Gizmos2D.beginColor(this.node, new Color(0, 255, 0, 150));
2024-06-07 01:45:52 -07:00
const points = (this._collider as PolygonCollider2D).points.map((p) => p.clone().add(this._collider.offset));
Gizmos2D.beginLocalPosition(this.node);
Gizmos2D.drawSolidPolygon(this.node, points);
Gizmos2D.endLocalPosition(this.node);
2024-06-07 04:27:38 -07:00
Gizmos2D.beginColor(this.node, new Color(0, 0, 255, 150));
2024-06-07 01:45:52 -07:00
Gizmos2D.drawSolidEllipse(this.node, this._portLeft.worldPosition, 20, 30);
Gizmos2D.drawSolidEllipse(this.node, this._portRight.worldPosition, 20, 30);
}
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);
2024-06-12 04:48:19 -07:00
EventManger.instance.on(GameEvent.SpawnMultiBall, this.spawnBall, this);
2024-03-10 03:12:55 -07:00
}
protected lateUpdate(dt: number): void {
2024-03-12 01:50:54 -07:00
if (this._trigged) {
CameraController.instance.shake(0.3);
2024-03-12 01:50:54 -07:00
const ball1 = this._originBall;
2024-05-15 00:42:31 -07:00
ball1.clearRigiState(false);
2024-04-03 02:43:18 -07:00
const ball2 = GameManager.instance.spawnBall(false, false);
2024-05-15 00:42:31 -07:00
ball2.clearRigiState(false);
2024-03-10 03:12:55 -07:00
ball1.node.setWorldPosition(this._portRight.worldPosition);
2024-05-15 00:42:31 -07:00
ball1.clearRigiState(true);
2024-06-12 04:48:19 -07:00
ball1.addForce(new Vec2(50, 0));
2024-03-10 03:12:55 -07:00
ball2.node.setWorldPosition(this._portLeft.worldPosition);
2024-05-15 00:42:31 -07:00
ball2.clearRigiState(true);
2024-06-12 04:48:19 -07:00
ball2.addForce(new Vec2(-50, 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
}
}
2024-06-12 04:48:19 -07:00
public async spawnBall(quantity: number) {
CameraController.instance.shake(0.3);
2024-06-12 04:48:19 -07:00
this._colliderEnabled = false;
for (let i = 0; i < quantity; i++) {
const ball = GameManager.instance.spawnBall(false, false);
if (i % 2 == 0) {
ball.node.setWorldPosition(this._portRight.worldPosition);
ball.addForce(new Vec2(40, 0));
} else {
ball.node.setWorldPosition(this._portLeft.worldPosition);
ball.addForce(new Vec2(-40, 0));
}
}
const fx = this._fxPool.get(ParticleSystem, GameManager.instance.topContainer);
const pos = this.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
AudioManager.playSfx(this._soundFX);
await Utils.delay(TimeConfig.DelayMultiBall);
this._colliderEnabled = true;
await Utils.waitUntil(() => fx.isStopped);
this._fxPool.release(fx);
}
2024-03-10 03:12:55 -07:00
private async onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
if (!this._colliderEnabled) return;
2024-03-12 01:50:54 -07:00
if (this._trigged) return;
this._colliderEnabled = false;
2024-03-12 01:50:54 -07:00
this._originBall = otherCollider.getComponent(Ball);
this._trigged = true;
2024-05-15 00:42:31 -07:00
const fx = this._fxPool.get(ParticleSystem, GameManager.instance.topContainer);
const pos = this.node.getWorldPosition();
pos.z = 10;
fx.node.setWorldPosition(pos);
2024-05-27 02:19:31 -07:00
AudioManager.playSfx(this._soundFX);
2024-06-09 05:12:08 -07:00
await Utils.delay(TimeConfig.DelayMultiBall);
this._colliderEnabled = true;
2024-06-09 05:12:08 -07:00
await Utils.waitUntil(() => fx.isStopped);
2024-04-03 02:43:18 -07:00
this._fxPool.release(fx);
2024-03-10 03:12:55 -07:00
}
}