room.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. const RoomState = {
  12. Invaild: -1,
  13. Lock: 0,
  14. UnLock: 1,
  15. Update: 2,
  16. Full: 3
  17. };
  18. cc.Class({
  19. extends: cc.Component,
  20. properties: {
  21. roomSprite: {
  22. default: null,
  23. type: cc.Sprite
  24. },
  25. animationImgNode: {
  26. default: null,
  27. type: cc.Node
  28. },
  29. roomBuySprites: {
  30. default: [],
  31. type: cc.SpriteFrame
  32. },
  33. buyButton: {
  34. default: null,
  35. type: cc.Button
  36. },
  37. priceLabel: {
  38. default: null,
  39. type: cc.Label
  40. },
  41. buyTipPrefab: {
  42. default: null,
  43. type: cc.Prefab
  44. },
  45. levelLabel: {
  46. default: null,
  47. type: cc.Label
  48. },
  49. earnMoneyLabel: {
  50. default: null,
  51. type: cc.Label
  52. },
  53. },
  54. init: function(config, totalMoney) {
  55. this.config = config;
  56. this.node.setPosition(config.x, config.y);
  57. this.node.width = config.width;
  58. this.node.height = config.height;
  59. let price = config.price[config.lv];
  60. if (totalMoney >= price) {
  61. this.setState(RoomState.Update);
  62. } else {
  63. this.setState(RoomState.Lock);
  64. }
  65. this.priceLabel.string = price.toString();
  66. this.levelLabel.string = "LV." + config.lv.toString();
  67. // 从本地资源中读取SpriteFrame
  68. cc.loader.loadRes("room/" + config.img, cc.SpriteFrame, (error, spriteFrame)=>{
  69. if (error) {
  70. console.log(error);
  71. } else {
  72. this.roomSprite.spriteFrame = spriteFrame;
  73. this.animationImgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  74. }
  75. });
  76. },
  77. // LIFE-CYCLE CALLBACKS:
  78. onLoad () {
  79. this.config = null;
  80. this.setState(RoomState.Invaild);
  81. this.setSchedule();
  82. },
  83. setSchedule: function() {
  84. this.schedule(function () {
  85. if (this.isEarnMoney()) {
  86. let price = this.config.speed[this.config.lv];
  87. this.earnMoneyLabel.string = "$" + price.toString();
  88. let moveAction = cc.moveBy(0.5, cc.p(0, 200));
  89. // let sqawn = cc.spawn([moveAction, cc.fadeOut(0.5)]);
  90. let callBack = cc.callFunc(function () {
  91. this.earnMoneyLabel.node.setPosition(cc.p(0, 0));
  92. // this.earnMoneyLabel.node.opacity = 255;
  93. Global.GameEvent.fire("earn_money", price);
  94. }, this);
  95. let sequence = cc.sequence(moveAction, callBack);
  96. this.earnMoneyLabel.node.runAction(sequence);
  97. }
  98. }, 1.0);
  99. },
  100. clickBuyButton: function() {
  101. if (Module.game.getComponent('game').getBuyTipNode() !== null) {
  102. return;
  103. }
  104. if (this.config !== undefined && this.state === RoomState.Update) {
  105. if (this.config.lv > 0) {
  106. let buyTipPrefab = cc.instantiate(this.buyTipPrefab);
  107. buyTipPrefab.getComponent('BuyTip').init(this, this.config, this.roomSprite.spriteFrame);
  108. buyTipPrefab.setScale(0, 0);
  109. buyTipPrefab.parent = Module.game;
  110. let action = cc.scaleTo(0.2, 1.0, 1.0).easing(cc.easeOut(0.95));
  111. buyTipPrefab.runAction(action);
  112. Module.game.getComponent('game').setBuyTipNode(buyTipPrefab);
  113. } else {
  114. this.updateAnimation();
  115. Global.GameEvent.fire("room_update", this);
  116. }
  117. }
  118. },
  119. isEarnMoney: function() {
  120. if (this.config.lv === 0) {
  121. return false;
  122. }
  123. return true;
  124. },
  125. getRoomConfig: function() {
  126. return this.config;
  127. },
  128. unlockUpdate: function() {
  129. this.setState(RoomState.Update);
  130. },
  131. lockUpdate: function() {
  132. this.setState(RoomState.Lock);
  133. },
  134. full: function() {
  135. this.setState(RoomState.Full);
  136. },
  137. setState: function(state) {
  138. if (this.state === state) {
  139. return;
  140. }
  141. switch (state) {
  142. case RoomState.Lock:
  143. this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[0];
  144. break;
  145. case RoomState.Update:
  146. this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[1];
  147. break;
  148. case RoomState.Full:
  149. this.buyButton.node.active = false;
  150. default:
  151. break;
  152. }
  153. this.state = state;
  154. },
  155. updateAnimation: function () {
  156. let scaleAction = cc.scaleTo(0.35, 1.5, 1.5);
  157. let spawn = cc.spawn([scaleAction, cc.fadeOut(0.35)]);
  158. let callBack = cc.callFunc(function () {
  159. this.animationImgNode.opacity = 255;
  160. this.animationImgNode.setScale(1.0, 1.0);
  161. }, this);
  162. let sequence = cc.sequence(spawn, callBack);
  163. this.animationImgNode.runAction(sequence);
  164. },
  165. // update (dt) {},
  166. });