Files
dino/assets/seripts/components/LineControl.ts
2026-04-30 00:13:37 +08:00

53 lines
1.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { _decorator, Collider2D, Component, Contact2DType, Node } from "cc";
import { GlobalData } from "../config/GlobalData";
import { GameState } from "../config/Config";
import { NoteControl } from "./NoteControl";
import { EventDispatcher } from "../libs/EventDispatcher";
const { ccclass, property } = _decorator;
@ccclass("LineControl")
export class LineControl extends Component {
start() {
// 监听碰撞事件
this.node
.getComponent(Collider2D)
.on(Contact2DType.BEGIN_CONTACT, this.onCollisionEnter, this);
}
onCollisionEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
// 如果游戏状态不是进行中,直接返回
if (GlobalData.gameState != GameState.PLAYING) {
return;
}
if (otherCollider.tag == 2) {
// 1. 剩余生命值减 1
GlobalData.lifeNumPerRound--;
// 2. 剩余生命值是否大于等于0
if (GlobalData.lifeNumPerRound >= 0) {
// 触发音符碰到线事件
EventDispatcher.getTarget().emit(EventDispatcher.UPDATE_LIFE);
}
// 3. 如果小于0游戏结束
if (GlobalData.lifeNumPerRound < 0) {
GlobalData.gameState = GameState.GAME_OVER;
// 调用 NoteControl 中的 pause 方法暂停音符
otherCollider.getComponent(NoteControl).noteHitLine();
// 发送游戏结束事件
EventDispatcher.getTarget().emit(EventDispatcher.GAME_OVER);
} else {
GlobalData.gameState = GameState.PAUSED;
otherCollider.getComponent(NoteControl).noteHitLine();
// 发送暂停事件
EventDispatcher.getTarget().emit(EventDispatcher.GAME_PAUSE);
}
// 4. 如果剩余生命值大于等于0继续游戏弹出暂停界面
// 线碰到了音符
}
}
update(deltaTime: number) {}
}