PastureIcon.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /** 养殖类 */
  2. import { AnimalState, DataEventId, ExpAddType, GameProp, PastureState, ProductType } from "../../game/data/GameData";
  3. import { MoveToCenter } from "../map/MoveToCenter";
  4. import Animals from "../view/Animals";
  5. import ProductShow from "../view/ProductShow";
  6. import { FarmCountDown } from "./FarmCountDown";
  7. import { IDHelper } from "./IDHelper";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class PastureIcon extends cc.Component {
  11. @property({ type: cc.Node, tooltip: "动物显示节点" }) points: cc.Node = null;
  12. @property({ type: cc.Node, tooltip: "动物喂食节点" }) eatNode: cc.Node = null;
  13. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  14. @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null;
  15. @property({ type: cc.Node, tooltip: "渲染节点" }) buildNode: cc.Node = null;
  16. @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
  17. @property({ type: cc.Node, tooltip: '文字动画' }) node_ani: cc.Node = null;
  18. @property({ type: cc.Node, tooltip: '锁住节点' }) node_lock: cc.Node = null;
  19. private animalsArr: Array<Animals> = new Array<Animals>();
  20. public configID: number = 0;
  21. public sortID: number = 0;
  22. private _state: PastureState = -1;
  23. public get state(): PastureState { return this._state; }
  24. data = null;
  25. //空地点击两次喂食
  26. private emptyClick = false;
  27. private pastureData = null;
  28. async onLoad() {
  29. let helper = this.getComponent(IDHelper);
  30. this.configID = helper.buildID;
  31. this.sortID = helper.sortID;
  32. this.data = gData.gameData.getPastureDataMap(this.configID);
  33. gData.pastureSystem.addPastureIcon(this);
  34. this.points.parent.active = false;
  35. let index = 0;
  36. let point = null;
  37. this.pastureData = gData.pastureSystem.setPastureData(this.configID)
  38. this.points.children.forEach(element => {
  39. point = element.getPosition();
  40. this.pastureData.setPointMap(index, point);
  41. index++;
  42. });
  43. for (var i = 0; i < 5; i++) {
  44. let prefab = await mk.loader.load("game/prefab/paster_" + this.configID, cc.Prefab);
  45. let animal = cc.instantiate(prefab);
  46. let animais = animal.getComponent(Animals);
  47. this.animalsArr.push(animais);
  48. animais.pointIndex = this.pastureData.getCanMoveIndex();
  49. animal.setPosition(this.pastureData.getCanMoveIndexPoint(animais.pointIndex));
  50. animal.parent = this.points.parent;
  51. }
  52. this.freshState();
  53. }
  54. update() {
  55. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  56. if (index != -1) {
  57. this.freshState();
  58. gData.gameData.needFreshArr.splice(index, 1);
  59. }
  60. }
  61. freshState() {
  62. this.data = gData.gameData.getPastureDataMap(this.configID);
  63. this.setState(this.data.state);
  64. }
  65. private setState(value: PastureState): void {
  66. if (this.state != value) {
  67. this._state = value;
  68. this.points.parent.active = false;
  69. this.eatNode.active = false;
  70. this.countDown.node.active = false;
  71. this.node_lock.active = false;
  72. switch (this.state) {
  73. case PastureState.Lock:
  74. for (var i = 0; i < this.buildNode.childrenCount; i++) {
  75. this.buildNode.children[i].color = cc.Color.GRAY;
  76. }
  77. for (var i = 0; i < this.animalsArr.length; i++) {
  78. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  79. }
  80. this.node_lock.active = true;
  81. break;
  82. case PastureState.Empty:
  83. for (var i = 0; i < this.buildNode.childrenCount; i++) {
  84. this.buildNode.children[i].color = cc.Color.WHITE;
  85. }
  86. for (var i = 0; i < this.animalsArr.length; i++) {
  87. this.animalsArr[i].setState(AnimalState.Hanger, this.configID);
  88. }
  89. if (this.idleAni && this.idleAni.active == false) {
  90. this.idleAni.active = true;
  91. }
  92. break;
  93. case PastureState.Growing:
  94. this.points.parent.active = true;
  95. this.eatNode.active = false;
  96. this.countDown.node.active = true;
  97. this.countDown.setData(this.data);
  98. for (var i = 0; i < this.animalsArr.length; i++) {
  99. this.animalsArr[i].setState(AnimalState.Eat, this.configID);
  100. }
  101. if (this.idleAni && this.idleAni.active == true) {
  102. this.idleAni.active = false;
  103. }
  104. break;
  105. case PastureState.Ripe:
  106. this.countDown.node.active = true;
  107. this.countDown.setState(1);
  108. for (var i = 0; i < this.animalsArr.length; i++) {
  109. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  110. }
  111. this.onProductComplete();
  112. break;
  113. case PastureState.Sick:
  114. this.points.parent.active = true;
  115. this.countDown.node.active = true;
  116. this.countDown.setState(0);
  117. for (var i = 0; i < this.animalsArr.length; i++) {
  118. this.animalsArr[i].setState(AnimalState.Wait, this.configID);
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. /**生产完成 */
  125. private async onProductComplete() {
  126. if (this.showGroup) {
  127. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  128. let item = cc.instantiate(prefab);
  129. let id = this.data.productID;
  130. item.getComponent(ProductShow).id = id;
  131. item.parent = this.showGroup;
  132. }
  133. }
  134. private clean(): void {
  135. this.showGroup.removeAllChildren();
  136. }
  137. isCanTouch = true;
  138. public async onTouch() {
  139. if (!this.isCanTouch) {
  140. return;
  141. }
  142. switch (this.data.state) {
  143. case PastureState.Lock:
  144. let config = gData.gameData.getProductMap(this.data.productID);
  145. if (config.unlock == 2) {
  146. if (gData.gameData.playerProp.orderData) {
  147. let times = config.value - gData.gameData.playerProp.orderData.overTimes;
  148. mk.tip.pop(`订单提现${times}次后解锁`);
  149. } else {
  150. mk.tip.pop(`提现订单后解锁`);
  151. }
  152. }
  153. else if (config.unlock == 1) {
  154. mk.tip.pop(`喂食${config.name}${config.value}次解锁`)
  155. }
  156. break;
  157. case PastureState.Empty:
  158. if (!this.emptyClick) {
  159. this.emptyClick = true;
  160. this.eatNode.active = true;
  161. }
  162. else {
  163. //调用喂食
  164. // this.onEat();
  165. this.eatNode.active = false;
  166. this.emptyClick = false;
  167. }
  168. break;
  169. case PastureState.Growing:
  170. mk.ui.openPanel('module/speedUpUI/speedUp');
  171. break;
  172. case PastureState.Ripe:
  173. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 1);
  174. this.isCanTouch = false;
  175. setTimeout(() => {
  176. this.isCanTouch = true;
  177. }, 1000);
  178. break;
  179. case PastureState.Sick:
  180. // mk.tip.pop(`生虫了!`);
  181. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  182. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  183. break;
  184. }
  185. }
  186. async clickEatBtn() {
  187. let flyRed = await this.onEat();
  188. if (flyRed) {
  189. //mk.audio.playEffect('redmoney');
  190. if (gData.gameData.playerProp.userFarmTaskInfo) {
  191. let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
  192. let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
  193. let addAni = cc.instantiate(this.node_ani);
  194. addAni.parent = this.node_ani.parent;
  195. addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
  196. cc.tween(addAni).delay(0.6).call(() => {
  197. addAni.opacity = 255;
  198. }).by(0.8, { y: 100 }).to(0.4, { opacity: 0 }).call(() => {
  199. addAni.destroy();
  200. }).start();
  201. }
  202. let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
  203. gData.gameData.gameStyle.dpFlyRedAni(pos);
  204. }
  205. }
  206. async onEat() {
  207. if (gData.gameData.leftTimes <= 0) {
  208. mk.ui.openPanel('module/speedUpUI/productReward');
  209. return false;
  210. }
  211. if (this.state == PastureState.Empty) {
  212. gData.gameData.isProducting = true;
  213. let config = gData.gameData.getProductMap(this.data.productID);
  214. let growSpan = Date.now() + config.time * 1000;
  215. this.data.state = PastureState.Growing;
  216. this.data.productID = this.data.productID;
  217. this.data.growSpan = growSpan;
  218. this.eatNode.active = false;
  219. this.getComponent(MoveToCenter).move();
  220. try {
  221. gData.gameData.setPastureDataMap(this.configID, this.data);
  222. gData.gameData.changeLeftTimes(-1);
  223. await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node);
  224. } catch (error) {
  225. console.log("=======pastureIcon");
  226. }
  227. //gData.gameData.updateOrderTaskCopyData(this.data.productID);
  228. gData.gameData.nextMake = null;
  229. gData.gameData.setNextProduct(false);
  230. gData.gameData.isProducting = false;
  231. let flyRed = await gData.gameData.updateNewTaskProgress();
  232. return flyRed;
  233. // if (flyRed) {
  234. // //mk.audio.playEffect('redmoney');
  235. // if (gData.gameData.playerProp.userFarmTaskInfo) {
  236. // let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
  237. // let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
  238. // let addAni = cc.instantiate(this.node_ani);
  239. // addAni.parent = this.node_ani.parent;
  240. // addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
  241. // cc.tween(addAni).delay(0.6).call(()=>{
  242. // addAni.opacity = 255;
  243. // }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
  244. // addAni.destroy();
  245. // }).start();
  246. // }
  247. // let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
  248. // gData.gameData.gameStyle.dpFlyRedAni(pos);
  249. // }
  250. } else {
  251. return false;
  252. }
  253. }
  254. clickSpeedUp() {
  255. mk.ui.openPanel('module/speedUpUI/speedUp');
  256. }
  257. onRipe(sendToServer) {
  258. this.data.state = PastureState.Ripe;
  259. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  260. gData.gameData.addProductMakeTimesById(this.data.productID);
  261. }
  262. onSick(sendToServer) {
  263. this.data.state = PastureState.Sick;
  264. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  265. }
  266. public async onHarvest() {
  267. this.countDown.cleanRiped();
  268. this.clean();
  269. this.data.state = PastureState.Empty;
  270. this.data.growSpan = 0;
  271. gData.gameData.setPastureDataMap(this.configID, this.data);
  272. //await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node);
  273. //gData.gameData.nextMake = null;
  274. gData.gameData.setNextProduct(false);
  275. if (gData.harvestData.getType == 0) {
  276. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  277. }
  278. else {
  279. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  280. }
  281. }
  282. public canHarvest() {
  283. this.getComponent(MoveToCenter).move();
  284. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 1);
  285. }
  286. public canClearSick() {
  287. this.getComponent(MoveToCenter).move();
  288. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  289. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  290. }
  291. }