CoverFlow.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import GameConfig from "../../GameConfig";
  2. import MainPage from "../MainPage";
  3. import GameMgr from "../../GameMgr";
  4. const {ccclass, property} = cc._decorator;
  5. @ccclass
  6. export default class CoverFlow extends cc.Component {
  7. @property({type: cc.Node})
  8. content: cc.Node = null;
  9. @property({type: cc.Prefab})
  10. song_item: cc.Prefab = null;
  11. private sample_item: cc.Node;
  12. private list_count: number;
  13. private min_scale: number = 0.8;
  14. private var_scale: number = 0.2;
  15. private space_y: number = 30;
  16. onLoad() {
  17. this.allSongRelease = GameMgr.instance.allSongRelease;
  18. this.content.height = 3000;
  19. this.sample_item = cc.instantiate(this.song_item);
  20. this.list_count = GameConfig.song_infos.length
  21. for (let i = 0; i < 3; i++) {
  22. this.addItem(GameConfig.song_infos[i], -1);
  23. }
  24. for (let i = GameConfig.song_infos.length - 1; i >= GameConfig.song_infos.length - 2; i--) {
  25. this.addItem(GameConfig.song_infos[i], 1);
  26. }
  27. }
  28. update() {
  29. let max_node = this.getMaxNode();
  30. if (max_node.y + this.content.y < this.node.height / 2) {
  31. let id = parseInt(max_node.name);
  32. if (id == GameConfig.song_infos[0].id) {
  33. id = GameConfig.song_infos[this.list_count - 1].id;
  34. } else {
  35. let index = GameConfig.getSongInfoIndex(id);
  36. id = GameConfig.song_infos[index - 1].id;
  37. }
  38. this.addItem(GameConfig.getSongInfo(id), 1);
  39. }
  40. let min_node = this.getMinNode();
  41. if (min_node.y + this.content.y > -(this.node.height / 2)) {
  42. let id = parseInt(min_node.name);
  43. if (id == GameConfig.song_infos[this.list_count - 1].id) {
  44. id = GameConfig.song_infos[0].id;
  45. } else {
  46. let index = GameConfig.getSongInfoIndex(id);
  47. id = GameConfig.song_infos[index + 1].id;
  48. }
  49. this.addItem(GameConfig.getSongInfo(id), -1);
  50. }
  51. for (let child of this.content.children) {
  52. if (
  53. child.y + this.content.y > this.node.height / 2 + child.height + this.space_y ||
  54. child.y + this.content.y < -this.node.height / 2 - child.height - this.space_y
  55. ) {
  56. child.destroy();
  57. }
  58. }
  59. let item_start = {node: null, offset_y_abs: 10000};
  60. let bounder_top_y = -(this.sample_item.height / 2);
  61. let bounder_center_y = -(this.node.height / 2);
  62. let bounder_bottom_y = -this.node.height + this.sample_item.height / 2;
  63. this.content.children.forEach((child: cc.Node) => {
  64. let view_y = child.y + this.content.y - this.node.height / 2;
  65. if (view_y > bounder_center_y && view_y <= bounder_top_y) {
  66. let rate = (bounder_top_y - view_y) / (bounder_top_y - bounder_center_y);
  67. child.setScale(this.min_scale + rate * this.var_scale);
  68. child.opacity = 155 + 100 * rate;
  69. } else if (view_y > bounder_bottom_y && view_y <= bounder_center_y) {
  70. let rate = (view_y - bounder_bottom_y) / (bounder_center_y - bounder_bottom_y);
  71. child.setScale(this.min_scale + rate * this.var_scale);
  72. child.opacity = 155 + 100 * rate;
  73. } else {
  74. child.setScale(this.min_scale);
  75. child.opacity = 155;
  76. }
  77. let offset_y_abs = Math.abs(view_y - bounder_center_y);
  78. if (offset_y_abs < item_start.offset_y_abs) {
  79. item_start.node = child;
  80. item_start.offset_y_abs = offset_y_abs;
  81. }
  82. });
  83. for (let child of this.content.children) {
  84. if (child == item_start.node && (!child.getChildByName("photo").getChildByName("lock").active || this.allSongRelease)) {
  85. child.getChildByName("point").active = true;
  86. child.getChildByName("start").active = true;
  87. } else {
  88. child.getChildByName("point").active = false;
  89. child.getChildByName("start").active = false;
  90. }
  91. }
  92. let rankList: cc.Node[] = [];
  93. for (let child of this.content.children) {
  94. rankList.push(child);
  95. }
  96. rankList.sort((a, b) => {
  97. return a.y - b.y;
  98. });
  99. let main_index = rankList.indexOf(item_start.node);
  100. for (let i = main_index - 1; i >= 0; i--) {
  101. let a = rankList[i + 1];
  102. let b = rankList[i];
  103. b.y = a.y - a.height / 2 * a.scaleY - this.space_y - b.height / 2 * b.scaleY;
  104. }
  105. for (let i = main_index + 1; i < rankList.length; i++) {
  106. let a = rankList[i - 1];
  107. let b = rankList[i];
  108. b.y = a.y + a.height / 2 * a.scaleY + this.space_y + b.height / 2 * b.scaleY;
  109. }
  110. }
  111. addItem(song_info: SongInfo, directionY: number) {
  112. let song_index = GameConfig.getSongInfoIndex(song_info.id);
  113. let item = cc.instantiate(this.song_item);
  114. item.name = song_info.id.toString();
  115. if (this.content.childrenCount == 0) {
  116. item.setPosition(0, 0);
  117. } else if (directionY < 0) {
  118. item.setPosition(0, this.getMinNode().y - item.height - this.space_y);
  119. } else if (directionY > 0) {
  120. item.setPosition(0, this.getMaxNode().y + item.height + this.space_y);
  121. }
  122. item.setScale(this.min_scale);
  123. item.getChildByName("layout").getChildByName("sequence").getChildByName("label").getComponent(cc.Label).string =
  124. (song_index + 1).toString();
  125. item.getChildByName("layout").getChildByName("song_stars").getChildByName("song").getComponent(cc.Label).string = song_info.name;
  126. let stars = item.getChildByName("layout").getChildByName("song_stars").getChildByName("stars");
  127. for (let j = 0; j < stars.childrenCount; j++) {
  128. if (j < song_info.stars) {
  129. stars.children[j].active = true;
  130. } else {
  131. stars.children[j].active = false;
  132. }
  133. }
  134. let imgUrl = GameConfig.remote_res + song_info.id + ".jpg?v=" + GameConfig.song_res_version;
  135. cc.loader.load({url: imgUrl, type: "jpg"}, (err, texture) => {
  136. if (!err) {
  137. let photo = item.getChildByName("photo");
  138. let photo_size = photo.getContentSize();
  139. photo.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
  140. photo.setContentSize(photo_size);
  141. }
  142. });
  143. item.getChildByName("start").on(cc.Node.EventType.TOUCH_END, () => {
  144. MainPage.instance.startGame(song_info.id);
  145. });
  146. //第一首歌默认解锁,后面的歌解锁要求上一首歌达到要求分数
  147. let song_lock = false;
  148. if (song_index > 0) {
  149. let last_song_id = GameConfig.song_infos[song_index - 1].id;
  150. song_lock = (GameMgr.getStorageScore(last_song_id) < GameConfig.song_unlock_need_score);
  151. }
  152. item.getChildByName("photo").getChildByName("mask").active = this.allSongRelease ? false : song_lock;
  153. item.getChildByName("photo").getChildByName("lock").active = this.allSongRelease ? false : song_lock;
  154. this.content.addChild(item);
  155. this.resetHeight();
  156. }
  157. getMinNode() {
  158. let node: cc.Node = null;
  159. for (let child of this.content.children) {
  160. if (node == null || child.y < node.y) {
  161. node = child;
  162. }
  163. }
  164. return node;
  165. }
  166. getMaxNode() {
  167. let node: cc.Node = null;
  168. for (let child of this.content.children) {
  169. if (node == null || child.y > node.y) {
  170. node = child;
  171. }
  172. }
  173. return node;
  174. }
  175. resetHeight() {
  176. let minY = this.getMinNode().y;
  177. let maxY = this.getMaxNode().y;
  178. let bestY = Math.abs(minY) < Math.abs(maxY) ? Math.abs(maxY) : Math.abs(minY);
  179. let height = bestY * 2 + this.sample_item.height + 100;
  180. this.content.height = height;
  181. }
  182. //是否解锁所有歌曲
  183. allSongRelease: boolean = false;
  184. tapSongLock(e: cc.Event) {
  185. this.allSongRelease = this.allSongRelease ? false : true;
  186. e.target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = this.allSongRelease ? "恢复" : "解锁";
  187. if (this.allSongRelease) {
  188. for (let item of this.content.children) {
  189. item.getChildByName("photo").getChildByName("mask").active = false;
  190. item.getChildByName("photo").getChildByName("lock").active = false;
  191. item.getChildByName("point").active = true;
  192. item.getChildByName("start").active = true;
  193. }
  194. } else {
  195. for (let item of this.content.children) {
  196. let song_id = parseInt(item.name);
  197. let song_lock = false;
  198. let song_index = GameConfig.getSongInfoIndex(song_id);
  199. if (song_index > 0) {
  200. let last_song_id = GameConfig.song_infos[song_index - 1].id;
  201. song_lock = (GameMgr.getStorageScore(last_song_id) < GameConfig.song_unlock_need_score);
  202. item.getChildByName("photo").getChildByName("mask").active = song_lock;
  203. item.getChildByName("photo").getChildByName("lock").active = song_lock;
  204. item.getChildByName("point").active = !song_lock;
  205. item.getChildByName("start").active = !song_lock;
  206. }
  207. }
  208. }
  209. }
  210. }