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

159 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-04-05 03:44:35 -07:00
import {
_decorator,
Component,
EventKeyboard,
EventTouch,
input,
Input,
KeyCode,
Node,
ParticleSystem,
Tween,
tween,
Vec3,
} from 'cc';
2024-03-26 20:04:28 -07:00
import { GameManager } from '../Manager/GameManager';
2024-03-28 20:35:44 -07:00
import TimeConfig from '../Enum/TimeConfig';
import Utilities from '../Utilities';
2024-03-29 04:24:58 -07:00
import { EventManger } from '../Manager/EventManger';
import GameEvent from '../Events/GameEvent';
import GameState from '../Enum/GameState';
2024-03-26 20:04:28 -07:00
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;
2024-04-05 03:44:35 -07:00
@property({ type: ParticleSystem, visible: true })
private _tapLEffect: ParticleSystem;
@property({ type: ParticleSystem, visible: true })
private _tapREffect: ParticleSystem;
2024-03-28 20:35:44 -07:00
private _timer = 0;
private _showed = false;
2024-03-29 04:24:58 -07:00
private _canShow = true;
2024-04-04 21:43:09 -07:00
private _playing = false;
2024-03-27 04:00:23 -07:00
2024-03-28 20:35:44 -07:00
protected onLoad(): void {
this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUpStart, this);
2024-03-29 04:24:58 -07:00
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
2024-03-26 20:04:28 -07:00
}
2024-04-05 03:44:35 -07:00
protected start() {
2024-03-28 20:35:44 -07:00
this.playTutorial();
}
protected update(dt: number): void {
2024-04-04 21:43:09 -07:00
if (!this._playing) return;
2024-03-28 20:35:44 -07:00
this._timer += dt;
if (!this._showed && this._timer > TimeConfig.Tutorial) {
this._showed = true;
this.playTutorial();
}
}
2024-03-29 04:24:58 -07:00
private onGameStateChange(state: GameState) {
switch (state) {
2024-04-04 21:43:09 -07:00
case GameState.Playing:
this._playing = true;
break;
2024-03-29 04:24:58 -07:00
case GameState.GameOver:
case GameState.End:
2024-04-04 21:43:09 -07:00
this._playing = false;
2024-03-29 04:24:58 -07:00
this._canShow = false;
break;
default:
this._canShow = true;
break;
}
}
2024-04-05 03:44:35 -07:00
private async playTutorial() {
2024-03-29 04:24:58 -07:00
if (this._canShow) {
this._tapL.active = true;
this._tapR.active = true;
2024-04-24 19:17:42 -07:00
this._tapLEffect.node.active = true;
this._tapREffect.node.active = true;
2024-04-05 03:44:35 -07:00
tween(this._tapL)
.call(() => this._tapLEffect.clear())
.to(0.5, { position: new Vec3(-470, -500), scale: new Vec3(0.9, 0.9) }, { easing: 'quintOut' })
.to(0.5, { position: new Vec3(-470, -550), scale: Vec3.ONE }, { easing: 'sineOut' })
2024-04-05 03:44:35 -07:00
.call(() => this._tapLEffect.play())
.union()
.repeatForever()
.start();
await Utilities.delay(0.5);
tween(this._tapR)
.call(() => this._tapREffect.clear())
.to(0.5, { position: new Vec3(470, -500), scale: new Vec3(0.9, 0.9) }, { easing: 'quintOut' })
.to(0.5, { position: new Vec3(470, -550), scale: Vec3.ONE }, { easing: 'sineOut' })
2024-04-05 03:44:35 -07:00
.call(() => this._tapREffect.play())
.union()
.repeatForever()
.start();
2024-03-29 04:24:58 -07:00
}
2024-03-28 20:35:44 -07:00
}
private stopTutorial() {
2024-04-05 03:44:35 -07:00
Tween.stopAllByTarget(this._tapL);
Tween.stopAllByTarget(this._tapR);
2024-03-28 20:35:44 -07:00
this._timer = 0;
this._showed = false;
2024-04-24 19:17:42 -07:00
this._tapLEffect.node.active = false;
this._tapREffect.node.active = false;
2024-03-28 20:35:44 -07:00
this._tapL.active = false;
this._tapR.active = false;
2024-03-26 20:04:28 -07:00
}
2024-03-27 04:00:23 -07:00
private startGame() {
2024-03-28 20:35:44 -07:00
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);
2024-03-26 20:04:28 -07:00
GameManager.instance.play();
}
2024-03-27 04:00:23 -07:00
private onTouch(event: EventTouch) {
2024-03-28 20:35:44 -07:00
this.stopTutorial();
}
private onTouchStart(event: EventTouch) {
2024-03-27 04:00:23 -07:00
this.startGame();
}
2024-03-28 20:35:44 -07:00
private onKeyInputUpStart(event: EventKeyboard) {
2024-03-27 04:00:23 -07:00
switch (event.keyCode) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
this.startGame();
break;
default:
break;
}
}
2024-03-28 20:35:44 -07:00
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;
}
}
2024-03-26 20:04:28 -07:00
}