import { _decorator, AudioClip, AudioSource, Component, director, instantiate, Label, Node, Prefab, resources, } from "cc"; import Common from "../libs/Common"; import Utils from "../libs/Utils"; import { GameStorageKeyConfig } from "../config/Config"; import { GlobalData } from "../config/GlobalData"; import { MoodListItem } from "../ui/MoodListItem"; import AudioEffect from "../libs/AudioEffect"; import { EventDispatcher } from "../libs/EventDispatcher"; const { ccclass, property } = _decorator; @ccclass("MoodSceneControl") export class MoodSceneControl extends Component { // 列表项预制体 @property(Prefab) moodItemPrefab: Prefab = null; // 内容列表父节点(挂载点) @property(Node) contentNode: Node = null; // 总的金币数标签 Label @property(Label) totalScoreLabel: Label = null; // 按钮组节点 @property(Node) buttonGroupNode: Node = null; // 保存当前用户临时选择的音乐 id selectedMusicId: number = 0; // 音频播放器 private audioSource: AudioSource = null; // 标记有没有点击过按钮 private isClickedButton: boolean = false; //歌曲列表 musicList: any[] = [ { id: 1, title: "Like A Dino!", bestScore: 0, isLocked: 0, unlockGold: 0, }, { id: 2, title: "It's Ok,Not To Be Ok!", bestScore: 0, isLocked: 1, unlockGold: 1000, }, { id: 3, title: "Good Luck Today!", bestScore: 0, isLocked: 1, unlockGold: 2000, }, { id: 4, title: "A Piece Of Cake!", bestScore: 0, isLocked: 1, unlockGold: 3000, }, { id: 5, title: "Im So Excited!", bestScore: 0, isLocked: 1, unlockGold: 4000, }, { id: 6, title: "It Is What It Is!", bestScore: 0, isLocked: 1, unlockGold: 5000, }, { id: 7, title: "A Silver Lining!", bestScore: 0, isLocked: 1, unlockGold: 6000, }, { id: 8, title: "So Suβ!", bestScore: 0, isLocked: 1, unlockGold: 7000, }, { id: 9, title: "You Made My Day!", bestScore: 0, isLocked: 1, unlockGold: 8000, }, ]; onLoad() { // 初始化音频播放器 this.audioSource = this.getComponent(AudioSource); // 初始化音乐了列表解锁状态 this.initMusicListLockStatus(); } start() { // 监听选中音乐列表某一个 item 事件 EventDispatcher.getTarget().on( EventDispatcher.SELECT_MOOD_ITEM, this.refedButtonDisplay, this, ); // 设置页面的背景颜色 Common.setPageBackgroundColorAsDino(this.node); // 设置总的金币数标签 this.setTotalScoreLabel(); // 初始化音乐列表 this.addMoodListItems(); } // 设置总的金币数标签 setTotalScoreLabel() { this.totalScoreLabel.string = GlobalData.totalScore.toString(); } initMusicListLockStatus() { // 获取每个歌曲的最佳成绩 const bestScoreMap: any = Utils.getCache(GameStorageKeyConfig.BestScore) || {}; // 默认解锁第一条音乐 Common.unlockMusic(1); // 遍历音乐列表 this.musicList.forEach((item) => { // 更新解锁信息 item.isLocked = Common.isMusicUnlocked(item.id) ? 0 : 1; // 更新最佳成绩 item.bestScore = bestScoreMap[item.id] || 0; }); } // 初始化音乐列表 addMoodListItems() { // 遍历音乐列表 this.musicList.forEach((item) => { // 实例化列表项 const moodItem = instantiate(this.moodItemPrefab); // 初始化列表项节点 moodItem.getComponent(MoodListItem).init(item); // 挂载到内容列表父节点 moodItem.parent = this.contentNode; }); } refedButtonDisplay(musicId: number) { this.selectedMusicId = musicId; // 找到当前选中的音乐 const musicItem = this.musicList.find((item) => item.id == musicId); if (!musicItem) return; // 判断音乐是否解锁 if (musicItem.isLocked) { // 未解锁 this.buttonGroupNode.getChildByName("button_buy").active = true; this.buttonGroupNode.getChildByName("button_cancel").active = true; this.buttonGroupNode.getChildByName("button_ok").active = false; // 设置购买的金额 this.buttonGroupNode .getChildByName("button_buy") .getChildByName("gold_num") .getComponent(Label).string = musicItem.unlockGold.toString() ?? "0"; } else { // 已解锁 this.buttonGroupNode.getChildByName("button_buy").active = false; this.buttonGroupNode.getChildByName("button_cancel").active = false; this.buttonGroupNode.getChildByName("button_ok").active = true; } // 播放音乐 this.playMusic(); } // 播放音乐 playMusic() { // 判断有没有临时选择的音乐 id if (!this.selectedMusicId) return; // 获取音乐文件路径 const musicPath = "audios/music/music_" + this.selectedMusicId; // 播放音乐 if (this.audioSource) { resources.load(musicPath, (err, clip: AudioClip) => { if (err) { console.error("加载音乐失败", err, musicPath); return; } // 停止播放 防止其他音乐在播放 this.audioSource.stop(); this.audioSource.clip = clip; this.audioSource.play(); // 设置声音高度 this.audioSource.volume = 1; }); } } // 三个按钮方法 // 回到首页 onBackToHome() { // 播放点击音效 AudioEffect.playClickAudio(); // 加载首页 director.loadScene("home"); } // 购买音乐 onBuyMusic() { // 判断有没有点击过按钮 if (this.isClickedButton) return; this.isClickedButton = true; this.scheduleOnce(() => { this.isClickedButton = false; }, 0.2); // 获取当前选择的音乐 const musicInfo = this.musicList.find( (item) => item.id === this.selectedMusicId, ); if (!musicInfo) { return; } // 当前金币数如果大于所需要的金币数 if (GlobalData.totalScore >= musicInfo.unlockGold) { // 扣除所需要的金币数 GlobalData.totalScore -= musicInfo.unlockGold; // 更新总的金币数标签 this.setTotalScoreLabel(); // 解锁当前音乐 EventDispatcher.getTarget().emit( EventDispatcher.UNLOCK_MOOD_ITEM, this.selectedMusicId, ); } else { // 发送提示消息 EventDispatcher.getTarget().emit(EventDispatcher.TIP_MSG, "金币不足"); } } // 确认购买 onConfirmBuyMusic() { // 播放点击音效 AudioEffect.playClickAudio(); // 设置当前选择音乐的 id if (this.selectedMusicId > 0) { GlobalData.selectedMusicID = this.selectedMusicId; // 设置音乐信息 GlobalData.currentMusicInfo = this.musicList[this.selectedMusicId - 1]; } // 回到首页 this.scheduleOnce(() => { director.loadScene("home"); }, 0.4); } update(deltaTime: number) {} }