FarmIcon.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { DataEventId, ExpAddType, FarmState, GameProp, InterAdType, 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. isCanTouch = true
  106. public async onTouch() {
  107. if (!this.isCanTouch) {
  108. return;
  109. }
  110. switch (this.data.state) {
  111. case FarmState.Lock:
  112. mk.tip.pop(`点击拓建即可解锁农田`);
  113. break;
  114. case FarmState.CanUnlock:
  115. mk.data.sendDataEvent(DataEventId.button_click, "拓建icon");
  116. gData.adUnlockData.openPanel(this.configID);
  117. break;
  118. case FarmState.Empty:
  119. gData.farmSystem.currSelectFarm = this;
  120. gData.plantData.openPanel(ProductType.nzw, this.configID);
  121. break;
  122. case FarmState.Growing:
  123. mk.ui.openPanel('module/speedUpUI/speedUp');
  124. break;
  125. case FarmState.Ripe:
  126. gData.farmSystem.currSelectFarm = this;
  127. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
  128. this.isCanTouch = false;
  129. setTimeout(() => {
  130. this.isCanTouch = true;
  131. }, 1000);
  132. break;
  133. case FarmState.Sick:
  134. // mk.tip.pop(`生虫了!`);
  135. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  136. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  137. break;
  138. }
  139. }
  140. public async onHarvest() {
  141. this.countDown.cleanRiped();
  142. this.process.cleanProcess();
  143. gData.farmSystem.currSelectFarm = null;
  144. this.data.state = FarmState.Empty;
  145. this.data.productID = 0;
  146. this.data.growSpan = 0;
  147. let child = this.node.getChildByName('hb');
  148. this.node.removeChild(child);
  149. gData.gameData.setFarmDataMap(this.configID, this.data);
  150. //await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, true);
  151. //gData.gameData.nextMake = null;
  152. gData.gameData.setNextProduct(false);
  153. if (gData.harvestData.getType == 0) {
  154. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  155. }
  156. else {
  157. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  158. }
  159. //gData.adData.checkPopInter(InterAdType.interstitial1_click_8);
  160. gData.gameData.popTableScreenADLogic(7);
  161. }
  162. public async plant(id: number, parentNode = null) {
  163. if (this.state == FarmState.Empty) {
  164. this.plantID = id;
  165. gData.gameData.isProducting = true;
  166. this.plantIcon.node.active = true;
  167. gData.farmSystem.currSelectFarm = this;
  168. gData.adData.checkPopRed();
  169. //gData.gameData.updateOrderTaskCopyData(id);
  170. try {
  171. gData.gameData.changeLeftTimes(-1);
  172. gData.gameData.addProductMakeTimesById(this.plantID);
  173. await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, parentNode);
  174. } catch (error) {
  175. console.log(`==========framIcon`);
  176. }
  177. gData.gameData.nextMake = null;
  178. gData.gameData.setNextProduct(false);
  179. this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
  180. this.plantIcon.getComponent(cc.Animation).play();
  181. this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  182. //this.doMakePlantFinishLogic();
  183. let flyRed = await gData.gameData.updateNewTaskProgress();
  184. return flyRed;
  185. }
  186. else {
  187. gData.gameData.setNextProduct(false);
  188. if(gData.gameData.nextType == 0){
  189. mk.tip.pop('农田已满,无法继续生产');
  190. return false;
  191. }else{
  192. let flyRed = await gData.gameData.makeProduct();
  193. return flyRed;
  194. }
  195. //mk.tip.pop('农田已满,无法继续生产');
  196. return false;
  197. }
  198. }
  199. /** 成熟 */
  200. public async onRiped(sendToServer) {
  201. this.data.state = FarmState.Ripe;
  202. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  203. }
  204. /** 生虫 */
  205. public onSick(sendToServer) {
  206. this.data.state = FarmState.Sick;
  207. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  208. }
  209. private doMakePlantFinishLogic() {
  210. let config = gData.gameData.getProductMap(this.plantID);
  211. if (mk.guide.getCurGuideIdString() == "10_2") {
  212. this.data.growSpan = 0;
  213. this.data.state = FarmState.Ripe;
  214. } else {
  215. let growSpan = Date.now() + config.time * 1000;
  216. this.data.growSpan = growSpan;
  217. this.data.state = FarmState.Growing;
  218. }
  219. this.data.productID = this.plantID;
  220. gData.gameData.setFarmDataMap(this.configID, this.data);
  221. gData.gameData.isProducting = false;
  222. }
  223. private async onPlantAnimationFinish() {
  224. this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  225. this.plantIcon.node.active = false;
  226. this.doMakePlantFinishLogic();
  227. }
  228. public novice_plant(): void {
  229. this.process.setData(this.data);
  230. }
  231. public novice_clean(): void {
  232. this.process.cleanProcess();
  233. }
  234. public canHarvest() {
  235. if(!this.isCanTouch){
  236. return;
  237. }
  238. gData.farmSystem.currSelectFarm = this;
  239. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
  240. this.isCanTouch = false;
  241. setTimeout(() => {
  242. this.isCanTouch = true;
  243. }, 1000);
  244. }
  245. public canClearSick() {
  246. gData.farmSystem.currSelectFarm = this;
  247. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  248. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  249. }
  250. public clickGuideBtn() {
  251. if (mk.guide.isGuiding()) {
  252. //gData.farmSystem.currSelectFarm = this;
  253. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, 0, false, true);
  254. }
  255. }
  256. }