EndView.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import HomeView from "./HomeView";
  2. import LayerPanel, {UrlInfo} from "../../Common/manage/Layer/LayerPanel";
  3. import CacheMgr from "../../Common/manage/CacheMgr";
  4. import GameView from "./GameView";
  5. import LayerMgr from "../../Common/manage/Layer/LayerMgr";
  6. import AudioMgr from "../../Common/manage/AudioMgr";
  7. import PanelMgr, {Layer} from "../../Common/manage/PanelMgr";
  8. import ShowConfig from "../../Common/ShowConfig";
  9. import Global from "../../Common/Global";
  10. import QgBanner from "../../Common/manage/Api/QgBanner";
  11. import Emit from "../../Common/manage/Emit/Emit";
  12. import EmitData from "../../Common/manage/Emit/EmitData";
  13. const {ccclass, property} = cc._decorator;
  14. @ccclass
  15. export default class EndView extends LayerPanel {
  16. public static getUrl(): UrlInfo {
  17. return {
  18. bundle: "endView",
  19. name: "endView"
  20. }
  21. }
  22. private _paramData: any = {};
  23. private _button: cc.Node = null;
  24. @property(cc.Prefab)
  25. private coinPrefab: cc.Prefab = null
  26. coinPool: cc.NodePool = null; //星星对象金币对象池
  27. getGold: cc.Node = null
  28. getScore: cc.Node = null
  29. gameBox: cc.Node = null
  30. maxScore: cc.Node = null
  31. newMax: cc.Node = null
  32. caiDai: cc.Node = null
  33. home_btn: cc.Node = null
  34. game_btn: cc.Node = null
  35. isNewMax: boolean = false
  36. score: number = null
  37. public initUI() {
  38. //todo logic
  39. this.getGold = this.getNode("topUi/huodejinbi/getGlod")
  40. this.gameBox = this.getNode("gameBox")
  41. this.getScore = this.getNode("topUi/getNum")
  42. this.maxScore = this.getNode("history/history_num")
  43. this.newMax = this.getNode("xinjilu")
  44. this.caiDai = this.getNode("caidai")
  45. this.game_btn = this.getNode("bottomUI/continue")
  46. this.onTouch(this.game_btn, this.handle_continue)
  47. this.home_btn = this.getNode("bottomUI/home")
  48. this.onTouch(this.home_btn, this.handle_home)
  49. this.coinPool = new cc.NodePool();
  50. this.initCoinPool();
  51. }
  52. public show(param: any) {
  53. //todo 逻辑
  54. this.isNewMax = param.isNewMax;
  55. this.getScore.getComponent(cc.Label).string = param.score;
  56. this.getGold.getComponent(cc.Label).string = "+" + param.score;
  57. this.score = Number(param.score);
  58. this.maxScore.getComponent(cc.Label).string = CacheMgr.checkpoint.toString();
  59. ShowConfig.show('endConfig').then((res) => {
  60. if (Global.config.endConfig.bannerShow == 1) {
  61. QgBanner.showBanner();
  62. } else {
  63. QgBanner.hideBanner();
  64. }
  65. });
  66. }
  67. public hide() {
  68. CacheMgr.updateData();
  69. if (Global.config.gameConfig.nativeConfig.type == 2) {
  70. Emit.instance().emit(EmitData.CLOSE_NATIVE) ;
  71. }
  72. }
  73. //todo logic 方法
  74. /** 初始化金币对象池 */
  75. initCoinPool(count: number = 20) {
  76. for (let i = 0; i < count; i++) {
  77. let coin = cc.instantiate(this.coinPrefab);
  78. this.coinPool.put(coin);
  79. }
  80. }
  81. /** 播放动画 */
  82. playAnim() {
  83. AudioMgr.play("get_coins")
  84. /** 随机金币数量 */
  85. let randomCount = Math.random() * 15 + 10;
  86. /** 起始位置 */
  87. let startPos = this.node.convertToNodeSpaceAR(this.getGold.parent.parent.convertToWorldSpaceAR(this.getGold.parent.position))
  88. /** 结束位置 */
  89. let endPos = LayerMgr.gameInfoLayer.children[0].children[0].position
  90. this.playCoinFlyAnim(randomCount, cc.v2(startPos), cc.v2(endPos));
  91. }
  92. /**
  93. * 播放金币飞出动画
  94. * @param count 金币数量
  95. * @param startPos 起始位置
  96. * @param endPos 结束位置
  97. * @param r 半径
  98. */
  99. playCoinFlyAnim(count: number, startPos: cc.Vec2, endPos: cc.Vec2, r: number = 200) {
  100. //确保当前节点池有足够的金币
  101. const poolSize = this.coinPool.size();
  102. const reCreateCoinCount = poolSize > count ? 0 : count - poolSize;
  103. this.initCoinPool(reCreateCoinCount);
  104. //生成园, 并且对圆上的点进行排序
  105. let points = this.getCirclePoints(r, startPos, count);
  106. let coinNodeList = points.map(pos => {
  107. let coin = this.coinPool.get();
  108. coin.setPosition(startPos);
  109. this.node.addChild(coin);
  110. return {
  111. node: coin,
  112. startPos: startPos,
  113. mdPos: pos,
  114. endPos: endPos,
  115. /** sub 用于把字符串显示为下标 */
  116. dis: (pos as any).sub(endPos).mag()
  117. };
  118. });
  119. coinNodeList = coinNodeList.sort((a, b) => {
  120. if (a.dis - b.dis > 0) return 1;
  121. if (a.dis - b.dis < 0) return -1;
  122. return 0;
  123. })
  124. //执行金币落袋的动画
  125. coinNodeList.forEach((item, index) => {
  126. item.node.runAction(
  127. cc.sequence(
  128. cc.moveTo(0.3, item.mdPos),
  129. cc.delayTime(index * 0.01),
  130. cc.moveTo(0.5, item.endPos),
  131. cc.callFunc(() => {
  132. this.coinPool.put(item.node);
  133. })
  134. )
  135. );
  136. });
  137. }
  138. /**
  139. *
  140. * @param r 半径
  141. * @param pos 圆心坐标
  142. * @param count 等分点数量
  143. * @param randomScope 等分点的随机播动范围
  144. */
  145. getCirclePoints(r: number, pos: cc.Vec2, count: number, randomScope: number = 60): cc.Vec2[] {
  146. let points = [];
  147. //弧度
  148. let radians = (Math.PI / 180) * Math.round(360 / count);
  149. for (let i = 0; i < count; i++) {
  150. let x = pos.x + r * Math.sin(radians * i);
  151. let y = pos.y + r * Math.cos(radians * i);
  152. points.unshift(cc.v3(x + Math.random() * randomScope, y + Math.random() * randomScope, 0));
  153. }
  154. return points;
  155. }
  156. handle_home() {
  157. PanelMgr.INS.openPanel({
  158. panel : HomeView,
  159. layer : Layer.gameLayer,
  160. call : ()=>{
  161. PanelMgr.INS.closePanel(EndView) ;
  162. }
  163. })
  164. }
  165. handle_continue() {
  166. console.log('zh: hammer 继续游戏')
  167. PanelMgr.INS.openPanel({
  168. panel : GameView,
  169. layer : Layer.gameLayer,
  170. call : ()=>{
  171. console.log('zh:call handle_continue')
  172. PanelMgr.INS.closePanel(EndView) ;
  173. }
  174. })
  175. }
  176. }