import { _decorator, Component, instantiate, Label, Node, Prefab, Vec3, } from "cc"; import { EventDispatcher } from "../libs/EventDispatcher"; import { GlobalData } from "../config/GlobalData"; import { DataManager } from "../libs/DataManager"; import Common from "../libs/Common"; 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; // 音符的数组 private noteArray: any[] = []; 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(); } async start() { // 获取当前关卡音符数据 this.noteArray = await Common.loadMusicNoteData(GlobalData.selectedMusicID); 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(); } // 重新生成音符 private async reGenerateNotes() { // 获取当前关卡音符数据 this.clearAllNote(); this.noteArray = await Common.loadMusicNoteData(GlobalData.selectedMusicID); this.createNotes(); this.updateNoteCount(); } // 更新音符总数的显示 updateNoteCount() { this.noteCountLabel.string = "音符数量:" + this.noteArray.length; } update(deltaTime: number) {} }