FarmIcon.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { DataEventId, ExpAddType, FarmState, GameProp, ProductType } from '../../game/data/GameData';
  2. import { MoveToCenter } from '../map/MoveToCenter';
  3. import ProductShow from '../view/ProductShow';
  4. import { FarmCountDown } from './FarmCountDown';
  5. import { FarmGrowProcess } from './FarmGrowProcess';
  6. import { IDHelper } from './IDHelper';
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export class FarmIcon extends cc.Component {
  10. @property({ type: cc.Node, tooltip: "未解锁时显示节点" }) lockNode: cc.Node = null;
  11. @property({ type: cc.Node, tooltip: "可解锁节点" }) canUnlockNode: cc.Node = null;
  12. @property({ type: cc.Node, tooltip: "选中闪烁节点" }) selectNode: cc.Node = null;
  13. @property({ type: cc.Sprite, tooltip: "种植动画图标" }) plantIcon: cc.Sprite = null;
  14. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  15. @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess = null;
  16. @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
  17. public configID: number = 0;
  18. public sortID: number = 0;
  19. public plantID: number = 0;
  20. private _state: FarmState = -1;
  21. public get state(): FarmState { return this._state; }
  22. public data = null;
  23. onLoad() {
  24. //this.plantIcon.node.active = false;
  25. let helper = this.getComponent(IDHelper);
  26. this.configID = helper.buildID;
  27. this.sortID = helper.sortID;
  28. gData.farmSystem.addFram(this);
  29. }
  30. start() {
  31. this.freshState();
  32. }
  33. update(dt) {
  34. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  35. if (index != -1) {
  36. this.freshState();
  37. gData.gameData.needFreshArr.splice(index, 1);
  38. }
  39. }
  40. freshState() {
  41. this.data = gData.gameData.getFarmDataMap(this.configID);
  42. this.setState(this.data.state);
  43. }
  44. private async setState(value: FarmState) {
  45. if (this.state != value) {
  46. this._state = value;
  47. this.lockNode.active = false;
  48. this.canUnlockNode.active = false;
  49. this.plantIcon.node.active = false;
  50. this.countDown.node.active = false;
  51. this.process.node.active = false;
  52. switch (this.state) {
  53. case FarmState.Lock:
  54. this.lockNode.active = true;
  55. break;
  56. case FarmState.CanUnlock:
  57. this.lockNode.active = true;
  58. this.canUnlockNode.active = true;
  59. break;
  60. case FarmState.Empty:
  61. if (this.idleAni && this.idleAni.active == false) {
  62. this.idleAni.active = true;
  63. }
  64. break;
  65. case FarmState.Growing:
  66. this.countDown.node.active = true;
  67. this.process.node.active = true;
  68. this.countDown.setData(this.data);
  69. this.process.setData(this.data);
  70. if (this.idleAni && this.idleAni.active == true) {
  71. this.idleAni.active = false;
  72. }
  73. break;
  74. case FarmState.Ripe:
  75. this.plantIcon.node.active = true;
  76. this.countDown.node.active = true;
  77. this.process.node.active = true;
  78. this.countDown.setState(1);
  79. this.process.setData(this.data);
  80. this.plantID = this.data.productID;
  81. let prefab = await mk.loader.load("game/prefab/ProductShow1", cc.Prefab);
  82. let item = cc.instantiate(prefab);
  83. item.name = 'hb';
  84. item.parent = this.node;
  85. break;
  86. case FarmState.Sick:
  87. this.plantIcon.node.active = true;
  88. this.countDown.node.active = true;
  89. this.process.node.active = true;
  90. this.countDown.setState(0);
  91. this.process.setData(this.data);
  92. break;
  93. }
  94. }
  95. }
  96. public activeFram(): void {
  97. this.lockNode.active = false;
  98. }
  99. public selectFarm(isSelect = false): void {
  100. this.selectNode.active = isSelect;
  101. if (isSelect) {
  102. this.getComponent(MoveToCenter).move();
  103. }
  104. }
  105. public async onTouch() {
  106. switch (this.data.state) {
  107. case FarmState.Lock:
  108. mk.tip.pop(`点击拓建即可解锁农田`);
  109. break;
  110. case FarmState.CanUnlock:
  111. mk.data.sendDataEvent(DataEventId.button_click, "拓建icon");
  112. gData.adUnlockData.openPanel(this.configID);
  113. break;
  114. case FarmState.Empty:
  115. gData.farmSystem.currSelectFarm = this;
  116. gData.plantData.openPanel(ProductType.nzw, this.configID);
  117. break;
  118. case FarmState.Growing:
  119. mk.ui.openPanel('module/speedUpUI/speedUp');
  120. break;
  121. case FarmState.Ripe:
  122. gData.farmSystem.currSelectFarm = this;
  123. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, true);
  124. break;
  125. case FarmState.Sick:
  126. // mk.tip.pop(`生虫了!`);
  127. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  128. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  129. break;
  130. }
  131. }
  132. public async onHarvest() {
  133. this.countDown.cleanRiped();
  134. this.process.cleanProcess();
  135. gData.farmSystem.currSelectFarm = null;
  136. this.data.state = FarmState.Empty;
  137. this.data.productID = 0;
  138. this.data.growSpan = 0;
  139. let child = this.node.getChildByName('hb');
  140. this.node.removeChild(child);
  141. gData.gameData.setFarmDataMap(this.configID, this.data);
  142. //await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, true);
  143. //gData.gameData.nextMake = null;
  144. gData.gameData.setNextProduct(false);
  145. if (gData.harvestData.getType == 0) {
  146. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  147. }
  148. else {
  149. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  150. }
  151. }
  152. public async plant(id: number, parentNode = null) {
  153. if (this.state == FarmState.Empty) {
  154. this.plantID = id;
  155. gData.gameData.isProducting = true;
  156. this.plantIcon.node.active = true;
  157. gData.farmSystem.currSelectFarm = this;
  158. gData.adData.checkPopRed();
  159. //gData.gameData.updateOrderTaskCopyData(id);
  160. try {
  161. gData.gameData.changeLeftTimes(-1);
  162. gData.gameData.addProductMakeTimesById(this.plantID);
  163. await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, parentNode);
  164. } catch (error) {
  165. console.log(`==========framIcon`);
  166. }
  167. gData.gameData.nextMake = null;
  168. gData.gameData.setNextProduct(false);
  169. this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
  170. this.plantIcon.getComponent(cc.Animation).play();
  171. this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  172. //this.doMakePlantFinishLogic();
  173. let flyRed = await gData.gameData.updateNewTaskProgress();
  174. return flyRed;
  175. }
  176. else {
  177. mk.tip.pop('农田已满,无法继续生产');
  178. return false;
  179. }
  180. }
  181. /** 成熟 */
  182. public async onRiped(sendToServer) {
  183. this.data.state = FarmState.Ripe;
  184. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  185. }
  186. /** 生虫 */
  187. public onSick(sendToServer) {
  188. this.data.state = FarmState.Sick;
  189. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  190. }
  191. private doMakePlantFinishLogic() {
  192. let config = gData.gameData.getProductMap(this.plantID);
  193. if (mk.guide.getCurGuideIdString() == "10_2") {
  194. this.data.growSpan = 0;
  195. this.data.state = FarmState.Ripe;
  196. } else {
  197. let growSpan = Date.now() + config.time * 1000;
  198. this.data.growSpan = growSpan;
  199. this.data.state = FarmState.Growing;
  200. }
  201. this.data.productID = this.plantID;
  202. gData.gameData.setFarmDataMap(this.configID, this.data);
  203. gData.gameData.isProducting = false;
  204. }
  205. private async onPlantAnimationFinish() {
  206. this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  207. this.plantIcon.node.active = false;
  208. this.doMakePlantFinishLogic();
  209. }
  210. public novice_plant(): void {
  211. this.process.setData(this.data);
  212. }
  213. public novice_clean(): void {
  214. this.process.cleanProcess();
  215. }
  216. public canHarvest() {
  217. gData.farmSystem.currSelectFarm = this;
  218. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, true);
  219. }
  220. public canClearSick() {
  221. gData.farmSystem.currSelectFarm = this;
  222. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  223. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  224. }
  225. public clickGuideBtn() {
  226. if (mk.guide.isGuiding()) {
  227. //gData.farmSystem.currSelectFarm = this;
  228. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, true, false, true);
  229. }
  230. }
  231. }