Files

105 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2026-04-30 00:13:37 +08:00
import {
_decorator,
Component,
instantiate,
Label,
Node,
Prefab,
Vec3,
} from "cc";
import { EventDispatcher } from "../libs/EventDispatcher";
import { GlobalData } from "../config/GlobalData";
2026-05-12 06:46:31 +08:00
import { DataManager } from "../libs/DataManager";
import Common from "../libs/Common";
2026-04-30 00:13:37 +08:00
const { ccclass, property } = _decorator;
@ccclass("NoteManager")
export class NoteManager extends Component {
// 音符的预制体
@property(Prefab)
private notePrefab: Prefab = null;
// 基准线的节点
@property(Node)
lineNode: Node = null;
// 定义基准线的坐标
private linePosition: Vec3 = new Vec3(0, 0, 0);
// 显示当前有多少个音符的 label
@property(Label)
noteCountLabel: Label = null;
// 音符的数组
2026-05-12 06:46:31 +08:00
private noteArray: any[] = [];
2026-04-30 00:13:37 +08:00
onLoad() {
// 监听清除所有音符事件
EventDispatcher.getTarget().on(
EventDispatcher.CLEAR_ALL_NOTE,
this.clearAllNote,
this,
);
// 监听过关事件
EventDispatcher.getTarget().on(
EventDispatcher.PASS_LEVLE,
this.reGenerateNotes,
this,
);
// 获取基准线的坐标
this.linePosition = this.lineNode.getPosition();
}
2026-05-12 06:46:31 +08:00
async start() {
// 获取当前关卡音符数据
this.noteArray = await Common.loadMusicNoteData(GlobalData.selectedMusicID);
2026-04-30 00:13:37 +08:00
this.createNotes();
this.updateNoteCount();
}
// 创建音符
private createNotes() {
for (let i = 0; i < this.noteArray.length; i++) {
const note = instantiate(this.notePrefab);
let noteX = 0;
if (this.noteArray[i].trackType == 0) {
noteX = -180;
} else if (this.noteArray[i].trackType == 1) {
noteX = 0;
} else if (this.noteArray[i].trackType == 2) {
noteX = 180;
}
const noteY =
GlobalData.noteSpeed * this.noteArray[i].timePoint +
this.linePosition.y;
note.setPosition(noteX, noteY);
this.node.addChild(note);
}
}
// 清除所有音符
private clearAllNote() {
// 清除所有音符节点
this.node.removeAllChildren();
}
// 重新生成音符
2026-05-12 06:46:31 +08:00
private async reGenerateNotes() {
// 获取当前关卡音符数据
2026-04-30 00:13:37 +08:00
this.clearAllNote();
2026-05-12 06:46:31 +08:00
this.noteArray = await Common.loadMusicNoteData(GlobalData.selectedMusicID);
2026-04-30 00:13:37 +08:00
this.createNotes();
this.updateNoteCount();
}
// 更新音符总数的显示
updateNoteCount() {
this.noteCountLabel.string = "音符数量:" + this.noteArray.length;
}
update(deltaTime: number) {}
}