PastureIcon.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { _decorator, Component, Node, Animation, Sprite, SpriteFrame, JsonAsset, ProgressBar, Prefab, instantiate, UITransform, v3, Label } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  4. import { Sound } from '../core/sound/Sound';
  5. import { BitmapFont } from '../core/ui/BitmapFont';
  6. import { WindowManager } from '../core/ui/window/WindowManager';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { Utils } from '../core/utils/Utils';
  9. import { ConfigData } from '../Data/ConfigData';
  10. import { g } from '../Data/g';
  11. import { platform } from '../Data/platform';
  12. import { TakeAWalk } from '../public/TakeAWalk';
  13. import { FactoryCountDown } from '../UI/Factory/FactoryCountDown';
  14. import { ProductShow } from '../UI/Factory/ProductShow';
  15. import { Animal, AnimalState } from './Pasture/Animal';
  16. const { ccclass, property } = _decorator;
  17. @ccclass('PastureIcon')
  18. export class PastureIcon extends Component {
  19. @property({ tooltip: "点击是否缩放动画" }) touchAmition: boolean = true;
  20. @property({ type: Animation, tooltip: "点击动画" }) animation: Animation;
  21. @property({ type: Node, tooltip: "建筑状态组" }) stateGroup: Node;
  22. @property({ type: Node, tooltip: "完成品展示节点" }) showGroup: Node;
  23. @property({ tooltip: "配置ID" }) configID: number = 0;
  24. @property({ type: FactoryCountDown, tooltip: "计时装置" }) countDown: FactoryCountDown;
  25. @property({ type: Node, tooltip: "喂养组" }) feedGroup: Node;
  26. @property({ type: Sprite, tooltip: "饲料图标" }) feedIcon: Sprite;
  27. @property({ type: Animation, tooltip: "喂养动画" }) feedAnimation: Animation;
  28. @property({ type: Label, tooltip: "喂食剩余次数" }) feedLabel: Label;
  29. @property({ type: Node, tooltip: "进度组" }) countDownGroup: Node;
  30. @property({ type: ProgressBar, tooltip: "倒计时条" }) countDownProgress: ProgressBar;
  31. @property({ type: BitmapFont, tooltip: "倒计时数字" }) countDownLabel: BitmapFont;
  32. @property({ type: Sprite, tooltip: "倒计时图标" }) countDownIcon: Sprite;
  33. @property({ type: Node, tooltip: "动物图标" }) animalParent: Node;
  34. @property({ type: Prefab, tooltip: "动物预制体" }) animalPrefabs: Prefab;
  35. @property({ type: [Node], tooltip: "活动点" }) walkPoints: Array<Node> = [];
  36. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  37. private _state: PastureState = PastureState.Lock;
  38. public get state(): PastureState {
  39. return this._state;
  40. }
  41. private hasActive = false;
  42. private havrestList: { id: number, node: Node }[] = [];
  43. public canShowFeedGroup = false;
  44. public canShowCountDownGroup = false;
  45. private feedCount = 0;
  46. private animals: Array<Animal> = [];
  47. async start() {
  48. if (g.gameData.hasBuildData(this.configID)) {
  49. this.activeFactroy();
  50. }
  51. this.feedIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("pasture_icons/pasture_feed_" + this.configID + "/spriteFrame", SpriteFrame, this.node);
  52. this.countDownIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("pasture_icons/pasture_speed_" + this.configID + "/spriteFrame", SpriteFrame, this.node);
  53. for (let i = 0; i < 5; i++) {
  54. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/pastureAnimals/paster_" + this.configID, Prefab, this.node);
  55. let animal = instantiate(prefab);
  56. animal.getComponent(TakeAWalk).points = this.walkPoints;
  57. animal.setPosition(this.walkPoints[Utils.random_both(i * 5, i * 5 + 4)].position);
  58. animal.setParent(this.animalParent);
  59. this.animals.push(animal.getComponent(Animal));
  60. }
  61. }
  62. update() {
  63. let producting = g.gameData.getProductingList(this.configID);
  64. if (producting.length > 0) {
  65. this.setState(PastureState.Producting);
  66. } else {
  67. this.setState(PastureState.Free);
  68. }
  69. if (this.state == PastureState.Lock) {
  70. if (g.gameData.hasBuildData(this.configID)) {
  71. this.activeFactroy();
  72. }
  73. }
  74. this.upDataAnimals();
  75. this.feedGroup.active = this.canShowFeedGroup && producting.length < g.gameData.getUnlock(this.configID);
  76. this.countDownGroup.active = this.canShowCountDownGroup && producting.length > 0;
  77. this.countDownProgress.progress = 1 - this.countDown.progress;
  78. this.countDownLabel.string = this.countDown.timerString;
  79. }
  80. public activeFactroy(): void {
  81. this.hasActive = true;
  82. }
  83. public async inPolygon() {
  84. if (this.touchAmition) {
  85. this.animation.play();
  86. }
  87. this.getComponent(Sound).play();
  88. if (this.state != PastureState.Lock) {
  89. if (this.havrestList.length > 0) {
  90. WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this));
  91. this.canShowFeedGroup = this.canShowCountDownGroup = false;
  92. } else {
  93. let productID = ConfigData.configMap.get("build")[this.configID]["product"][0];
  94. let result = await this.http.send("/api/product/getProductTimes", { productIDList: productID + "" });
  95. if (result.code == 0) {
  96. let countData = result.data;
  97. this.feedCount = ConfigData.configMap.get("product")[productID]["reap"] - (countData[productID] ? countData[productID] : 0);
  98. this.feedLabel.string = this.feedCount + "";
  99. this.canShowFeedGroup = this.canShowCountDownGroup = true;
  100. }
  101. }
  102. } else {
  103. let lvl = ConfigData.configMap.get("build")[this.configID]["lvl"];
  104. WindowManager.showTips(`${lvl}级解锁`);
  105. }
  106. }
  107. private getThankingKey(): string {
  108. let key = "";
  109. switch (this.configID) {
  110. case 30001: key = "chick"; break;
  111. case 30002: key = "cow"; break;
  112. case 30003: key = "pig"; break;
  113. case 30004: key = "corn"; break;
  114. case 30005: key = "milk"; break;
  115. case 30006: key = "sugar"; break;
  116. case 30007: key = "cake"; break;
  117. case 30008: key = "fastfood"; break;
  118. case 30009: key = "noodle"; break;
  119. }
  120. return key;
  121. }
  122. public async onFeedButton() {
  123. if (this.feedCount > 0) {
  124. let producting = g.gameData.getProductingList(this.configID);
  125. let productID = ConfigData.configMap.get("build")[this.configID]["product"][0];
  126. if (producting.length < g.gameData.getUnlock(this.configID)) {
  127. platform.umUp("factoryClick");//牧场下单
  128. let result = await this.http.send("/api/product/createOrder", { buildID: this.configID, productID: productID });
  129. if (result.code == 0) {
  130. platform.reportThinking("feed", JSON.stringify({ farm_name: this.getThankingKey(), feed_name: productID, id: g.userData.id, level: g.userData.getLevel(), role_name: g.userData.nickName, feed_time: Utils.formatDate(new Date()) }));
  131. this.feedAnimation.play();
  132. this.feedCount--;
  133. this.feedLabel.string = this.feedCount + "";
  134. let time = ConfigData.configMap.get("product")[productID]["time"];
  135. producting.push({ productID: productID, ripeDate: Date.now() + time * 1000 });
  136. if (result.data.productPrize) {
  137. g.gameData.productPrize = result.data.productPrize;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. public async onSpeedButton() {
  144. if (g.userData.diamond >= 25) {
  145. let result = await this.http.send("/api/product/quickProduct", { buildID: this.configID });
  146. if (result.code == 0) {
  147. platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" }));
  148. g.userData.diamond -= result.data;
  149. this.countDown.ripe();
  150. this.canShowFeedGroup = false;
  151. this.canShowCountDownGroup = false;
  152. return;
  153. }
  154. }
  155. WindowManager.showTips("钻石不足");
  156. }
  157. public onHarvest(mult: number): void {
  158. this.havrestList.shift().node.destroy();
  159. }
  160. /**生产完成回调 */
  161. public async onProductComplete(id: number) {
  162. if (this.showGroup) {
  163. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/ProductShow", null, this.node);
  164. let item = instantiate(prefab);
  165. item.getComponent(ProductShow).id = id;
  166. item.setParent(this.showGroup);
  167. let x = Utils.random_both(-this.showGroup.getComponent(UITransform).width / 2, this.showGroup.getComponent(UITransform).width / 2);
  168. let y = Utils.random_both(-this.showGroup.getComponent(UITransform).height / 2, this.showGroup.getComponent(UITransform).height / 2);
  169. item.setPosition(v3(x, y, 0));
  170. this.havrestList.push({ id: id, node: item });
  171. }
  172. }
  173. public clean(): void {
  174. this.showGroup.removeAllChildren();
  175. this.havrestList = [];
  176. }
  177. private setState(value: PastureState): void {
  178. if (this.hasActive) {
  179. if (this._state != value) {
  180. this._state = value;
  181. if (this._state == PastureState.Producting) {
  182. this.stateGroup && (this.stateGroup.active = true);
  183. } else {
  184. this.stateGroup && (this.stateGroup.active = false);
  185. }
  186. }
  187. }
  188. }
  189. private upDataAnimals(): void {
  190. let producting = g.gameData.getProductingList(this.configID);
  191. if (this.animals.length == 5) {
  192. for (let i = 0; i < producting.length; i++) {
  193. this.animals[i].state = AnimalState.Wait;
  194. }
  195. for (let j = 0; j < this.havrestList.length; j++) {
  196. this.animals[producting.length + j].state = AnimalState.Out;
  197. }
  198. for (let k = 0; k < this.animals.length - this.havrestList.length - producting.length; k++) {
  199. this.animals[producting.length + this.havrestList.length + k].state = AnimalState.Hungry;
  200. }
  201. }
  202. }
  203. }
  204. export enum PastureState {
  205. Lock,
  206. Producting,
  207. Free
  208. }