PastureIcon.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** 养殖类 */
  2. import { AnimalState, DataEventId, ExpAddType, GameProp, PastureState, ProductType } from "../../game/data/GameData";
  3. import { MoveToCenter } from "../map/MoveToCenter";
  4. import Animals from "../view/Animals";
  5. import ProductShow from "../view/ProductShow";
  6. import { FarmCountDown } from "./FarmCountDown";
  7. import { IDHelper } from "./IDHelper";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class PastureIcon extends cc.Component {
  11. @property({ type: cc.Node, tooltip: "动物显示节点" }) points: cc.Node = null;
  12. @property({ type: cc.Node, tooltip: "动物喂食节点" }) eatNode: cc.Node = null;
  13. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  14. @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null;
  15. @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
  16. private animalsArr: Array<Animals> = new Array<Animals>();
  17. public configID: number = 0;
  18. public sortID: number = 0;
  19. private _state: PastureState = -1;
  20. public get state(): PastureState { return this._state; }
  21. data = null;
  22. //空地点击两次喂食
  23. private emptyClick = false;
  24. private pastureData = null;
  25. async onLoad() {
  26. let helper = this.getComponent(IDHelper);
  27. this.configID = helper.buildID;
  28. this.sortID = helper.sortID;
  29. this.data = gData.gameData.getPastureDataMap(this.configID);
  30. gData.pastureSystem.addPastureIcon(this);
  31. this.points.parent.active = false;
  32. let index = 0;
  33. let point = null;
  34. this.pastureData = gData.pastureSystem.setPastureData(this.configID)
  35. this.points.children.forEach(element => {
  36. point = element.getPosition();
  37. this.pastureData.setPointMap(index, point);
  38. index++;
  39. });
  40. for (var i = 0; i < 5; i++) {
  41. let prefab = await mk.loader.load("game/prefab/paster_" + this.configID, cc.Prefab);
  42. let animal = cc.instantiate(prefab);
  43. let animais = animal.getComponent(Animals);
  44. this.animalsArr.push(animais);
  45. animais.pointIndex = this.pastureData.getCanMoveIndex();
  46. animal.setPosition(this.pastureData.getCanMoveIndexPoint(animais.pointIndex));
  47. animal.parent = this.points.parent;
  48. }
  49. this.freshState();
  50. }
  51. update() {
  52. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  53. if (index != -1) {
  54. this.freshState();
  55. gData.gameData.needFreshArr.splice(index, 1);
  56. }
  57. }
  58. freshState() {
  59. this.data = gData.gameData.getPastureDataMap(this.configID);
  60. this.setState(this.data.state);
  61. }
  62. private setState(value: PastureState): void {
  63. if (this.state != value) {
  64. this._state = value;
  65. this.points.parent.active = false;
  66. this.eatNode.active = false;
  67. this.countDown.node.active = false;
  68. switch (this.state) {
  69. case PastureState.Lock:
  70. for (var i = 0; i < this.animalsArr.length; i++) {
  71. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  72. }
  73. break;
  74. case PastureState.Empty:
  75. for (var i = 0; i < this.animalsArr.length; i++) {
  76. this.animalsArr[i].setState(AnimalState.Hanger, this.configID);
  77. }
  78. if (this.idleAni && this.idleAni.active == false) {
  79. this.idleAni.active = true;
  80. }
  81. break;
  82. case PastureState.Growing:
  83. this.points.parent.active = true;
  84. this.eatNode.active = false;
  85. this.countDown.node.active = true;
  86. this.countDown.setData(this.data);
  87. for (var i = 0; i < this.animalsArr.length; i++) {
  88. this.animalsArr[i].setState(AnimalState.Eat, this.configID);
  89. }
  90. if (this.idleAni && this.idleAni.active == true) {
  91. this.idleAni.active = false;
  92. }
  93. break;
  94. case PastureState.Ripe:
  95. this.countDown.node.active = true;
  96. this.countDown.setState(1);
  97. for (var i = 0; i < this.animalsArr.length; i++) {
  98. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  99. }
  100. this.onProductComplete();
  101. break;
  102. case PastureState.Sick:
  103. this.points.parent.active = true;
  104. this.countDown.node.active = true;
  105. this.countDown.setState(0);
  106. for (var i = 0; i < this.animalsArr.length; i++) {
  107. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. /**生产完成 */
  114. private async onProductComplete() {
  115. if (this.showGroup) {
  116. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  117. let item = cc.instantiate(prefab);
  118. let id = this.data.productID;
  119. item.getComponent(ProductShow).id = id;
  120. item.parent = this.showGroup;
  121. }
  122. }
  123. private clean(): void {
  124. this.showGroup.removeAllChildren();
  125. }
  126. public async onTouch() {
  127. switch (this.data.state) {
  128. case PastureState.Lock:
  129. let config = gData.gameData.getProductMap(this.data.productID);
  130. if (config.unlock == 2) {
  131. mk.tip.pop(`农场达到${config.value}级解锁`)
  132. }
  133. else if (config.unlock == 1) {
  134. mk.tip.pop(`喂食${config.name}${config.value}次解锁`)
  135. }
  136. break;
  137. case PastureState.Empty:
  138. if (!this.emptyClick) {
  139. this.emptyClick = true;
  140. this.eatNode.active = true;
  141. }
  142. else {
  143. //调用喂食
  144. // this.onEat();
  145. this.eatNode.active = false;
  146. this.emptyClick = false;
  147. }
  148. break;
  149. case PastureState.Growing:
  150. mk.ui.openPanel('module/speedUpUI/speedUp');
  151. break;
  152. case PastureState.Ripe:
  153. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  154. break;
  155. case PastureState.Sick:
  156. // mk.tip.pop(`生虫了!`);
  157. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  158. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  159. break;
  160. }
  161. }
  162. onEat() {
  163. if (this.state == PastureState.Empty) {
  164. gData.gameData.isProducting = true;
  165. let config = gData.gameData.getProductMap(this.data.productID);
  166. let growSpan = Date.now() + config.time * 1000;
  167. this.data.state = PastureState.Growing;
  168. this.data.productID = this.data.productID;
  169. this.data.growSpan = growSpan;
  170. gData.gameData.setPastureDataMap(this.configID, this.data);
  171. this.eatNode.active = false;
  172. this.getComponent(MoveToCenter).move();
  173. gData.gameData.changeLeftTimes(-1);
  174. gData.gameData.isProducting = false;
  175. }
  176. }
  177. clickSpeedUp() {
  178. mk.ui.openPanel('module/speedUpUI/speedUp');
  179. }
  180. onRipe() {
  181. this.data.state = PastureState.Ripe;
  182. gData.gameData.setPastureDataMap(this.configID, this.data);
  183. gData.gameData.addProductMakeTimesById(this.data.productID);
  184. }
  185. onSick() {
  186. this.data.state = PastureState.Sick;
  187. gData.gameData.setPastureDataMap(this.configID, this.data);
  188. }
  189. public onHarvest(): void {
  190. this.countDown.cleanRiped();
  191. this.clean();
  192. this.data.state = PastureState.Empty;
  193. this.data.growSpan = 0;
  194. gData.gameData.setPastureDataMap(this.configID, this.data);
  195. gData.gameData.setNextProduct(false);
  196. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  197. }
  198. }