DogContainer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. GlobalD.game._ManageUIScript.onSettingStickAlpha(255);
  138. }
  139. }
  140. })
  141. },
  142. _resetDogState() {
  143. GlobalD.Dog = null;
  144. GlobalD.Stick = null;
  145. this.DogNode.active = false;
  146. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
  147. GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
  148. GlobalD.game._ManageUIScript.onSettingStickAlpha(125);
  149. },
  150. //切换小狗状态
  151. onSwitchDogActive() {
  152. console.log(GlobalD.Dog);
  153. GlobalD.GameData.onSetDogShowState({ isShow: 1 === GlobalD.Dog.isShow ? 0 : 1 }, (value) => {
  154. console.log(value.data);
  155. GlobalD.Dog = value.data.dog;
  156. //修改按钮提示文字
  157. this.showButtonLabel.string = 1 === GlobalD.Dog.isShow ? "隐藏小狗" : "显示小狗"
  158. })
  159. },
  160. _updateDogState(dogValue) {
  161. this.DogSliderLabel.string = "剩" + dogValue.remainingDay + "天";
  162. this.basinSliderLabel.string = "剩余" + dogValue.remainingConsumption + "G";
  163. //狗的剩余天数进度
  164. let _dogSliderNodeScript = this.DogSliderNode.getComponent("slider_progress");
  165. let _dogSliderProccess = 0 === dogValue.totalDay ? 0 : dogValue.remainingDay / dogValue.totalDay;
  166. _dogSliderNodeScript.onSetProcgress(_dogSliderProccess);
  167. //狗粮的剩余进度
  168. let _basinSliderNodeScript = this.basinSliderNode.getComponent("slider_progress");
  169. let _basinSliderProccess = 0 === dogValue.totalConsumption ? 0 : dogValue.remainingConsumption / dogValue.totalConsumption;
  170. _basinSliderNodeScript.onSetProcgress(_basinSliderProccess);
  171. if (dogValue.remainingConsumption > 0) {
  172. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasinFood;
  173. this.DogNode.getComponent(cc.Sprite).spriteFrame = this.work;
  174. GlobalD.game._ManageUIScript.onSettingDogAlpha(255);
  175. } else {
  176. //不够狗粮,睡觉
  177. this.basinNode.getComponent(cc.Sprite).spriteFrame = this.dogBasin;
  178. this.DogNode.getComponent(cc.Sprite).spriteFrame = this.sleep;
  179. GlobalD.game._ManageUIScript.onSettingDogAlpha(125);
  180. }
  181. },
  182. });