PastureIcon.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. @property({ type: cc.Node, tooltip: '文字动画' }) node_ani: cc.Node = null;
  17. private animalsArr: Array<Animals> = new Array<Animals>();
  18. public configID: number = 0;
  19. public sortID: number = 0;
  20. private _state: PastureState = -1;
  21. public get state(): PastureState { return this._state; }
  22. data = null;
  23. //空地点击两次喂食
  24. private emptyClick = false;
  25. private pastureData = null;
  26. async onLoad() {
  27. let helper = this.getComponent(IDHelper);
  28. this.configID = helper.buildID;
  29. this.sortID = helper.sortID;
  30. this.data = gData.gameData.getPastureDataMap(this.configID);
  31. gData.pastureSystem.addPastureIcon(this);
  32. this.points.parent.active = false;
  33. let index = 0;
  34. let point = null;
  35. this.pastureData = gData.pastureSystem.setPastureData(this.configID)
  36. this.points.children.forEach(element => {
  37. point = element.getPosition();
  38. this.pastureData.setPointMap(index, point);
  39. index++;
  40. });
  41. for (var i = 0; i < 5; i++) {
  42. let prefab = await mk.loader.load("game/prefab/paster_" + this.configID, cc.Prefab);
  43. let animal = cc.instantiate(prefab);
  44. let animais = animal.getComponent(Animals);
  45. this.animalsArr.push(animais);
  46. animais.pointIndex = this.pastureData.getCanMoveIndex();
  47. animal.setPosition(this.pastureData.getCanMoveIndexPoint(animais.pointIndex));
  48. animal.parent = this.points.parent;
  49. }
  50. this.freshState();
  51. }
  52. update() {
  53. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  54. if (index != -1) {
  55. this.freshState();
  56. gData.gameData.needFreshArr.splice(index, 1);
  57. }
  58. }
  59. freshState() {
  60. this.data = gData.gameData.getPastureDataMap(this.configID);
  61. this.setState(this.data.state);
  62. }
  63. private setState(value: PastureState): void {
  64. if (this.state != value) {
  65. this._state = value;
  66. this.points.parent.active = false;
  67. this.eatNode.active = false;
  68. this.countDown.node.active = false;
  69. switch (this.state) {
  70. case PastureState.Lock:
  71. for (var i = 0; i < this.animalsArr.length; i++) {
  72. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  73. }
  74. break;
  75. case PastureState.Empty:
  76. for (var i = 0; i < this.animalsArr.length; i++) {
  77. this.animalsArr[i].setState(AnimalState.Hanger, this.configID);
  78. }
  79. if (this.idleAni && this.idleAni.active == false) {
  80. this.idleAni.active = true;
  81. }
  82. break;
  83. case PastureState.Growing:
  84. this.points.parent.active = true;
  85. this.eatNode.active = false;
  86. this.countDown.node.active = true;
  87. this.countDown.setData(this.data);
  88. for (var i = 0; i < this.animalsArr.length; i++) {
  89. this.animalsArr[i].setState(AnimalState.Eat, this.configID);
  90. }
  91. if (this.idleAni && this.idleAni.active == true) {
  92. this.idleAni.active = false;
  93. }
  94. break;
  95. case PastureState.Ripe:
  96. this.countDown.node.active = true;
  97. this.countDown.setState(1);
  98. for (var i = 0; i < this.animalsArr.length; i++) {
  99. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  100. }
  101. this.onProductComplete();
  102. break;
  103. case PastureState.Sick:
  104. this.points.parent.active = true;
  105. this.countDown.node.active = true;
  106. this.countDown.setState(0);
  107. for (var i = 0; i < this.animalsArr.length; i++) {
  108. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. /**生产完成 */
  115. private async onProductComplete() {
  116. if (this.showGroup) {
  117. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  118. let item = cc.instantiate(prefab);
  119. let id = this.data.productID;
  120. item.getComponent(ProductShow).id = id;
  121. item.parent = this.showGroup;
  122. }
  123. }
  124. private clean(): void {
  125. this.showGroup.removeAllChildren();
  126. }
  127. public async onTouch() {
  128. switch (this.data.state) {
  129. case PastureState.Lock:
  130. let config = gData.gameData.getProductMap(this.data.productID);
  131. if (config.unlock == 2) {
  132. mk.tip.pop(`完成${config.value}次任务红包后解锁`);
  133. }
  134. else if (config.unlock == 1) {
  135. mk.tip.pop(`喂食${config.name}${config.value}次解锁`)
  136. }
  137. break;
  138. case PastureState.Empty:
  139. if (!this.emptyClick) {
  140. this.emptyClick = true;
  141. this.eatNode.active = true;
  142. }
  143. else {
  144. //调用喂食
  145. // this.onEat();
  146. this.eatNode.active = false;
  147. this.emptyClick = false;
  148. }
  149. break;
  150. case PastureState.Growing:
  151. mk.ui.openPanel('module/speedUpUI/speedUp');
  152. break;
  153. case PastureState.Ripe:
  154. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  155. break;
  156. case PastureState.Sick:
  157. // mk.tip.pop(`生虫了!`);
  158. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  159. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  160. break;
  161. }
  162. }
  163. async onEat() {
  164. if (gData.gameData.leftTimes <= 0) {
  165. mk.ui.openPanel('module/speedUpUI/productReward');
  166. return;
  167. }
  168. if (this.state == PastureState.Empty) {
  169. gData.gameData.isProducting = true;
  170. let config = gData.gameData.getProductMap(this.data.productID);
  171. let growSpan = Date.now() + config.time * 1000;
  172. this.data.state = PastureState.Growing;
  173. this.data.productID = this.data.productID;
  174. this.data.growSpan = growSpan;
  175. gData.gameData.setPastureDataMap(this.configID, this.data);
  176. this.eatNode.active = false;
  177. this.getComponent(MoveToCenter).move();
  178. gData.gameData.changeLeftTimes(-1);
  179. gData.gameData.isProducting = false;
  180. let flyRed = await gData.gameData.updateNewTaskProgress();
  181. if (flyRed) {
  182. //mk.audio.playEffect('redmoney');
  183. if (gData.gameData.playerProp.userFarmTaskInfo) {
  184. let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
  185. let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
  186. let addAni = cc.instantiate(this.node_ani);
  187. addAni.parent = this.node_ani.parent;
  188. addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
  189. cc.tween(addAni).call(()=>{
  190. addAni.opacity = 255;
  191. }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
  192. addAni.destroy();
  193. }).start();
  194. }
  195. let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
  196. gData.gameData.gameStyle.dpFlyRedAni(pos);
  197. }
  198. }
  199. }
  200. clickSpeedUp() {
  201. mk.ui.openPanel('module/speedUpUI/speedUp');
  202. }
  203. onRipe(sendToServer) {
  204. this.data.state = PastureState.Ripe;
  205. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  206. gData.gameData.addProductMakeTimesById(this.data.productID);
  207. }
  208. onSick(sendToServer) {
  209. this.data.state = PastureState.Sick;
  210. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  211. }
  212. public onHarvest(): void {
  213. this.countDown.cleanRiped();
  214. this.clean();
  215. this.data.state = PastureState.Empty;
  216. this.data.growSpan = 0;
  217. gData.gameData.setPastureDataMap(this.configID, this.data);
  218. gData.gameData.setNextProduct(false);
  219. if (gData.harvestData.getType == 0) {
  220. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  221. }
  222. else {
  223. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  224. }
  225. }
  226. public canHarvest() {
  227. this.getComponent(MoveToCenter).move();
  228. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  229. }
  230. public canClearSick() {
  231. this.getComponent(MoveToCenter).move();
  232. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  233. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  234. }
  235. }