import { _decorator, Collider2D, Component, Contact2DType, Vec2, Node } from 'cc'; import { GameManager } from '../Manager/GameManager'; import Utilities from '../Utilities'; import { Ball } from './Ball'; 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; private originBall: Ball; private trigged = false; protected onLoad(): void { this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); } protected lateUpdate(dt: number): void { if (this.trigged) { const ball1 = this.originBall; const ball2 = GameManager.instance.spawnBall(false); ball1.node.setWorldPosition(this._portRight.worldPosition); ball1.addForce(new Vec2(10, 0)); ball2.node.setWorldPosition(this._portLeft.worldPosition); ball2.addForce(new Vec2(-10, 0)); this.trigged = false; } } private async onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) { if (this.trigged) return; this._collider.enabled = false; this.originBall = otherCollider.getComponent(Ball); this.trigged = true; await Utilities.delay(500); this._collider.enabled = true; } }