PastureIcon.ts 13 KB

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