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

35 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-03-08 03:07:41 -08:00
import { _decorator, CCInteger, Collider2D, Component, Contact2DType, EventHandler, tween, Vec2 } from 'cc';
2024-03-10 03:12:55 -07:00
import Utilities from '../Utilities';
2024-03-10 20:46:50 -07:00
import { Ball } from './Ball';
2024-03-12 01:50:54 -07:00
import TimeConfig from '../Enum/TimeConfig';
2024-03-08 03:07:41 -08:00
const { ccclass, property } = _decorator;
@ccclass('Cannon')
export class Cannon extends Component {
@property({ type: Collider2D, visible: true })
private _collider: Collider2D;
@property({ type: CCInteger, visible: true })
private _force = 30;
@property({ type: EventHandler, visible: true })
private onDone: EventHandler[] = [];
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
private async onBeginContact(seft: Collider2D, other: Collider2D) {
const ball = other.getComponent(Ball);
this._collider.enabled = false;
if (ball) {
ball.setActiveRigi(false);
tween(ball.node).to(0.1, { worldPosition: this.node.worldPosition }).start();
2024-03-12 01:50:54 -07:00
await Utilities.delay(TimeConfig.DelayCannonFire);
2024-03-08 03:07:41 -08:00
ball.setActiveRigi(true);
ball.throwBall(new Vec2(0, this._force));
2024-03-12 01:50:54 -07:00
await Utilities.delay(TimeConfig.DelayCannonDone);
2024-03-08 03:07:41 -08:00
this._collider.enabled = true;
EventHandler.emitEvents(this.onDone, ball);
}
}
}