import { _decorator, Component, Node, instantiate, Prefab, find } from 'cc'; import { Http } from '../core/net/Http'; import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils'; import { WindowManager } from '../core/ui/window/WindowManager'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { FarmManager } from '../Main/FarmManager'; import { PastureSystem } from '../Main/Pasture/PastureSystem'; import { FactorySystem } from '../UI/Factory/FactorySystem'; import { ConfigData } from './ConfigData'; import { g } from './g'; const { ccclass, property } = _decorator; @ccclass('GameDataWatcher') export class GameDataWatcher extends Component { @property({ type: [Node], tooltip: "视频解锁土地图标" }) adUnlockNodes: Array = []; private _level = 0; @property({ tooltip: "网络请求对象", type: Http }) public http: Http; @property({ tooltip: "工厂系统", type: FactorySystem }) public factorySys: FactorySystem; @property({ tooltip: "牧场系统", type: PastureSystem }) public pastureSys: PastureSystem; private hongbaoWindowDelay = 0; async start() { FarmManager.plantWindowIsOpening = false; if (!g.userData.noviceReceived) { let prefab = await ResourcesUtils.load("Prefabs/新手引导", Prefab, this.node); let novice = instantiate(prefab); novice.setParent(find("Canvas/UI")); } } update(dt) { for (let i = 0; i < this.adUnlockNodes.length; i++) { if (!g.gameData.hasBuildData(31003 + i) && g.gameData.hasBuildData(31003 + i - 1)) { this.adUnlockNodes[i].active = true; } else { this.adUnlockNodes[i].active = false; } } // this.adUnlockNodes[0].active && (this.adUnlockNodes[0].active = !g.gameData.hasBuildData(31003)); // this.adUnlockNodes[1].active && (this.adUnlockNodes[1].active = !g.gameData.hasBuildData(31006)); if (WindowManager.getWindowList().length == 0) { this.hongbaoWindowDelay += dt; if (this.hongbaoWindowDelay >= 120) { WindowManager.open("Prefabs/Hongbao", WindowOpenMode.NotCloseAndAdd); this.hongbaoWindowDelay = 0; } } if (this._level != g.userData.getLevel()) { this._level = g.userData.getLevel(); let config = ConfigData.configMap.get("build"); for (let k in config) { if (config[k].lvl <= this._level && config[k].unlockVideo == 0) { if (!g.gameData.hasBuildData(config[k].id)) { g.gameData.setBuildData(config[k].id, 0); } } } } if (g.gameData.productPrize != null) { WindowManager.open("Prefabs/PlantRewardWindow", WindowOpenMode.NotCloseAndAdd, g.gameData.productPrize); g.gameData.productPrize = null; } if (g.gameData.needReloadData) { g.gameData.needReloadData = false; this.reloadGameData(); } } private async reloadGameData() { this.factorySys.clean(); this.pastureSys.clean(); let gameData = await this.http.send("/api/product/getBuildData"); if (gameData.code == 0) { g.gameData.setGameData(gameData.data); if (WindowManager.getWindowList().length > 0) { WindowManager.close(); } } else { WindowManager.showTips("获取游戏数据失败,请检查网络"); } } }