GameInfoView.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. GameInfoView.gameInfoViewIns = this;
  51. this.gold_num = CacheMgr.gold;
  52. this.diamond_num = CacheMgr.diamond;
  53. this.animationTime = Global.config.gameInfo.animation;
  54. this.gold = this.getNode("gold/num");
  55. this.gold_add_button = this.getNode("gold/add");
  56. this.timeouts.set("gold", []);
  57. this.timeouts.set("diamond", []);
  58. let gold = this.getNode("gold")
  59. let gold_icon = this.getNode("gold/icon")
  60. this.initPool()
  61. this.scheduleOnce(() => {
  62. let wp = gold.convertToWorldSpaceAR(gold_icon.position)
  63. this.fly_end = this.node.convertToNodeSpaceAR(wp)
  64. }, 0)
  65. }
  66. show(param: any): void {
  67. this.gold.getComponent(cc.Label).string = this.gold_num.toString();
  68. this.onTouch(this.gold_add_button, () => {
  69. PanelMgr.INS.openPanel({
  70. panel: ShortageView,
  71. layer: Layer.gameLayer,
  72. param: {
  73. type: "gold",
  74. }
  75. })
  76. });
  77. }
  78. hide() {
  79. }
  80. //初始化节点池
  81. private initPool() {
  82. if (this.fly_gold) {
  83. for (let i = 0; i < this.fly_gold_max; i++) {
  84. let gold = cc.instantiate(this.fly_gold)
  85. this.goldPool.put(gold)
  86. }
  87. }
  88. }
  89. /**
  90. * 监听金币是否改变
  91. * @param dt
  92. * @protected
  93. */
  94. protected update(dt: number) {
  95. let newGold = CacheMgr.gold;
  96. if (this.gold_num != newGold) {
  97. this.changeAnimation("gold", newGold - this.gold_num);
  98. }
  99. }
  100. /**
  101. * 修改金币动画
  102. * @param type
  103. * @param num
  104. * @private
  105. */
  106. private changeAnimation(type: string, num: number) {
  107. this.clearTimeOut(type);
  108. let num_bas = Math.abs(num);
  109. let time = this.animationTime / num_bas;
  110. let allTime = 0; //累计耗时间
  111. let num_ = this[type + "_num"];
  112. this[type + "_num"] += num;
  113. for (let i = 1; i <= num_bas; i++) {
  114. if (num < 0) {
  115. let arr = this.timeouts.get(type);
  116. arr[i] = window.setTimeout(() => {
  117. this[type].getComponent(cc.Label).string = (num_ - i).toString();
  118. }, allTime * 1000);
  119. allTime += time;
  120. } else {
  121. let arr = this.timeouts.get(type);
  122. arr[i] = window.setTimeout(() => {
  123. this[type].getComponent(cc.Label).string = (num_ + i).toString();
  124. }, allTime * 1000);
  125. allTime += time;
  126. }
  127. }
  128. }
  129. /**
  130. * 清空所有动画
  131. * @param type
  132. * @private
  133. */
  134. private clearTimeOut(type: string) {
  135. //停止所有关于 该类型改变的值
  136. let timeouts = this.timeouts.get(type);
  137. for (let i = 0; i < timeouts.length; i++) {
  138. if (this.timeouts[i]) {
  139. window.clearTimeout(timeouts[i]);
  140. }
  141. }
  142. this[type].getComponent(cc.Label).string = this[type + "_num"].toString(); //直接赋值
  143. this.timeouts.set(type, []);
  144. }
  145. /**
  146. * @param point 需要飞金币的起始点(世界坐标)
  147. * @private
  148. */
  149. public fly_gold_animation(pointPosition: cc.Vec3) {
  150. pointPosition = this.node.convertToNodeSpaceAR(pointPosition)
  151. return new Promise((resolve, reject) => {
  152. let num = Tools.getRandom(this.fly_gold_min, this.fly_gold_max - 1)
  153. if (num > this.goldPool.size()) {
  154. resolve(true)
  155. return
  156. }
  157. let p: any [] = []
  158. for (let i = 0; i < num; i++) {
  159. let pro = new Promise((resolve, reject) => {
  160. let gold = this.goldPool.get()
  161. gold.position = cc.v3(pointPosition)
  162. this.node.addChild(gold)
  163. let scale = Tools.getRealRandom(this.gold_scale_min, this.gold_scale_max)
  164. gold.scale = scale
  165. //随机角度, 随机半径
  166. let angle = Tools.getRandom(0, 360)
  167. let r = Tools.getRandom(this.radius_min, this.radius_max + 1)
  168. let boomPoint = Tools.getCirclePoint(pointPosition, r, angle)
  169. tween(gold)
  170. .to(this.boomTime, {position: boomPoint}, {easing: "quadOut"})
  171. .to(this.flyTime, {position: this.fly_end}, {easing: "quadOut"})
  172. .call(() => {
  173. this.goldPool.put(gold)
  174. resolve(true)
  175. })
  176. .start()
  177. })
  178. p.push(pro)
  179. }
  180. Promise.all(p).then(() => {
  181. resolve(true)
  182. })
  183. })
  184. }
  185. }