pinball/assets/_Game/Scripts/Gameplay/Flipper.ts

128 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-03-06 03:09:17 -08:00
import {
_decorator,
Component,
HingeJoint2D,
input,
CCFloat,
Input,
EventKeyboard,
KeyCode,
Enum,
2024-03-28 20:35:44 -07:00
Animation,
Vec3,
AnimationState,
2024-03-06 03:09:17 -08:00
} from 'cc';
2024-03-28 20:35:44 -07:00
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import TimeConfig from '../Enum/TimeConfig';
2024-02-27 18:19:33 -08:00
const { ccclass, property } = _decorator;
2024-03-28 20:35:44 -07:00
export enum ControllerSide {
2024-03-06 03:09:17 -08:00
Left,
Right,
}
Enum(ControllerSide);
2024-02-27 18:19:33 -08:00
@ccclass('Flipper')
export class Flipper extends Component {
2024-03-28 20:35:44 -07:00
@property({ type: Animation, visible: true })
private _animation: Animation;
2024-02-27 18:19:33 -08:00
@property({ visible: true, type: HingeJoint2D })
2024-03-06 03:09:17 -08:00
private _hingeJoint: HingeJoint2D;
2024-02-27 18:19:33 -08:00
@property({ visible: true, type: CCFloat })
2024-03-06 03:09:17 -08:00
private _motorSpeedActive: number;
2024-02-27 18:19:33 -08:00
@property({ visible: true, type: CCFloat })
2024-03-06 03:09:17 -08:00
private _motorSpeedDeActive: number;
@property({ visible: true, type: ControllerSide })
private side = ControllerSide.Left;
2024-03-28 20:35:44 -07:00
private _timer = 0;
private _isAnimationPlaying;
2024-03-06 03:09:17 -08:00
protected onLoad(): void {
input.on(Input.EventType.KEY_DOWN, this.onKeyInputDown, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUp, this);
2024-03-28 20:35:44 -07:00
EventManger.instance.on(GameEvent.ControlTouchStart, this.onTouchStart, this);
EventManger.instance.on(GameEvent.ControlTouchEnd, this.onTouchEnd, this);
}
protected start(): void {
this._animation.play();
}
protected update(dt: number): void {
this._timer += dt;
this._isAnimationPlaying = this._animation.getState(this._animation.defaultClip.name).isPlaying;
if (!this._isAnimationPlaying && this._timer >= TimeConfig.Tutorial) {
this._animation.play();
}
2024-03-06 03:09:17 -08:00
}
//#region Input Handler
private onKeyInputDown(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
if (this.side == ControllerSide.Left) this.activeFlipper();
2024-03-28 20:35:44 -07:00
this._timer = 0;
if (this._animation.getState(this._animation.defaultClip.name).isPlaying) {
this._animation.stop();
}
2024-03-06 03:09:17 -08:00
break;
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
if (this.side == ControllerSide.Right) this.activeFlipper();
2024-03-28 20:35:44 -07:00
this._timer = 0;
if (this._isAnimationPlaying) {
this._animation.stop();
}
2024-03-06 03:09:17 -08:00
break;
default:
break;
}
}
private onKeyInputUp(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
if (this.side == ControllerSide.Left) this.deActiveFlipper();
break;
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
if (this.side == ControllerSide.Right) this.deActiveFlipper();
break;
default:
break;
}
}
2024-03-28 20:35:44 -07:00
private onTouchStart(controllerSide: ControllerSide) {
if (this.side == controllerSide) {
this.activeFlipper();
}
this.node.setScale(Vec3.ONE);
this._timer = 0;
if (this._isAnimationPlaying) {
this._animation.stop();
2024-03-06 03:09:17 -08:00
}
}
2024-03-28 20:35:44 -07:00
private onTouchEnd(controllerSide: ControllerSide) {
if (this.side == controllerSide) {
this.deActiveFlipper();
2024-03-06 03:09:17 -08:00
}
}
//#endregion
2024-02-28 03:25:11 -08:00
2024-03-06 03:09:17 -08:00
private activeFlipper(): void {
2024-02-27 18:19:33 -08:00
this._hingeJoint.motorSpeed = this._motorSpeedActive;
}
2024-03-06 03:09:17 -08:00
private deActiveFlipper(): void {
2024-02-27 18:19:33 -08:00
this._hingeJoint.motorSpeed = this._motorSpeedDeActive;
}
}