DogContainer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. work: {
  5. default: null,
  6. type: cc.SpriteFrame,
  7. serializable: true,
  8. },
  9. sleep: {
  10. default: null,
  11. type: cc.SpriteFrame,
  12. serializable: true,
  13. },
  14. dogBasinFood: {
  15. default: null,
  16. type: cc.SpriteFrame,
  17. serializable: true,
  18. },
  19. dogBasin: {
  20. default: null,
  21. type: cc.SpriteFrame,
  22. serializable: true,
  23. },
  24. DogNode: {
  25. default: null,
  26. type: cc.Node,
  27. serializable: true,
  28. },
  29. DogSliderNode: {
  30. default: null,
  31. type: cc.Node,
  32. serializable: true,
  33. },
  34. DogSliderLabel: {
  35. default: null,
  36. type: cc.Label,
  37. serializable: true,
  38. },
  39. basinNode: {
  40. default: null,
  41. type: cc.Node,
  42. serializable: true,
  43. },
  44. basinSliderNode: {
  45. default: null,
  46. type: cc.Node,
  47. serializable: true,
  48. },
  49. basinSliderLabel: {
  50. default: null,
  51. type: cc.Label,
  52. serializable: true,
  53. },
  54. showButtonLabel: {
  55. default: null,
  56. type: cc.Label,
  57. serializable: true,
  58. },
  59. infoBoxNode: {
  60. default: null,
  61. type: cc.Node,
  62. serializable: true,
  63. },
  64. //是否是自家的狗
  65. bSelfDog: {
  66. default: true,
  67. visible: false
  68. },
  69. buyDogFoodPrefab: {
  70. default: null,
  71. type: cc.Prefab,
  72. serializable: true,
  73. }
  74. },
  75. // LIFE-CYCLE CALLBACKS:
  76. onLoad() {
  77. this.onGetSelfList();
  78. },
  79. start() {
  80. this.basinNode.on(cc.Node.EventType.TOUCH_START, () => {
  81. console.log("点击添加狗粮,this.bSelfDog =", this.bSelfDog);
  82. if (!this.bSelfDog) return;
  83. //生成预制件
  84. let _foodPanel = cc.instantiate(this.buyDogFoodPrefab);
  85. let parent = cc.find('Canvas/UICamera/DAPPContainer');
  86. _foodPanel.parent = parent;
  87. _foodPanel.setPosition(0, 0);
  88. //把操作对象传入
  89. let _buyDogInfoScript = _foodPanel.getComponent("BuyDogInfo");
  90. _buyDogInfoScript.parentNode = this;
  91. _buyDogInfoScript.onLoadFoodList();
  92. })
  93. },
  94. /**
  95. * 直接更新狗的状态
  96. * @param {狗对象}} dog
  97. */
  98. onSetSelfDogState(dog) {
  99. this.bSelfDog = true;
  100. GlobalD.Dog = dog;
  101. this.infoBoxNode.active = true;
  102. this.DogNode.active = true;
  103. this._updateDogState(GlobalD.Dog);
  104. },
  105. onGetSelfList() {
  106. // let ManageUI = cc.find("GameNode/ManageUI").getComponent("ManageUI");
  107. this.bSelfDog = true;
  108. //获取狗的信息
  109. this._updateList({});
  110. },
  111. //获取其他用户狗的信息
  112. onGetOtherUserList() {
  113. this.bSelfDog = false;
  114. this._updateList({ otherUserId: GlobalD.OtherUserInfo.userId });
  115. },
  116. _updateList(data) {
  117. this.infoBoxNode.active = false;
  118. this._resetDogState();
  119. GlobalD.GameData.onGetEquipmentListState(data, (value) => {
  120. // console.log(value);
  121. let _list = value.data;
  122. for (let i = 0; i < _list.length; i++) {
  123. if (0 === _list[i].otherType) {
  124. GlobalD.Dog = _list[i];
  125. //存在狗
  126. if (this.bSelfDog) {
  127. this.infoBoxNode.active = true;
  128. this.DogNode.active = true;
  129. } else {
  130. this.DogNode.active = 1 === GlobalD.Dog.isShow ? true : false;
  131. }
  132. if (this.DogNode.active) {
  133. this._updateDogState(GlobalD.Dog);
  134. }
  135. } else if (1 === _list[i].otherType) {
  136. GlobalD.Stick = _list[i];
  137. if (GlobalD.Stick.remainingDay > 0)
  138. GlobalD.game._ManageUIScript.onSettingStickAlpha(255);
  139. }
  140. }
  141. })
  142. },
  143. _resetDogState() {
  144. GlobalD.Dog = null;
  145. GlobalD.Stick = null;
  146. this.DogNode.active = false;
  147. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
  148. GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
  149. GlobalD.game._ManageUIScript.onSettingStickAlpha(125);
  150. },
  151. //切换小狗状态
  152. onSwitchDogActive() {
  153. console.log(GlobalD.Dog);
  154. GlobalD.GameData.onSetDogShowState({ isShow: 1 === GlobalD.Dog.isShow ? 0 : 1 }, (value) => {
  155. console.log(value.data);
  156. GlobalD.Dog = value.data.dog;
  157. //修改按钮提示文字
  158. this.showButtonLabel.string = 1 === GlobalD.Dog.isShow ? "隐藏小狗" : "显示小狗"
  159. this.DogNode.opacity = 1 === GlobalD.Dog.isShow ? 255 : 125;
  160. })
  161. },
  162. _updateDogState(dogValue) {
  163. //修改按钮提示文字
  164. this.showButtonLabel.string = 1 === dogValue.isShow ? "隐藏小狗" : "显示小狗";
  165. //修改半显示状态
  166. this.DogNode.opacity = 1 === dogValue.isShow ? 255 : 125;
  167. this.DogSliderLabel.string = "剩" + dogValue.remainingDay + "天";
  168. this.basinSliderLabel.string = "剩余" + dogValue.remainingConsumption + "G";
  169. //狗的剩余天数进度
  170. let _dogSliderNodeScript = this.DogSliderNode.getComponent("slider_progress");
  171. let _dogSliderProccess = 0 === dogValue.totalDay ? 0 : dogValue.remainingDay / dogValue.totalDay;
  172. _dogSliderNodeScript.onSetProcgress(_dogSliderProccess);
  173. //狗粮的剩余进度
  174. let _basinSliderNodeScript = this.basinSliderNode.getComponent("slider_progress");
  175. let _basinSliderProccess = 0 === dogValue.totalConsumption ? 0 : dogValue.remainingConsumption / dogValue.totalConsumption;
  176. _basinSliderNodeScript.onSetProcgress(_basinSliderProccess);
  177. if (dogValue.remainingDay > 0 && dogValue.remainingConsumption > 0) {
  178. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasinFood;
  179. this.DogNode.getComponent(cc.Sprite).spriteFrame = this.work;
  180. GlobalD.game._ManageUIScript.onSettingDogAlpha(255);
  181. } else {
  182. //不够狗粮,睡觉
  183. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
  184. this.DogNode.getComponent(cc.Sprite).spriteFrame = this.sleep;
  185. GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
  186. }
  187. },
  188. });