FarmIcon.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 onHarvest(): void {
  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. gData.gameData.setFarmDataMap(this.configID, this.data);
  139. gData.gameData.setNextProduct(false);
  140. if (gData.harvestData.getType == 0) {
  141. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  142. }
  143. else {
  144. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  145. }
  146. let child = this.node.getChildByName('hb');
  147. this.node.removeChild(child);
  148. }
  149. public async plant(id: number) {
  150. if (this.state == FarmState.Empty) {
  151. this.plantID = id;
  152. gData.gameData.isProducting = true;
  153. this.plantIcon.node.active = true;
  154. gData.gameData.changeLeftTimes(-1);
  155. gData.farmSystem.currSelectFarm = this;
  156. gData.adData.checkPopRed();
  157. gData.gameData.addProductMakeTimesById(this.plantID);
  158. gData.gameData.nextMake = null;
  159. gData.gameData.setNextProduct(false);
  160. this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
  161. this.plantIcon.getComponent(cc.Animation).play();
  162. this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  163. let flyRed = await gData.gameData.updateNewTaskProgress();
  164. return flyRed;
  165. }
  166. else {
  167. mk.tip.pop('农田已满,无法继续生产');
  168. return false;
  169. }
  170. }
  171. /** 成熟 */
  172. public async onRiped(sendToServer) {
  173. this.data.state = FarmState.Ripe;
  174. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  175. }
  176. /** 生虫 */
  177. public onSick(sendToServer) {
  178. this.data.state = FarmState.Sick;
  179. gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
  180. }
  181. private async onPlantAnimationFinish() {
  182. this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  183. this.plantIcon.node.active = false;
  184. let config = gData.gameData.getProductMap(this.plantID);
  185. let growSpan = Date.now() + config.time * 1000;
  186. this.data.state = FarmState.Growing;
  187. this.data.productID = this.plantID;
  188. this.data.growSpan = growSpan;
  189. gData.gameData.setFarmDataMap(this.configID, this.data);
  190. gData.gameData.isProducting = false;
  191. }
  192. public novice_plant(): void {
  193. this.process.setData(this.data);
  194. }
  195. public novice_clean(): void {
  196. this.process.cleanProcess();
  197. }
  198. public canHarvest() {
  199. gData.farmSystem.currSelectFarm = this;
  200. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node);
  201. }
  202. public canClearSick() {
  203. gData.farmSystem.currSelectFarm = this;
  204. mk.data.sendDataEvent(DataEventId.button_click, "生虫icon");
  205. gData.adClearSickData.openPanel(ProductType.nzw, this.configID);
  206. }
  207. }