game.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. var Module = require('Module');
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. roomPrefab: {
  15. default: null,
  16. type: cc.Prefab
  17. },
  18. earnMoneyButton: {
  19. default: null,
  20. type: cc.Button
  21. },
  22. totalMoneyLabel: {
  23. default: null,
  24. type: cc.Label
  25. },
  26. currentEarnMoneyLabel: {
  27. default: null,
  28. type: cc.Label
  29. },
  30. starCountLabel: {
  31. default: null,
  32. type: cc.Label
  33. },
  34. starNode: {
  35. default: null,
  36. type: cc.Node
  37. },
  38. friendSystemPrefab: {
  39. default: null,
  40. type: cc.Prefab
  41. },
  42. friendSystemButton: {
  43. default:null,
  44. type: cc.Button
  45. },
  46. },
  47. // LIFE-CYCLE CALLBACKS:
  48. onLoad () {
  49. Module.game = this.node;
  50. this.rooms = [];
  51. this.totalMoney = 2000;
  52. this.totalMoneyLabel.string = this.totalMoney.toString();
  53. this.currentEarnMoney = 0;
  54. this.starCount = 0;
  55. this.currentTime = 0;
  56. this.buyTipNode = null;
  57. this.loadRes();
  58. // 修改赚钱按钮的渲染排序的 Z 轴深度, 这样后面显示tipLabel时就可以被盖住了
  59. this.earnMoneyButton.node.zIndex = 1;
  60. this.setEventListener();
  61. },
  62. onDestroy() {
  63. Global.GameEvent.off("room_update", this);
  64. },
  65. // 监听事件
  66. setEventListener: function() {
  67. Global.GameEvent.on("room_update", this, (room)=>{
  68. this.buyTipNode = null;
  69. let config = room.getComponent('room').getRoomConfig();
  70. if (config.lv < config.price.length) {
  71. let price = config.price[config.lv];
  72. this.totalMoney -= price;
  73. this.totalMoneyLabel.string = this.totalMoney.toString();
  74. this.starCount += 1;
  75. this.starCountLabel.string = this.starCount.toString();
  76. config.lv += 1;
  77. room.getComponent('room').init(config, this.totalMoney);
  78. if (config.lv === config.price.length-1) {
  79. room.getComponent('room').full();
  80. }
  81. }
  82. });
  83. Global.GameEvent.on("earn_money", this, (price)=>{
  84. this.totalMoney += price;
  85. this.totalMoneyLabel.string = this.totalMoney.toString();
  86. });
  87. },
  88. // 加载本地资源
  89. loadRes: function () {
  90. cc.loader.loadRes("./Config/Layout", (error, result)=>{
  91. if (error) {
  92. console.log(error);
  93. } else {
  94. let total = result["total"];
  95. for (let i = 1; i <= total; i++) {
  96. let roomConfig = result["room" + i];
  97. let roomNode = cc.instantiate(this.roomPrefab);
  98. // 将当前界面上的所有建筑保存在一个数组里, 方便后续调用
  99. this.rooms.push(roomNode);
  100. // 添加到界面上
  101. roomNode.parent = this.node;
  102. // 配置room
  103. roomNode.getComponent("room").init(roomConfig, this.totalMoney);
  104. }
  105. }
  106. });
  107. },
  108. // 点击赚钱按钮
  109. clickEarnMoneyButton: function () {
  110. let totalSpeed = 0;
  111. let unlockCount = 0;
  112. for (let i = 0; i < this.rooms.length; i++) {
  113. let room = this.rooms[i];
  114. let config = room.getComponent('room').getRoomConfig();
  115. let isEarnMoney = room.getComponent('room').isEarnMoney();
  116. if (config !== undefined && isEarnMoney) {
  117. unlockCount += 1;
  118. // 根据当前等级取赚钱速度
  119. let speed = config.speed[config.lv];
  120. totalSpeed += speed;
  121. }
  122. }
  123. let totalMoney = 100 * unlockCount + totalSpeed / 5;
  124. if (totalMoney === 0) {
  125. totalMoney = 100;
  126. }
  127. this.addTipLabel(totalMoney)
  128. },
  129. getBuyTipNode: function() {
  130. return this.buyTipNode;
  131. },
  132. setBuyTipNode: function(buyTipNode) {
  133. this.buyTipNode = buyTipNode;
  134. },
  135. // 添加提示
  136. addTipLabel: function (totalMoney) {
  137. let tipNode = new cc.Node("tipNode");
  138. let tipLabel = tipNode.addComponent(cc.Label);
  139. tipLabel.fontSize = 80;
  140. tipLabel.lineHeight = 80;
  141. tipLabel.string = "$" + totalMoney.toString();
  142. let color = new cc.Color(252, 222, 2);
  143. tipNode.color = color;
  144. this.node.addChild(tipNode, 0);
  145. tipNode.position = this.earnMoneyButton.node.position;
  146. let moveAction = cc.moveBy(0.35, cc.p(0, 200));
  147. let spawn = cc.spawn([moveAction, cc.fadeOut(0.35)]);
  148. let callBack = cc.callFunc(function () {
  149. this.totalMoney += totalMoney;
  150. this.totalMoneyLabel.string = this.totalMoney.toString();
  151. }, this);
  152. let sequence = cc.sequence(spawn, callBack);
  153. tipNode.runAction(sequence);
  154. },
  155. // 显示好友系统
  156. showFriendSystem: function() {
  157. let friendSystem = cc.instantiate(this.friendSystemPrefab);
  158. friendSystem.getComponent('FriendSystem').init(this.friendSystemButton.node.position);
  159. this.node.addChild(friendSystem, 2);
  160. friendSystem.position = this.friendSystemButton.node.position;
  161. friendSystem.opacity = 0;
  162. friendSystem.setScale(0, 0);
  163. let moveAction = cc.moveTo(0.35, cc.p(0, 0));
  164. let scaleAction = cc.scaleTo(0.35, 1.0, 1.0);
  165. let spawn = cc.spawn([moveAction, scaleAction, cc.fadeIn(0.4)]);
  166. friendSystem.runAction(spawn);
  167. },
  168. update (dt) {
  169. if (this.rooms.length > 0) {
  170. this.currentTime += dt;
  171. var earnMoneySpeed = 0;
  172. for (let i = 0; i < this.rooms.length; i++) {
  173. let room = this.rooms[i];
  174. let config = room.getComponent('room').getRoomConfig();
  175. let price = config.price[config.lv];
  176. let isEarnMoney = room.getComponent('room').isEarnMoney();
  177. if (config !== undefined && isEarnMoney) {
  178. // 根据当前等级取赚钱速度
  179. let speed = config.speed[config.lv];
  180. earnMoneySpeed += speed;
  181. if (this.totalMoney >= price) {
  182. room.getComponent('room').unlockUpdate();
  183. } else {
  184. room.getComponent('room').lockUpdate();
  185. }
  186. } else {
  187. // 这是0级时的情况
  188. if (this.totalMoney < price) {
  189. room.getComponent('room').lockUpdate();
  190. } else {
  191. room.getComponent('room').unlockUpdate();
  192. }
  193. }
  194. }
  195. this.currentEarnMoney = earnMoneySpeed;
  196. this.currentEarnMoneyLabel.string = this.currentEarnMoney + " / 秒";
  197. }
  198. },
  199. });