GameDataWatcher.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { _decorator, Component, Node, instantiate, Prefab, find } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  4. import { WindowManager } from '../core/ui/window/WindowManager';
  5. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  6. import { FarmManager } from '../Main/FarmManager';
  7. import { PastureSystem } from '../Main/Pasture/PastureSystem';
  8. import { FactorySystem } from '../UI/Factory/FactorySystem';
  9. import { ConfigData } from './ConfigData';
  10. import { g } from './g';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('GameDataWatcher')
  13. export class GameDataWatcher extends Component {
  14. @property({ type: [Node], tooltip: "视频解锁土地图标" }) adUnlockNodes: Array<Node> = [];
  15. private _level = 0;
  16. @property({ tooltip: "网络请求对象", type: Http })
  17. public http: Http;
  18. @property({ tooltip: "工厂系统", type: FactorySystem })
  19. public factorySys: FactorySystem;
  20. @property({ tooltip: "牧场系统", type: PastureSystem })
  21. public pastureSys: PastureSystem;
  22. private hongbaoWindowDelay = 0;
  23. async start() {
  24. FarmManager.plantWindowIsOpening = false;
  25. if (!g.userData.noviceReceived) {
  26. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/新手引导", Prefab, this.node);
  27. let novice = instantiate(prefab);
  28. novice.setParent(find("Canvas/UI"));
  29. }
  30. }
  31. update(dt) {
  32. for (let i = 0; i < this.adUnlockNodes.length; i++) {
  33. if (!g.gameData.hasBuildData(31003 + i) && g.gameData.hasBuildData(31003 + i - 1)) {
  34. this.adUnlockNodes[i].active = true;
  35. } else {
  36. this.adUnlockNodes[i].active = false;
  37. }
  38. }
  39. // this.adUnlockNodes[0].active && (this.adUnlockNodes[0].active = !g.gameData.hasBuildData(31003));
  40. // this.adUnlockNodes[1].active && (this.adUnlockNodes[1].active = !g.gameData.hasBuildData(31006));
  41. if (WindowManager.getWindowList().length == 0) {
  42. this.hongbaoWindowDelay += dt;
  43. if (this.hongbaoWindowDelay >= 120) {
  44. WindowManager.open("Prefabs/Hongbao", WindowOpenMode.NotCloseAndAdd);
  45. this.hongbaoWindowDelay = 0;
  46. }
  47. }
  48. if (this._level != g.userData.getLevel()) {
  49. this._level = g.userData.getLevel();
  50. let config = ConfigData.configMap.get("build");
  51. for (let k in config) {
  52. if (config[k].lvl <= this._level && config[k].unlockVideo == 0) {
  53. if (!g.gameData.hasBuildData(config[k].id)) {
  54. g.gameData.setBuildData(config[k].id, 0);
  55. }
  56. }
  57. }
  58. }
  59. if (g.gameData.productPrize != null) {
  60. WindowManager.open("Prefabs/PlantRewardWindow", WindowOpenMode.NotCloseAndAdd, g.gameData.productPrize);
  61. g.gameData.productPrize = null;
  62. }
  63. if (g.gameData.needReloadData) {
  64. g.gameData.needReloadData = false;
  65. this.reloadGameData();
  66. }
  67. }
  68. private async reloadGameData() {
  69. this.factorySys.clean();
  70. this.pastureSys.clean();
  71. let gameData = await this.http.send("/api/product/getBuildData");
  72. if (gameData.code == 0) {
  73. g.gameData.setGameData(gameData.data);
  74. if (WindowManager.getWindowList().length > 0) {
  75. WindowManager.close();
  76. }
  77. } else {
  78. WindowManager.showTips("获取游戏数据失败,请检查网络");
  79. }
  80. }
  81. }