pinball/assets/_Game/Scripts/UI/TutorialController.ts

124 lines
3.7 KiB
TypeScript

import { _decorator, Component, EventKeyboard, EventTouch, input, Input, KeyCode, Node, Tween, tween, Vec3 } from 'cc';
import { GameManager } from '../Manager/GameManager';
import TimeConfig from '../Enum/TimeConfig';
import Utilities from '../Utilities';
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import GameState from '../Enum/GameState';
const { ccclass, property } = _decorator;
@ccclass('TutorialController')
export class TutorialController extends Component {
@property({ type: Node, visible: true })
private _tapL: Node;
@property({ type: Node, visible: true })
private _tapR: Node;
private _timer = 0;
private _showed = false;
private _canShow = true;
protected onLoad(): void {
this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUpStart, this);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
}
protected async start() {
tween(this._tapL)
.by(0.5, { position: new Vec3(0, 50), scale: new Vec3(-0.1, -0.1) }, { easing: 'quintOut' })
.by(0.5, { position: new Vec3(0, -50), scale: new Vec3(0.1, 0.1) }, { easing: 'sineOut' })
.union()
.repeatForever()
.start();
await Utilities.delay(0.5);
tween(this._tapR)
.by(0.5, { position: new Vec3(0, 50), scale: new Vec3(-0.1, -0.1) }, { easing: 'quintOut' })
.by(0.5, { position: new Vec3(0, -50), scale: new Vec3(0.1, 0.1) }, { easing: 'sineOut' })
.union()
.repeatForever()
.start();
this.playTutorial();
}
protected update(dt: number): void {
this._timer += dt;
if (!this._showed && this._timer > TimeConfig.Tutorial) {
this._showed = true;
this.playTutorial();
}
}
private onGameStateChange(state: GameState) {
switch (state) {
case GameState.GameOver:
case GameState.End:
this._canShow = false;
break;
default:
this._canShow = true;
break;
}
}
private playTutorial() {
if (this._canShow) {
this._tapL.active = true;
this._tapR.active = true;
}
}
private stopTutorial() {
this._timer = 0;
this._showed = false;
this._tapL.active = false;
this._tapR.active = false;
}
private startGame() {
this.stopTutorial();
this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.off(Input.EventType.KEY_UP, this.onKeyInputUpStart, this);
this.node.on(Input.EventType.TOUCH_START, this.onTouch, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUp, this);
GameManager.instance.play();
}
private onTouch(event: EventTouch) {
this.stopTutorial();
}
private onTouchStart(event: EventTouch) {
this.startGame();
}
private onKeyInputUpStart(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
this.startGame();
break;
default:
break;
}
}
private onKeyInputUp(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
this.stopTutorial();
break;
default:
break;
}
}
}