FarmIcon.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { DataEventId, ExpAddType, FarmState, GameProp, ProductType } from '../../game/data/GameData';
  2. import { MoveToCenter } from '../map/MoveToCenter';
  3. import { FarmCountDown } from './FarmCountDown';
  4. import { FarmGrowProcess } from './FarmGrowProcess';
  5. import { IDHelper } from './IDHelper';
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export class FarmIcon extends cc.Component {
  9. @property({ type: cc.Node, tooltip: "未解锁时显示节点" }) lockNode: cc.Node = null;
  10. @property({ type: cc.Node, tooltip: "可解锁节点" }) canUnlockNode: cc.Node = null;
  11. @property({ type: cc.Node, tooltip: "选中闪烁节点" }) selectNode: cc.Node = null;
  12. @property({ type: cc.Sprite, tooltip: "种植动画图标" }) plantIcon: cc.Sprite = null;
  13. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  14. @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess = null;
  15. @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
  16. public configID: number = 0;
  17. public sortID: number = 0;
  18. public plantID: number = 0;
  19. private _state: FarmState = -1;
  20. public get state(): FarmState { return this._state; }
  21. public data = null;
  22. onLoad() {
  23. //this.plantIcon.node.active = false;
  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. if (this.configID == 31000) {
  85. mk.guide.open(15);
  86. }
  87. break;
  88. case FarmState.Sick:
  89. this.plantIcon.node.active = true;
  90. this.countDown.node.active = true;
  91. this.process.node.active = true;
  92. this.countDown.setState(0);
  93. this.process.setData(this.data);
  94. break;
  95. case FarmState.NoOpen:
  96. this.lockNode.active = true;
  97. this.lockNode.color = cc.Color.GRAY;
  98. break;
  99. }
  100. }
  101. }
  102. public activeFram(): void {
  103. this.lockNode.active = false;
  104. }
  105. public selectFarm(isSelect = false): void {
  106. this.selectNode.active = isSelect;
  107. if (isSelect) {
  108. this.getComponent(MoveToCenter).move();
  109. }
  110. }
  111. isCanTouch = true
  112. public async onTouch() {
  113. if (!this.isCanTouch) {
  114. return;
  115. }
  116. switch (this.data.state) {
  117. case FarmState.Lock:
  118. mk.tip.pop(`点击拓建即可解锁农田`);
  119. break;
  120. case FarmState.CanUnlock:
  121. mk.data.sendDataEvent(DataEventId.button_click, "拓建icon");
  122. gData.adUnlockData.openPanel(this.configID);
  123. break;
  124. case FarmState.Empty:
  125. gData.farmSystem.currSelectFarm = this;
  126. mk.guide.next();
  127. gData.plantData.openPanel(ProductType.nzw, this.configID);
  128. break;
  129. case FarmState.Growing:
  130. mk.ui.openPanel('module/speedUpUI/speedUp');
  131. break;
  132. case FarmState.Ripe:
  133. gData.farmSystem.currSelectFarm = this;
  134. this.onHarvest();
  135. // gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
  136. this.isCanTouch = false;
  137. setTimeout(() => {
  138. this.isCanTouch = true;
  139. }, 1000);
  140. mk.guide.next();
  141. break;
  142. case FarmState.Sick:
  143. // mk.tip.pop(`生虫了!`);
  144. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  145. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  146. break;
  147. case FarmState.NoOpen:
  148. mk.tip.pop(`暂未开放`);
  149. break;
  150. }
  151. }
  152. public async onHarvest() {
  153. this.countDown.cleanRiped();
  154. this.process.cleanProcess();
  155. gData.farmSystem.currSelectFarm = null;
  156. this.data.state = FarmState.Empty;
  157. this.data.productID = 0;
  158. this.data.growSpan = 0;
  159. let child = this.node.getChildByName('hb');
  160. this.node.removeChild(child);
  161. gData.gameData.setFarmDataMap(this.configID, this.data);
  162. //await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, true);
  163. //gData.gameData.nextMake = null;
  164. gData.gameData.setNextProduct(false);
  165. gData.gameData.gameStyle.doCropFlyLogic(this.plantID);
  166. if (gData.harvestData.getType == 0) {
  167. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  168. }
  169. else {
  170. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  171. }
  172. gData.gameData.popTableScreenADLogic(7);
  173. mk.guide.open(16);
  174. }
  175. public async plant(id: number, parentNode = null) {
  176. if (this.state == FarmState.Empty) {
  177. this.plantID = id;
  178. gData.gameData.isProducting = true;
  179. this.plantIcon.node.active = true;
  180. gData.farmSystem.currSelectFarm = this;
  181. gData.adData.checkPopRed();
  182. //gData.gameData.updateOrderTaskCopyData(id);
  183. try {
  184. gData.gameData.changeLeftTimes(-1);
  185. gData.gameData.addProductMakeTimesById(this.plantID);
  186. // await gData.gameData.gameStyle.doCropFlyLogic(this.plantID, this.node, parentNode);
  187. } catch (error) {
  188. console.log(`==========framIcon`);
  189. }
  190. gData.gameData.nextMake = null;
  191. gData.gameData.setNextProduct(false);
  192. this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
  193. this.plantIcon.getComponent(cc.Animation).play();
  194. this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  195. //this.doMakePlantFinishLogic();
  196. let flyRed = await gData.gameData.updateNewTaskProgress();
  197. return flyRed;
  198. }
  199. else {
  200. gData.gameData.nextMake = null;
  201. gData.gameData.setNextProduct(false);
  202. if (gData.gameData.nextType == 0) {
  203. mk.tip.pop('农田已满,无法继续生产');
  204. return false;
  205. } else {
  206. let flyRed = await gData.gameData.makeProduct();
  207. return flyRed;
  208. }
  209. //mk.tip.pop('农田已满,无法继续生产');
  210. return false;
  211. }
  212. }
  213. /** 成熟 */
  214. public onRiped(sendToServer) {
  215. console.log('onRiped ' + this.configID);
  216. this.data.state = FarmState.Ripe;
  217. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  218. }
  219. /** 生虫 */
  220. public onSick(sendToServer) {
  221. this.data.state = FarmState.Sick;
  222. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  223. }
  224. private doMakePlantFinishLogic() {
  225. let config = gData.gameData.getProductMap(this.plantID);
  226. // if (mk.guide.getCurGuideIdString() == "10_2") {
  227. // this.data.growSpan = 0;
  228. // this.data.state = FarmState.Ripe;
  229. // } else {
  230. // let growSpan = Date.now() + config.time * 1000;
  231. // this.data.growSpan = growSpan;
  232. // this.data.state = FarmState.Growing;
  233. // }
  234. let growSpan = Date.now() + config.time * 1000;
  235. this.data.growSpan = growSpan;
  236. this.data.state = FarmState.Growing;
  237. this.data.productID = this.plantID;
  238. gData.gameData.setFarmDataMap(this.configID, this.data);
  239. gData.gameData.isProducting = false;
  240. }
  241. private async onPlantAnimationFinish() {
  242. this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  243. this.plantIcon.node.active = false;
  244. this.doMakePlantFinishLogic();
  245. }
  246. public novice_plant(): void {
  247. this.process.setData(this.data);
  248. }
  249. public novice_clean(): void {
  250. this.process.cleanProcess();
  251. }
  252. public canHarvest() {
  253. if (!this.isCanTouch) {
  254. return;
  255. }
  256. gData.farmSystem.currSelectFarm = this;
  257. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
  258. this.isCanTouch = false;
  259. setTimeout(() => {
  260. this.isCanTouch = true;
  261. }, 1000);
  262. }
  263. public canClearSick() {
  264. gData.farmSystem.currSelectFarm = this;
  265. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  266. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  267. }
  268. public clickGuideBtn() {
  269. if (mk.guide.isGuiding()) {
  270. //gData.farmSystem.currSelectFarm = this;
  271. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node, 0, false, true);
  272. }
  273. }
  274. }