2024-03-27 04:00:23 -07:00
|
|
|
import { _decorator, Component, EventKeyboard, EventTouch, input, Input, KeyCode, Node, 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-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-03-28 20:35:44 -07:00
|
|
|
protected async start() {
|
2024-03-26 20:04:28 -07:00
|
|
|
tween(this._tapL)
|
2024-03-28 20:35:44 -07:00
|
|
|
.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' })
|
2024-03-26 20:04:28 -07:00
|
|
|
.union()
|
|
|
|
.repeatForever()
|
|
|
|
.start();
|
2024-03-28 20:35:44 -07:00
|
|
|
await Utilities.delay(0.5);
|
2024-03-26 20:04:28 -07:00
|
|
|
tween(this._tapR)
|
2024-03-28 20:35:44 -07:00
|
|
|
.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' })
|
2024-03-26 20:04:28 -07:00
|
|
|
.union()
|
|
|
|
.repeatForever()
|
|
|
|
.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-03-28 20:35:44 -07:00
|
|
|
private playTutorial() {
|
2024-03-29 04:24:58 -07:00
|
|
|
if (this._canShow) {
|
|
|
|
this._tapL.active = true;
|
|
|
|
this._tapR.active = true;
|
|
|
|
}
|
2024-03-28 20:35:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private stopTutorial() {
|
|
|
|
this._timer = 0;
|
|
|
|
this._showed = false;
|
|
|
|
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
|
|
|
}
|