PastureIcon.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. public async onTouch() {
  128. switch (this.data.state) {
  129. case PastureState.Lock:
  130. let config = gData.gameData.getProductMap(this.data.productID);
  131. if (config.unlock == 2) {
  132. let times =config.value - gData.gameData.playerProp.completeFarmTaskTimes;
  133. mk.tip.pop(`完成${times}次任务红包后解锁`);
  134. }
  135. else if (config.unlock == 1) {
  136. mk.tip.pop(`喂食${config.name}${config.value}次解锁`)
  137. }
  138. break;
  139. case PastureState.Empty:
  140. if (!this.emptyClick) {
  141. this.emptyClick = true;
  142. this.eatNode.active = true;
  143. }
  144. else {
  145. //调用喂食
  146. // this.onEat();
  147. this.eatNode.active = false;
  148. this.emptyClick = false;
  149. }
  150. break;
  151. case PastureState.Growing:
  152. mk.ui.openPanel('module/speedUpUI/speedUp');
  153. break;
  154. case PastureState.Ripe:
  155. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  156. break;
  157. case PastureState.Sick:
  158. // mk.tip.pop(`生虫了!`);
  159. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  160. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  161. break;
  162. }
  163. }
  164. async clickEatBtn()
  165. {
  166. let flyRed = await this.onEat();
  167. if (flyRed) {
  168. //mk.audio.playEffect('redmoney');
  169. if (gData.gameData.playerProp.userFarmTaskInfo) {
  170. let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
  171. let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
  172. let addAni = cc.instantiate(this.node_ani);
  173. addAni.parent = this.node_ani.parent;
  174. addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
  175. cc.tween(addAni).delay(0.6).call(()=>{
  176. addAni.opacity = 255;
  177. }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
  178. addAni.destroy();
  179. }).start();
  180. }
  181. let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
  182. gData.gameData.gameStyle.dpFlyRedAni(pos);
  183. }
  184. }
  185. async onEat() {
  186. if (gData.gameData.leftTimes <= 0) {
  187. mk.ui.openPanel('module/speedUpUI/productReward');
  188. return false;
  189. }
  190. if (this.state == PastureState.Empty) {
  191. gData.gameData.isProducting = true;
  192. let config = gData.gameData.getProductMap(this.data.productID);
  193. let growSpan = Date.now() + config.time * 1000;
  194. this.data.state = PastureState.Growing;
  195. this.data.productID = this.data.productID;
  196. this.data.growSpan = growSpan;
  197. gData.gameData.setPastureDataMap(this.configID, this.data);
  198. this.eatNode.active = false;
  199. this.getComponent(MoveToCenter).move();
  200. gData.gameData.changeLeftTimes(-1);
  201. gData.gameData.isProducting = false;
  202. gData.gameData.nextMake = null;
  203. gData.gameData.setNextProduct(false);
  204. let flyRed = await gData.gameData.updateNewTaskProgress();
  205. return flyRed;
  206. // if (flyRed) {
  207. // //mk.audio.playEffect('redmoney');
  208. // if (gData.gameData.playerProp.userFarmTaskInfo) {
  209. // let com = gData.gameData.playerProp.userFarmTaskInfo.completeCount;
  210. // let count = gData.gameData.playerProp.userFarmTaskInfo.taskCount;
  211. // let addAni = cc.instantiate(this.node_ani);
  212. // addAni.parent = this.node_ani.parent;
  213. // addAni.getComponent(cc.Label).string = `百元红包:${com}/${count}`;
  214. // cc.tween(addAni).delay(0.6).call(()=>{
  215. // addAni.opacity = 255;
  216. // }).by(0.8, {y : 100}).to(0.4, {opacity : 0}).call(()=>{
  217. // addAni.destroy();
  218. // }).start();
  219. // }
  220. // let pos = this.eatNode.parent.convertToWorldSpaceAR(this.eatNode.getPosition());
  221. // gData.gameData.gameStyle.dpFlyRedAni(pos);
  222. // }
  223. }else{
  224. return false;
  225. }
  226. }
  227. clickSpeedUp() {
  228. mk.ui.openPanel('module/speedUpUI/speedUp');
  229. }
  230. onRipe(sendToServer) {
  231. this.data.state = PastureState.Ripe;
  232. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  233. gData.gameData.addProductMakeTimesById(this.data.productID);
  234. }
  235. onSick(sendToServer) {
  236. this.data.state = PastureState.Sick;
  237. gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
  238. }
  239. public onHarvest(): void {
  240. this.countDown.cleanRiped();
  241. this.clean();
  242. this.data.state = PastureState.Empty;
  243. this.data.growSpan = 0;
  244. gData.gameData.setPastureDataMap(this.configID, this.data);
  245. gData.gameData.setNextProduct(false);
  246. if (gData.harvestData.getType == 0) {
  247. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  248. }
  249. else {
  250. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  251. }
  252. }
  253. public canHarvest() {
  254. this.getComponent(MoveToCenter).move();
  255. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  256. }
  257. public canClearSick() {
  258. this.getComponent(MoveToCenter).move();
  259. mk.data.sendDataEvent(DataEventId.button_click, "生病icon");
  260. gData.adClearSickData.openPanel(ProductType.dw, this.configID);
  261. }
  262. }