import GameConfig from "../../GameConfig"; import MainPage from "../MainPage"; import GameMgr from "../../GameMgr"; const {ccclass, property} = cc._decorator; @ccclass export default class CoverFlow extends cc.Component { @property({type: cc.Node}) content: cc.Node = null; @property({type: cc.Prefab}) song_item: cc.Prefab = null; private sample_item: cc.Node; private list_count: number; private min_scale: number = 0.8; private var_scale: number = 0.2; private space_y: number = 30; onLoad() { this.allSongRelease = GameMgr.instance.allSongRelease; this.content.height = 3000; this.sample_item = cc.instantiate(this.song_item); this.list_count = GameConfig.song_infos.length for (let i = 0; i < 3; i++) { this.addItem(GameConfig.song_infos[i], -1); } for (let i = GameConfig.song_infos.length - 1; i >= GameConfig.song_infos.length - 2; i--) { this.addItem(GameConfig.song_infos[i], 1); } } update() { let max_node = this.getMaxNode(); if (max_node.y + this.content.y < this.node.height / 2) { let id = parseInt(max_node.name); if (id == GameConfig.song_infos[0].id) { id = GameConfig.song_infos[this.list_count - 1].id; } else { let index = GameConfig.getSongInfoIndex(id); id = GameConfig.song_infos[index - 1].id; } this.addItem(GameConfig.getSongInfo(id), 1); } let min_node = this.getMinNode(); if (min_node.y + this.content.y > -(this.node.height / 2)) { let id = parseInt(min_node.name); if (id == GameConfig.song_infos[this.list_count - 1].id) { id = GameConfig.song_infos[0].id; } else { let index = GameConfig.getSongInfoIndex(id); id = GameConfig.song_infos[index + 1].id; } this.addItem(GameConfig.getSongInfo(id), -1); } for (let child of this.content.children) { if ( child.y + this.content.y > this.node.height / 2 + child.height + this.space_y || child.y + this.content.y < -this.node.height / 2 - child.height - this.space_y ) { child.destroy(); } } let item_start = {node: null, offset_y_abs: 10000}; let bounder_top_y = -(this.sample_item.height / 2); let bounder_center_y = -(this.node.height / 2); let bounder_bottom_y = -this.node.height + this.sample_item.height / 2; this.content.children.forEach((child: cc.Node) => { let view_y = child.y + this.content.y - this.node.height / 2; if (view_y > bounder_center_y && view_y <= bounder_top_y) { let rate = (bounder_top_y - view_y) / (bounder_top_y - bounder_center_y); child.setScale(this.min_scale + rate * this.var_scale); child.opacity = 155 + 100 * rate; } else if (view_y > bounder_bottom_y && view_y <= bounder_center_y) { let rate = (view_y - bounder_bottom_y) / (bounder_center_y - bounder_bottom_y); child.setScale(this.min_scale + rate * this.var_scale); child.opacity = 155 + 100 * rate; } else { child.setScale(this.min_scale); child.opacity = 155; } let offset_y_abs = Math.abs(view_y - bounder_center_y); if (offset_y_abs < item_start.offset_y_abs) { item_start.node = child; item_start.offset_y_abs = offset_y_abs; } }); for (let child of this.content.children) { if (child == item_start.node && (!child.getChildByName("photo").getChildByName("lock").active || this.allSongRelease)) { child.getChildByName("point").active = true; child.getChildByName("start").active = true; } else { child.getChildByName("point").active = false; child.getChildByName("start").active = false; } } let rankList: cc.Node[] = []; for (let child of this.content.children) { rankList.push(child); } rankList.sort((a, b) => { return a.y - b.y; }); let main_index = rankList.indexOf(item_start.node); for (let i = main_index - 1; i >= 0; i--) { let a = rankList[i + 1]; let b = rankList[i]; b.y = a.y - a.height / 2 * a.scaleY - this.space_y - b.height / 2 * b.scaleY; } for (let i = main_index + 1; i < rankList.length; i++) { let a = rankList[i - 1]; let b = rankList[i]; b.y = a.y + a.height / 2 * a.scaleY + this.space_y + b.height / 2 * b.scaleY; } } addItem(song_info: SongInfo, directionY: number) { let song_index = GameConfig.getSongInfoIndex(song_info.id); let item = cc.instantiate(this.song_item); item.name = song_info.id.toString(); if (this.content.childrenCount == 0) { item.setPosition(0, 0); } else if (directionY < 0) { item.setPosition(0, this.getMinNode().y - item.height - this.space_y); } else if (directionY > 0) { item.setPosition(0, this.getMaxNode().y + item.height + this.space_y); } item.setScale(this.min_scale); item.getChildByName("layout").getChildByName("sequence").getChildByName("label").getComponent(cc.Label).string = (song_index + 1).toString(); item.getChildByName("layout").getChildByName("song_stars").getChildByName("song").getComponent(cc.Label).string = song_info.name; let stars = item.getChildByName("layout").getChildByName("song_stars").getChildByName("stars"); for (let j = 0; j < stars.childrenCount; j++) { if (j < song_info.stars) { stars.children[j].active = true; } else { stars.children[j].active = false; } } let imgUrl = GameConfig.remote_res + song_info.id + ".jpg?v=" + GameConfig.song_res_version; cc.loader.load({url: imgUrl, type: "jpg"}, (err, texture) => { if (!err) { let photo = item.getChildByName("photo"); let photo_size = photo.getContentSize(); photo.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture); photo.setContentSize(photo_size); } }); item.getChildByName("start").on(cc.Node.EventType.TOUCH_END, () => { MainPage.instance.startGame(song_info.id); }); //第一首歌默认解锁,后面的歌解锁要求上一首歌达到要求分数 let song_lock = false; if (song_index > 0) { let last_song_id = GameConfig.song_infos[song_index - 1].id; song_lock = (GameMgr.getStorageScore(last_song_id) < GameConfig.song_unlock_need_score); } item.getChildByName("photo").getChildByName("mask").active = this.allSongRelease ? false : song_lock; item.getChildByName("photo").getChildByName("lock").active = this.allSongRelease ? false : song_lock; this.content.addChild(item); this.resetHeight(); } getMinNode() { let node: cc.Node = null; for (let child of this.content.children) { if (node == null || child.y < node.y) { node = child; } } return node; } getMaxNode() { let node: cc.Node = null; for (let child of this.content.children) { if (node == null || child.y > node.y) { node = child; } } return node; } resetHeight() { let minY = this.getMinNode().y; let maxY = this.getMaxNode().y; let bestY = Math.abs(minY) < Math.abs(maxY) ? Math.abs(maxY) : Math.abs(minY); let height = bestY * 2 + this.sample_item.height + 100; this.content.height = height; } //是否解锁所有歌曲 allSongRelease: boolean = false; tapSongLock(e: cc.Event) { this.allSongRelease = this.allSongRelease ? false : true; e.target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = this.allSongRelease ? "恢复" : "解锁"; if (this.allSongRelease) { for (let item of this.content.children) { item.getChildByName("photo").getChildByName("mask").active = false; item.getChildByName("photo").getChildByName("lock").active = false; item.getChildByName("point").active = true; item.getChildByName("start").active = true; } } else { for (let item of this.content.children) { let song_id = parseInt(item.name); let song_lock = false; let song_index = GameConfig.getSongInfoIndex(song_id); if (song_index > 0) { let last_song_id = GameConfig.song_infos[song_index - 1].id; song_lock = (GameMgr.getStorageScore(last_song_id) < GameConfig.song_unlock_need_score); item.getChildByName("photo").getChildByName("mask").active = song_lock; item.getChildByName("photo").getChildByName("lock").active = song_lock; item.getChildByName("point").active = !song_lock; item.getChildByName("start").active = !song_lock; } } } } }