FarmIcon.ts 9.4 KB

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