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

29 lines
1.0 KiB
TypeScript

import { _decorator, CCInteger, Collider2D, Component, Contact2DType, Node, RigidBody2D } from 'cc';
import { GameManager } from '../Manager/GameManager';
import Utilities from '../Utilities';
import ObjectPool from '../Pool/ObjectPool';
import { Ball } from './Ball';
const { ccclass, property, float } = _decorator;
@ccclass('Goal')
export class Goal extends Component {
@property({ type: Collider2D, visible: true })
private _collider: Collider2D;
@property({ type: CCInteger, visible: true })
private _score: number;
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onContactBegin, this);
}
private async onContactBegin(selfCollider: Collider2D, otherCollider: Collider2D) {
const ball = otherCollider.getComponent(Ball);
if (ball) {
GameManager.instance.goal(this._score, ball.node.getWorldPosition());
ball.setActiveRigi(false);
await Utilities.delay(1000);
ObjectPool.release(ball.node);
}
}
}