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

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-03-21 01:59:08 -07:00
import {
_decorator,
CCInteger,
Collider2D,
Component,
Contact2DType,
EventHandler,
tween,
Vec2,
Animation,
Vec3,
2024-04-03 02:43:18 -07:00
AudioClip,
2024-03-21 01:59:08 -07:00
} 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-21 01:59:08 -07:00
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
2024-03-28 20:35:44 -07:00
import { CameraController } from '../Environments/CameraController';
2024-04-03 02:43:18 -07:00
import { SoundManager } from '../Manager/SoundManager';
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;
2024-03-21 01:59:08 -07:00
@property({ type: Animation, visible: true })
private _animation: Animation;
2024-03-08 03:07:41 -08:00
@property({ type: CCInteger, visible: true })
private _force = 30;
2024-04-03 02:43:18 -07:00
@property({ type: AudioClip, visible: true })
private _soundFx: AudioClip;
2024-03-08 03:07:41 -08:00
@property({ type: EventHandler, visible: true })
private onDone: EventHandler[] = [];
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
2024-03-21 01:59:08 -07:00
EventManger.instance.on(GameEvent.BallOut, this.onBallOut, this);
2024-03-08 03:07:41 -08:00
}
2024-04-05 01:09:19 -07:00
private async onBeginContact(self: Collider2D, other: Collider2D) {
2024-03-08 03:07:41 -08:00
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-28 20:35:44 -07:00
CameraController.instance.shake(0.2);
2024-03-21 01:59:08 -07:00
this._animation.play();
2024-03-08 03:07:41 -08:00
ball.setActiveRigi(true);
ball.throwBall(new Vec2(0, this._force));
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.DelayCannonDone);
2024-03-21 01:59:08 -07:00
tween(this._collider.node).to(0.5, { scale: Vec3.ZERO }, { easing: 'backIn' }).start();
2024-03-08 03:07:41 -08:00
EventHandler.emitEvents(this.onDone, ball);
}
}
2024-03-21 01:59:08 -07:00
private onBallOut() {
tween(this._collider.node)
.to(0.5, { scale: Vec3.ONE }, { easing: 'backOut', onComplete: () => (this._collider.enabled = true) })
.start();
}
2024-03-08 03:07:41 -08:00
}