pinball/assets/_Game/Scripts/Environments/SlingShot.ts

29 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-04-03 02:43:18 -07:00
import { _decorator, Collider2D, Component, Contact2DType, Node, Animation, RigidBody2D, AudioClip } from 'cc';
2024-03-28 20:35:44 -07:00
import { CameraController } from './CameraController';
2024-04-03 02:43:18 -07:00
import { SoundManager } from '../Manager/SoundManager';
2024-03-21 01:59:08 -07:00
const { ccclass, property } = _decorator;
@ccclass('SlingShot')
export default class SlingShot extends Component {
@property({ type: Animation, visible: true })
private _animation: Animation;
@property({ type: Collider2D, visible: true })
private _collider: Collider2D;
2024-04-03 02:43:18 -07:00
@property({ type: AudioClip, visible: true })
private _soundFx: AudioClip;
2024-03-21 01:59:08 -07:00
protected onLoad(): void {
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
onBeginContact(self: Collider2D, other: Collider2D) {
this._animation.play();
2024-03-28 20:35:44 -07:00
const velocity = other.getComponent(RigidBody2D).linearVelocity.length();
2024-04-03 02:43:18 -07:00
SoundManager.instance.playSfx(this._soundFx);
if (velocity >= 40) {
CameraController.instance.shake(0.08);
}
2024-03-21 01:59:08 -07:00
}
}