GameInfoView.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import CacheMgr from "../../Common/manage/CacheMgr";
  2. import LayerPanel, {UrlInfo} from "../../Common/manage/Layer/LayerPanel";
  3. import Global from "../../Common/Global";
  4. import ShortageView from "./ShortageView";
  5. import PanelMgr, {Layer} from "../../Common/manage/PanelMgr";
  6. import property = cc._decorator.property;
  7. import Tools from "../../Common/Tools";
  8. import tween = cc.tween;
  9. const {ccclass} = cc._decorator;
  10. @ccclass
  11. export default class GameInfoView extends LayerPanel {
  12. public static getUrl(): UrlInfo {
  13. return {
  14. bundle: "gameInfoView",
  15. name: "gameInfoView"
  16. }
  17. }
  18. @property(cc.Prefab)
  19. private fly_gold: cc.Prefab = null //执行飞金币动画的图片
  20. @property(cc.Integer)
  21. private boomTime: number = 0.5 //金币爆开时长
  22. @property(cc.Integer)
  23. private flyTime: number = 1 //金币飞行时间
  24. @property(cc.Integer)
  25. private radius_min: number = 50 //最小半径
  26. @property(cc.Integer)
  27. private radius_max: number = 100 //最大半径
  28. @property(cc.Integer)
  29. private fly_gold_min: number = 4
  30. @property(cc.Integer)
  31. private fly_gold_max: number = 10
  32. @property(cc.Integer)
  33. private gold_scale_min: number = 0.9
  34. @property(cc.Integer)
  35. private gold_scale_max: number = 1.2
  36. private goldPool: cc.NodePool = new cc.NodePool() //
  37. private fly_end: cc.Vec3 = null
  38. private gold: cc.Node = null;
  39. private diamond: cc.Node = null;
  40. private gold_add_button: cc.Node = null;
  41. private animationTime: number = null;
  42. private gold_num: number = null;
  43. private diamond_num: number = 0;
  44. private timeouts: Map<string, number[]> = new Map<string, number[]>();
  45. private static gameInfoViewIns: GameInfoView = null;
  46. public static INS(): GameInfoView {
  47. return this.gameInfoViewIns;
  48. }
  49. initUI(): void {
  50. console.log('zh:gameInfoView initUI')
  51. GameInfoView.gameInfoViewIns = this;
  52. this.gold_num = CacheMgr.gold;
  53. this.diamond_num = CacheMgr.diamond;
  54. this.animationTime = Global.config.gameInfo.animation;
  55. this.gold = this.getNode("gold/num");
  56. this.gold_add_button = this.getNode("gold/add");
  57. this.timeouts.set("gold", []);
  58. this.timeouts.set("diamond", []);
  59. let gold = this.getNode("gold")
  60. let gold_icon = this.getNode("gold/icon")
  61. this.initPool()
  62. this.scheduleOnce(() => {
  63. let wp = gold.convertToWorldSpaceAR(gold_icon.position)
  64. this.fly_end = this.node.convertToNodeSpaceAR(wp)
  65. }, 0)
  66. }
  67. show(param: any): void {
  68. this.gold.getComponent(cc.Label).string = this.gold_num.toString();
  69. this.onTouch(this.gold_add_button, () => {
  70. PanelMgr.INS.openPanel({
  71. panel: ShortageView,
  72. layer: Layer.gameLayer,
  73. param: {
  74. type: "gold",
  75. }
  76. })
  77. });
  78. }
  79. hide() {
  80. }
  81. //初始化节点池
  82. private initPool() {
  83. if (this.fly_gold) {
  84. for (let i = 0; i < this.fly_gold_max; i++) {
  85. let gold = cc.instantiate(this.fly_gold)
  86. this.goldPool.put(gold)
  87. }
  88. }
  89. }
  90. /**
  91. * 监听金币是否改变
  92. * @param dt
  93. * @protected
  94. */
  95. protected update(dt: number) {
  96. let newGold = CacheMgr.gold;
  97. if (this.gold_num != newGold) {
  98. this.changeAnimation("gold", newGold - this.gold_num);
  99. }
  100. }
  101. /**
  102. * 修改金币动画
  103. * @param type
  104. * @param num
  105. * @private
  106. */
  107. private changeAnimation(type: string, num: number) {
  108. this.clearTimeOut(type);
  109. let num_bas = Math.abs(num);
  110. let time = this.animationTime / num_bas;
  111. let allTime = 0; //累计耗时间
  112. let num_ = this[type + "_num"];
  113. this[type + "_num"] += num;
  114. for (let i = 1; i <= num_bas; i++) {
  115. if (num < 0) {
  116. let arr = this.timeouts.get(type);
  117. arr[i] = window.setTimeout(() => {
  118. this[type].getComponent(cc.Label).string = (num_ - i).toString();
  119. }, allTime * 1000);
  120. allTime += time;
  121. } else {
  122. let arr = this.timeouts.get(type);
  123. arr[i] = window.setTimeout(() => {
  124. this[type].getComponent(cc.Label).string = (num_ + i).toString();
  125. }, allTime * 1000);
  126. allTime += time;
  127. }
  128. }
  129. }
  130. /**
  131. * 清空所有动画
  132. * @param type
  133. * @private
  134. */
  135. private clearTimeOut(type: string) {
  136. //停止所有关于 该类型改变的值
  137. let timeouts = this.timeouts.get(type);
  138. for (let i = 0; i < timeouts.length; i++) {
  139. if (this.timeouts[i]) {
  140. window.clearTimeout(timeouts[i]);
  141. }
  142. }
  143. this[type].getComponent(cc.Label).string = this[type + "_num"].toString(); //直接赋值
  144. this.timeouts.set(type, []);
  145. }
  146. /**
  147. * @param point 需要飞金币的起始点(世界坐标)
  148. * @private
  149. */
  150. public fly_gold_animation(pointPosition: cc.Vec3) {
  151. pointPosition = this.node.convertToNodeSpaceAR(pointPosition)
  152. return new Promise((resolve, reject) => {
  153. let num = Tools.getRandom(this.fly_gold_min, this.fly_gold_max - 1)
  154. if (num > this.goldPool.size()) {
  155. resolve(true)
  156. return
  157. }
  158. let p: any [] = []
  159. for (let i = 0; i < num; i++) {
  160. let pro = new Promise((resolve, reject) => {
  161. let gold = this.goldPool.get()
  162. gold.position = cc.v3(pointPosition)
  163. this.node.addChild(gold)
  164. let scale = Tools.getRealRandom(this.gold_scale_min, this.gold_scale_max)
  165. gold.scale = scale
  166. //随机角度, 随机半径
  167. let angle = Tools.getRandom(0, 360)
  168. let r = Tools.getRandom(this.radius_min, this.radius_max + 1)
  169. let boomPoint = Tools.getCirclePoint(pointPosition, r, angle)
  170. tween(gold)
  171. .to(this.boomTime, {position: boomPoint}, {easing: "quadOut"})
  172. .to(this.flyTime, {position: this.fly_end}, {easing: "quadOut"})
  173. .call(() => {
  174. this.goldPool.put(gold)
  175. resolve(true)
  176. })
  177. .start()
  178. })
  179. p.push(pro)
  180. }
  181. Promise.all(p).then(() => {
  182. resolve(true)
  183. })
  184. })
  185. }
  186. }