FarmIcon.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { _decorator, Component, Node, find, Sprite, SpriteFrame, Animation, director, game } 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 { OpenWindow } from '../core/ui/window/OpenWindow';
  6. import { WindowManager } from '../core/ui/window/WindowManager';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { ConfigData } from '../Data/ConfigData';
  9. import { g } from '../Data/g';
  10. import { platform } from '../Data/platform';
  11. import { MoveToCenter } from '../Map/MoveToCenter';
  12. import { PlantWindow } from '../UI/Farm/PlantWindow';
  13. import { FarmCountDown } from './FarmCountDown';
  14. import { FarmGrowProcess } from './FarmGrowProcess';
  15. import { FarmManager } from './FarmManager';
  16. import { FarmSystem } from './FarmSystem';
  17. import { IDHelper } from './IDHelper';
  18. const { ccclass, property } = _decorator;
  19. @ccclass('FarmIcon')
  20. export class FarmIcon extends Component {
  21. @property({ type: Node, tooltip: "未解锁时显示节点" }) lockNode: Node;
  22. @property({ type: Node, tooltip: "选中闪烁节点" }) selectNode: Node;
  23. @property({ type: Sprite, tooltip: "种植动画图标" }) plantIcon: Sprite;
  24. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown;
  25. @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess;
  26. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  27. public configID: number = 0;
  28. public sortID: number = 0;
  29. private farmSystem: FarmSystem;
  30. private plantID: number = 0;
  31. private _state: FarmState = FarmState.Lock;
  32. private hasActive = false;
  33. public get state(): FarmState { return this._state; }
  34. onLoad() {
  35. this.configID = this.getComponent(IDHelper).buildID;
  36. this.sortID = this.getComponent(IDHelper).sortID;
  37. this.checkFarmSystem();
  38. this.lockNode.active = !(this.selectNode.active = false);
  39. }
  40. start() {
  41. if (g.gameData.getProductingList(this.configID)[0]) {
  42. this.countDown.setData(this.configID, false);
  43. this.process.setData(this.configID);
  44. }
  45. }
  46. update() {
  47. if (this.state == FarmState.Lock && g.gameData.hasBuildData(this.configID)) {
  48. this.activeFram();
  49. }
  50. let producting = g.gameData.getProductingList(this.configID);
  51. if (producting.length > 0) {
  52. if (producting[0].ripeDate <= Date.now()) {
  53. this.setState(FarmState.Ripe);
  54. } else {
  55. this.setState(FarmState.Growing);
  56. }
  57. this.countDown.setData(this.configID, false);
  58. this.process.setData(this.configID);
  59. } else {
  60. this.setState(FarmState.Empty);
  61. this.countDown.cleanRiped();
  62. this.process.cleanProcess();
  63. }
  64. }
  65. private setState(value: FarmState): void {
  66. if (this.hasActive) {
  67. if (this.state != value) {
  68. this._state = value;
  69. }
  70. }
  71. }
  72. private checkFarmSystem() {
  73. var frameSystemNode = find("Canvas/FarmSystem");
  74. var farmSystem: FarmSystem;
  75. if (!frameSystemNode) {//如果没有窗口系统节点则创建一个
  76. var frameSystemNode = new Node("FarmSystem");
  77. farmSystem = frameSystemNode.addComponent(FarmSystem);
  78. }
  79. else {
  80. farmSystem = frameSystemNode.getComponent(FarmSystem);
  81. }
  82. this.farmSystem = farmSystem || frameSystemNode.addComponent(FarmSystem);//如果没有窗口系统脚本则创建一个
  83. this.farmSystem.addFram(this);
  84. if (!frameSystemNode.parent) {
  85. frameSystemNode.parent = find("Canvas");
  86. }
  87. }
  88. public activeFram(): void {
  89. this.lockNode.active = false;
  90. this.hasActive = true;
  91. }
  92. public selectFarm(isSelect = false): void {
  93. this.selectNode.active = isSelect;
  94. if (isSelect) {
  95. this.getComponent(MoveToCenter).move();
  96. }
  97. }
  98. public async onTouch() {
  99. if (this.state != FarmState.Lock) {
  100. if (this.state == FarmState.Empty) {
  101. var sound = this.getComponent(Sound);
  102. sound && sound.play();
  103. this.getComponent(OpenWindow).open(this.configID);
  104. } else if (this.state == FarmState.Growing) {
  105. this.countDown.setCountActive(true);
  106. } else if (this.state == FarmState.Ripe) {
  107. WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this));
  108. }
  109. } else {
  110. let config = ConfigData.configMap.get("build")[this.configID];
  111. if (config["unlockVideo"] != 1) {
  112. let lvl = config["lvl"];
  113. WindowManager.showTips(`${lvl}级解锁`);
  114. } else {
  115. if (g.gameData.hasBuildData(this.configID - 1)) {
  116. WindowManager.open("Prefabs/ADUnlockWindow", WindowOpenMode.CloseAndCover, this.configID);
  117. platform.umUp("videoExpansion");//视频扩充埋点
  118. }
  119. }
  120. }
  121. }
  122. public onHarvest(): void {
  123. this.countDown.cleanRiped();
  124. this.process.cleanProcess();
  125. this.farmSystem.currSelectFarm = null;
  126. g.gameData.getProductingList(this.configID).shift();
  127. }
  128. public async plant(id: number) {
  129. if (this.state == FarmState.Empty && !FarmManager.plantIsRequesting) {
  130. FarmManager.plantIsRequesting = true;
  131. let result = await this.http.send("/api/product/createOrder", { buildID: this.configID, productID: id });
  132. if (result.code == 0) {
  133. this.plantID = id;
  134. FarmManager.plantCountMap.set(this.plantID, FarmManager.plantCountMap.get(this.plantID) - 1);
  135. if (FarmManager.plantCountMap.get(this.plantID) == 0) {
  136. PlantWindow.needReLoad = true;
  137. }
  138. this.plantIcon.node.active = true;
  139. this.plantIcon.spriteFrame = await ResourcesUtils.load<SpriteFrame>("plant_icons/" + "plantIcon_" + this.plantID + "/spriteFrame", SpriteFrame, this.node);
  140. this.plantIcon.getComponent(Animation).play();
  141. this.plantIcon.getComponent(Sound).play();
  142. this.plantIcon.getComponent(Animation).on(Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  143. let config = ConfigData.configMap.get("product")[this.plantID];
  144. g.gameData.getProductingList(this.configID).push({ productID: this.plantID, ripeDate: Date.now() + config["time"] * 1000 });
  145. this.farmSystem.selectNextFarm();
  146. if (result.data.productPrize) {
  147. g.gameData.productPrize = result.data.productPrize;
  148. }
  149. }
  150. // else {
  151. // WindowManager.showTips("请求失败");
  152. // }
  153. FarmManager.plantIsRequesting = false;
  154. }
  155. }
  156. public onRiped(): void {
  157. this._state = FarmState.Ripe;
  158. g.gameData.getProductingList(this.configID)[0].ripeDate = Date.now();
  159. }
  160. private async onPlantAnimationFinish() {
  161. this.plantIcon.getComponent(Animation).off(Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  162. this.plantIcon.node.active = false;
  163. this.farmSystem.hideAllSelectProgressGroup();
  164. this.countDown.setData(this.configID);
  165. this.process.setData(this.configID);
  166. }
  167. public novice_plant(): void {
  168. this.process.setData(this.configID);
  169. }
  170. public novice_clean(): void {
  171. this.process.cleanProcess();
  172. }
  173. }
  174. export enum FarmState {
  175. Lock,
  176. Empty,
  177. Growing,
  178. Ripe
  179. }