GameData.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. var reGameStates = require('GameStates');
  2. var AConfig = require('../Config');
  3. import utils from "../Network/netUtils";
  4. import GameNet from "../Network/GameNet"
  5. import gameToast from "../Network/gameToast"
  6. //全局数据类
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. //记录全部道路的index
  11. GameData_highwayIndex: {
  12. default: [],
  13. type: [cc.Integer],
  14. visible: false,
  15. serializable: false,
  16. },
  17. //记录建筑物存储的信息
  18. GameData_buildings: {
  19. default: [],
  20. visible: false,
  21. serializable: false,
  22. },
  23. HighWayPrefabs: cc.Prefab,
  24. //MyMapNode
  25. //建筑物的节点
  26. BuildingsParent: {
  27. default: null,
  28. type: cc.Node,
  29. },
  30. ManageUI: {
  31. default: null,
  32. type: cc.Node,
  33. },
  34. GameVersion: {
  35. default: -1,
  36. type: cc.Integer,
  37. },
  38. //读取数据
  39. readData: {
  40. default: null,
  41. visible: false,
  42. },
  43. /**
  44. * 接口
  45. * todo cnt和snb 由钱包读取
  46. */
  47. CNT: {
  48. default: 0,
  49. visible: false,
  50. },
  51. SNB: {
  52. default: 0,
  53. visible: false,
  54. },
  55. //读取的土地文件
  56. ConfigLand: {
  57. default: null,
  58. visible: false
  59. },
  60. toast: null,
  61. scheduleObj: null,
  62. toastCallback: null,
  63. isStart: true,
  64. /**
  65. * 限制重复触发支付, 正在支付中转态,
  66. */
  67. isPayingCnt:false,
  68. isPlayingSnb:false,
  69. },
  70. onLoad() {
  71. //初始化GameData全局变量
  72. GlobalD.GameData = this;
  73. this.GameConfig();
  74. },
  75. GameConfig() {
  76. this.AddBuildingCost = 5;
  77. this.RemoveBuildingCost = 5;
  78. //
  79. this.Dapp = {
  80. UserInfo: null
  81. }
  82. },
  83. start() {
  84. this._tiledMap = GlobalD.TiledMap._tiledMap;
  85. //调用初始化dapp
  86. this.isDebugMode(GlobalD.dapp);
  87. },
  88. /**
  89. * 根据 不存在dapp 的话,使用本地测试
  90. * @param {*} bInit
  91. */
  92. isDebugMode: function (bInit) {
  93. if (bInit) {
  94. //读取网络数据
  95. try {
  96. GlobalD.dapp.cntBalance().then((cntBalance) => {
  97. //会延迟返回
  98. // console.log("获取cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  99. GlobalD.GameData.SetCNT(cntBalance);
  100. });
  101. } catch (err) {
  102. console.error(err) // 初始化失败,运行环境不是钱包环境
  103. }
  104. //登录时候已经初始化好snb了
  105. this.SNB = GlobalD.UserInfo.snb;
  106. this.readData = userData.readData;
  107. // cc.log('playerPullInfo 读取到数据?:', this.readData)
  108. this.InitNextworkData();
  109. this.InitSceneInfo();
  110. // 自动存储数据
  111. /**
  112. * todo 如果退出游戏,触发一次存储。
  113. */
  114. this.AutoSaveData = function () {
  115. this.pushData(true);
  116. };
  117. this.schedule(this.AutoSaveData, 20);
  118. } else {
  119. // 清除
  120. cc.log("本地数据重新开始,并且不初始化");
  121. // this.onClearAllData();
  122. this.Init();
  123. this.InitSceneInfo();
  124. this.AutoSaveData = function () {
  125. this.pushData(false);
  126. };
  127. this.schedule(this.AutoSaveData, 20);
  128. }
  129. },
  130. getData: function () {
  131. //如果开局没有读取到网络数据,就这里初始化
  132. if (userData.readData == null) {
  133. cc.log("本地数据:", cc.sys.localStorage.getItem('userdata'));
  134. let userdata = cc.sys.localStorage.getItem('userdata');
  135. if (userdata) {
  136. this.readData = JSON.parse(userdata);
  137. }
  138. this.Init();
  139. this.InitSceneInfo();
  140. } else {
  141. this.readData = userData.readData;
  142. cc.log('读取到数据?:', this.readData)
  143. this.Init();
  144. this.InitSceneInfo();
  145. }
  146. },
  147. InitSceneInfo() {
  148. //根据顺序生成
  149. //初始化时间ui
  150. cc.find("GameNode/ManageTimer").getComponent('ManageTimer').Init();
  151. //初始化金钱ui,gold,diamond, cnt,snb,
  152. cc.find("GameNode/ManageGolden").getComponent('ManageGolden').InitManageGlodenUI();
  153. // //初始化地图物件
  154. // cc.find("GameNode/ManageMap").getComponent('ManageMap').InitManageMap();
  155. GlobalD.TiledMap.onInitSolid();
  156. //初始化生成房屋
  157. /**
  158. * dapp设定: 如果固定土地冲突,删除冲突的建筑和回收仓库
  159. */
  160. this.ManageUI.getComponent('ManageBuildings').InitBuildings();
  161. //初始化道路
  162. /**
  163. * 原本游戏设定是先生产道路,现在为了方便处理等房屋生产后,再处理公路
  164. * dapp设定: 如果固定土地冲突,删除对应的公路
  165. */
  166. this.onSpawnHighway();
  167. //初始化生成人物
  168. /**
  169. * dapp设定: 如果固定土地位置冲突,把人物生成位置放置到主路
  170. * onSpawnWorkerAIFromStoredData 此函数处理
  171. */
  172. cc.find('GameNode/ManageWorker').getComponent('ManageWorker').InitWorkerAI();
  173. let _BFirstLoadGame = this.readData ? this.readData.BFirstLoadGame : 0;
  174. //初始化新手教学
  175. GlobalD.ManageTask.InitTask(_BFirstLoadGame);
  176. this.ManageUI.getComponent('ManageUI').Init();
  177. },
  178. InitNextworkData() {
  179. // console.log("this.readData", this.readData);
  180. this.Golden = this.readData.Golden;
  181. this.Diamond = this.readData.Diamond;
  182. this.shareGive = this.readData.shareGive;
  183. //签到分享给钻石金币
  184. this.signInGive = this.readData.signInGive;
  185. //每个月 给的 低保
  186. this.EveryGive = this.readData.EveryGive;
  187. //公共分享
  188. this.publicGive = this.readData.publicGive;
  189. // cc.log('初始化网络数据');
  190. //读取日期数据
  191. this.GameYear = this.readData.GameYear;
  192. this.GameMonth = this.readData.GameMonth;
  193. this.GameDay = this.readData.GameDay;
  194. //读取金币
  195. this.Golden = this.readData.Golden;
  196. //读取钻石
  197. this.Diamond = this.readData.Diamond;
  198. // cc.log('this.readData.Diamond', this.readData.Diamond);
  199. //读取工人等级
  200. this.WorkerLV = this.readData.WorkerLV;
  201. //工人数量
  202. this.WorkerNum = this.readData.WorkerNum;
  203. //工人容量
  204. this.WorkerCapacity = this.readData.WorkerCapacity;
  205. this.GameDate = this.readData.GameDate;
  206. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  207. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  208. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  209. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  210. //面板状态
  211. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  212. //解锁设置为 不用解锁
  213. // this.BuildingLockStateArray.forEach((value, index, array) => {
  214. // array[index] = 1;
  215. // })
  216. this.BuildingLockStateArray = this.readData.BuildingLockStateArray;//.split('_');
  217. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  218. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  219. this.FoodTradeState = this.readData.FoodTradeState;
  220. this.WoodTradeState = this.readData.WoodTradeState;
  221. this.MineralTradeState = this.readData.MineralTradeState;
  222. //工人工作信息
  223. if (this.readData.characterInfoArray) {
  224. this.CharacterInfoArray = this.readData.characterInfoArray;
  225. } else {
  226. this.CharacterInfoArray = [];
  227. }
  228. //道路数据
  229. if (this.readData.highwayIndex)
  230. this.GameData_highwayIndex = this.readData.highwayIndex;
  231. //建筑物数据
  232. if (this.readData.buildingsInfo)
  233. this.GameData_buildings = this.readData.buildingsInfo;
  234. this.LotteryTimes = this.readData.LotteryTimes;
  235. //任务
  236. if (this.readData.TaskIconCountClick)
  237. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  238. },
  239. /**
  240. * 初始化
  241. */
  242. Init: function () {
  243. let _BFirstLoadGame = false;// this.readData ? this.readData.BFirstLoadGame : 0;
  244. //date
  245. this.GameYear = 0;
  246. this.GameMonth = 0;
  247. this.GameDay = 0;
  248. this.GameDate = '0000/00/01';
  249. /**
  250. * 新增 CNT 和 SNB
  251. */
  252. this.CNT = 0;
  253. this.SNB = 0;
  254. this.Golden = 2000;
  255. this.Diamond = 100;
  256. this.WorkerLV = 0;
  257. this.WorkerNum = 0;
  258. this.CharacterInfoArray = [];
  259. this.WorkerCapacity = 5;
  260. this.TerritoryStateArray = [1, 1, 1, 1, 1, 1, 1, 1];//测试,全开地图
  261. /**
  262. * 添加建筑往后添加,到时候ManageBuildings 脚本 会初始化根据顺序
  263. * BuildingStateArray---相关 GetBuildingStateArray 获取建筑状态
  264. * BuildingNumArray ---相关 GetBuildingNumArray 获取建筑数量
  265. * BuildingFrameArray ---相关 ManageUI 添加面板是否解锁之类的相关预制
  266. */
  267. this.BuildingStateArray = [
  268. 1, //公路
  269. 1, //路铲
  270. 1, //拆迁
  271. 1, //农舍
  272. 1, //单元楼
  273. 1, //别墅
  274. 1, //农田
  275. 1,//伐木场
  276. 1, //矿坑
  277. 1, //加工厂
  278. 1,//冷饮摊
  279. 1,//贩卖机
  280. 1,//面包房
  281. 1,//早餐车
  282. 1, //饮茶店
  283. 1,//点心店
  284. 1, //美食店
  285. 1,//西餐厅
  286. 1, //花店
  287. 1, //美发店
  288. 1,//洋装店
  289. 1, //珠宝店
  290. 1, //电影院
  291. 1,//路灯
  292. 1, //绿化带
  293. 1, //花坛
  294. 1,//喷泉
  295. 1, //警察局
  296. 1,//游乐场
  297. 1, //蓝色城堡
  298. 1, //粉色城堡
  299. 1, //Holy Farmland
  300. 1 //Holy Farmland seed
  301. ];
  302. this.BuildingLockStateArray = [
  303. 1, //公路
  304. 1, //路铲
  305. 1, //拆迁
  306. 1, //农舍
  307. 0, //单元楼
  308. 0, //别墅
  309. 1, //农田
  310. 0,//伐木场
  311. 0, //矿坑
  312. 1, //加工厂
  313. 0,//冷饮摊
  314. 0,//贩卖机
  315. 0,//面包房
  316. 0,//早餐车
  317. 0, //饮茶店
  318. 0,//点心店
  319. 0, //美食店
  320. 0,//西餐厅
  321. 0, //花店
  322. 0, //美发店
  323. 0,//洋装店
  324. 0, //珠宝店
  325. 0, //电影院
  326. 1,//路灯
  327. 1, //绿化带
  328. 1, //花坛
  329. 0,//喷泉
  330. 0, //警察局
  331. 0,//游乐场
  332. 0, //蓝色城堡
  333. 0, //粉色城堡
  334. 1, //Holy Farmland
  335. 1, //Holy Farmland seed
  336. ];
  337. //建筑物数量
  338. this.BuildingNumArray = [
  339. 0, //公路
  340. 0, //路铲
  341. 0, //拆迁
  342. 4, //农舍
  343. 1, //单元楼
  344. 1, //别墅
  345. 4, //农田
  346. 1,//伐木场
  347. 1, //矿坑
  348. 2, //加工厂
  349. 2,//冷饮摊
  350. 1,//贩卖机
  351. 1,//面包房
  352. 1,//早餐车
  353. 1, //饮茶店
  354. 1,//点心店
  355. 1, //美食店
  356. 1,//西餐厅
  357. 1, //花店
  358. 1, //美发店
  359. 1,//洋装店
  360. 1, //珠宝店
  361. 1, //电影院
  362. 1,//路灯
  363. 1, //绿化带
  364. 1, //花坛
  365. 1,//喷泉
  366. 1, //警察局
  367. 1,//游乐场
  368. 1, //蓝色城堡
  369. 1, //粉色城堡
  370. 1, //Holy Farmland
  371. 1, //Holy Farmland seed
  372. ];
  373. this.DiamondNumArray = AConfig.DiamondArray;
  374. //todo 钻石消耗默认值
  375. this.DiamondNumArray.forEach((value, index, array) => {
  376. array[index] = 0;
  377. })
  378. cc.log('钻石消耗默认值', this.DiamondNumArray);
  379. this.EveryDayRewardsArray = [0, 0, 0, 0, 0, 0, 0];
  380. this.LastTimeEveryDayRewardsDate = '0000/00/00';
  381. this.LastTimeLuckDate = '0000/00/00';
  382. this.FoodTradeState = 1;
  383. this.WoodTradeState = 1;
  384. this.MineralTradeState = 1;
  385. //转盘分享给钻石金币
  386. this.shareGive = [200, 5];
  387. //签到分享给钻石金币
  388. this.signInGive = [200, 5];
  389. //每个月 给的 低保
  390. this.EveryGive = [500, 10];
  391. //公共分享
  392. this.publicGive = [200, 50];
  393. //每天抽奖次数
  394. this.LotteryTimes = 10;
  395. // 1 === _BFirstLoadGame
  396. if (_BFirstLoadGame) {
  397. console.log("this.readData", this.readData);
  398. // cc.log('初始化网络数据');
  399. //读取日期数据
  400. this.GameYear = this.readData.GameYear;
  401. this.GameMonth = this.readData.GameMonth;
  402. this.GameDay = this.readData.GameDay;
  403. /**
  404. * 接口
  405. * todo cnt和snb 由钱包读取
  406. */
  407. this.CNT = 0;
  408. this.SNB = 0;
  409. //读取金币
  410. this.Golden = this.readData.Golden;
  411. //读取钻石
  412. this.Diamond = this.readData.Diamond;
  413. // cc.log('this.readData.Diamond', this.readData.Diamond);
  414. //读取工人等级
  415. this.WorkerLV = this.readData.WorkerLV;
  416. //工人数量
  417. this.WorkerNum = this.readData.WorkerNum;
  418. //工人容量
  419. this.WorkerCapacity = this.readData.WorkerCapacity;
  420. this.GameDate = this.readData.GameDate;
  421. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  422. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  423. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  424. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  425. //面板状态
  426. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  427. //解锁设置为 不用解锁
  428. this.BuildingLockStateArray.forEach((value, index, array) => {
  429. array[index] = 1;
  430. })
  431. // this.BuildingLockStateArray = this.readData.BuildingLockStateArray.split('_');
  432. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  433. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  434. this.FoodTradeState = this.readData.FoodTradeState;
  435. this.WoodTradeState = this.readData.WoodTradeState;
  436. this.MineralTradeState = this.readData.MineralTradeState;
  437. //工人工作信息
  438. if (this.readData.characterInfoArray)
  439. this.CharacterInfoArray = this.readData.characterInfoArray;
  440. //道路数据
  441. if (this.readData.highwayIndex)
  442. this.GameData_highwayIndex = this.readData.highwayIndex;
  443. //建筑物数据
  444. if (this.readData.buildingsInfo)
  445. this.GameData_buildings = this.readData.buildingsInfo;
  446. this.LotteryTimes = this.readData.LotteryTimes;
  447. //任务
  448. if (this.readData.TaskIconCountClick)
  449. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  450. //测试5W
  451. // this.PlusDiamond(50000);
  452. }
  453. },
  454. //GET / SET /Plus
  455. GetGameDate: function () {
  456. return this.GameDate;
  457. },
  458. SetGameDate: function (num) {
  459. this.GameDate = num;
  460. },
  461. PlusGameDate: function (num) {
  462. this.GameDate += num;
  463. },
  464. GetGolden: function () {
  465. return parseInt(this.Golden);
  466. },
  467. GetGoldenCallBack(_CallBack) {
  468. if (_CallBack)
  469. _CallBack({ resGolden: parseInt(this.Golden) });
  470. },
  471. SetGolden: function (Num) {
  472. let LastMoney = this.Golden;
  473. this.Golden = Num;
  474. let CurrentMoney = Num;
  475. this.ManageUI.getComponent('ManageUI').GoldenChangeCallBack(this.GetGolden(), LastMoney, CurrentMoney);
  476. },
  477. PlusGolden: function (Num) {
  478. if (this.Golden + Num < 0) {
  479. this.SetGolden(0);
  480. }
  481. else {
  482. this.SetGolden(this.Golden + Num);
  483. }
  484. // task.task50W();
  485. },
  486. GetDiamond: function () {
  487. return parseInt(this.Diamond);
  488. },
  489. SetDiamond: function (num) {
  490. let LastMoney = this.Diamond;
  491. this.Diamond = num;
  492. let CurrentMoney = num;
  493. this.ManageUI.getComponent('ManageUI').DiamondChangeCallBack(this.GetDiamond(), LastMoney, CurrentMoney);
  494. },
  495. PlusDiamond: function (num) {
  496. if (this.Diamond + num < 0) {
  497. this.SetDiamond(0);
  498. }
  499. else {
  500. this.SetDiamond(this.Diamond + num);
  501. }
  502. },
  503. /**
  504. * 游戏里面的cnt 和神农呗
  505. */
  506. GetCNT: function () {
  507. return parseFloat(this.CNT);
  508. },
  509. SetCNT: function (Num) {
  510. this.CNT = Num;
  511. this.ManageUI.getComponent('ManageUI').CNTChangeCallBack(this.CNT);
  512. },
  513. GetSNB: function () {
  514. return parseFloat(this.SNB);
  515. },
  516. SetSNB: function (Num) {
  517. this.SNB = Num;
  518. this.ManageUI.getComponent('ManageUI').SNBChangeCallBack(this.SNB);
  519. },
  520. GetWorkerLV: function () {
  521. return parseInt(this.WorkerLV);
  522. },
  523. SetWorkerLV: function (num) {
  524. this.WorkerLV = num;
  525. },
  526. PlusWorkerLV: function (num) {
  527. this.WorkerLV += num;
  528. },
  529. GetWorkerNum: function () {
  530. return parseInt(this.WorkerNum);
  531. },
  532. SetWorkerNum: function (num) {
  533. this.WorkerNum = num;
  534. },
  535. PlusWorkerNum: function (num) {
  536. this.WorkerNum += num;
  537. },
  538. GetLastTimeEveryDayRewardsDate: function () {
  539. return this.LastTimeEveryDayRewardsDate;
  540. },
  541. //获取转盘 时间
  542. GetLastTimeLuckDate: function () {
  543. return this.LastTimeLuckDate;
  544. },
  545. //设置转盘时间
  546. SetLastTimeLuckDate: function (DateString) {
  547. this.LastTimeLuckDate = DateString;
  548. },
  549. SetLastTimeEveryDayRewardsDate: function (DateString) {
  550. this.LastTimeEveryDayRewardsDate = DateString;
  551. },
  552. //工人信息数组
  553. GetWorkerCharacterInfoArray: function () {
  554. return this.CharacterInfoArray;
  555. },
  556. SetWorkerCharacterInfoArray: function (item) {
  557. this.CharacterInfoArray = item;
  558. },
  559. /*** */
  560. GetWorkerCapacity: function () {
  561. return parseInt((this.WorkerCapacity));
  562. },
  563. SetWorkerCapacity: function (num) {
  564. this.WorkerCapacity = num;
  565. },
  566. PlusWorkerCapacity: function (num) {
  567. this.WorkerCapacity += num;
  568. },
  569. GetTerritoryStateArray: function () {
  570. return this.TerritoryStateArray;
  571. },
  572. SetTerritoryStateArray: function (aTerritoryStateArray) {
  573. this.TerritoryStateArray = aTerritoryStateArray;
  574. },
  575. PlusTerritoryStateArray: function (Item) {
  576. this.TerritoryStateArray.push(Item);
  577. },
  578. GetBuildingStateArray: function () {
  579. return this.BuildingStateArray;
  580. },
  581. SetBuildingStateArray: function (aBuildingStateArray) {
  582. this.BuildingStateArray = aBuildingStateArray;
  583. },
  584. PlusBuildingStateArray: function (Item) {
  585. this.BuildingStateArray.push(Item);
  586. },
  587. GetBuildingLockStateArray: function () {
  588. return this.BuildingLockStateArray;
  589. },
  590. SetBuildingLockStateArray: function (aArray) {
  591. this.BuildingLockStateArray = aArray;
  592. },
  593. PlusBuildingLockStateArray: function (Item) {
  594. this.BuildingLockStateArray.push(Item);
  595. },
  596. GetBuildingNumArray: function () {
  597. return this.BuildingNumArray;
  598. },
  599. SetBuildingNumArray: function (aBuildingNumArray) {
  600. this.BuildingNumArray = aBuildingNumArray;
  601. },
  602. PlusBuildingNumArray: function (Item) {
  603. this.BuildingNumArray.push(Item);
  604. },
  605. //操作钻石数据
  606. GetDiamondNumArray: function () {
  607. return this.DiamondNumArray;
  608. },
  609. SetDiamondNumArray: function (aDiamondNumArray) {
  610. this.DiamondNumArray = aDiamondNumArray;
  611. },
  612. GetEveryDayRewardsArray: function () {
  613. return this.EveryDayRewardsArray;
  614. },
  615. SetEveryDayRewardsArray: function (aArray) {
  616. this.EveryDayRewardsArray = aArray;
  617. },
  618. PlusEveryDayRewardsArray: function (Item) {
  619. this.EveryDayRewardsArray.push(Item);
  620. },
  621. GetFoodTradeState: function () {
  622. return parseInt((this.FoodTradeState));
  623. },
  624. SetFoodTradeState: function (num) {
  625. this.FoodTradeState = num;
  626. },
  627. PlusFoodTradeState: function (num) {
  628. this.FoodTradeState += num;
  629. },
  630. GetWoodTradeState: function () {
  631. return parseInt((this.WoodTradeState));
  632. },
  633. SetWoodTradeState: function (num) {
  634. this.WoodTradeState = num;
  635. },
  636. PlusWoodTradeState: function (num) {
  637. this.WoodTradeState += num;
  638. },
  639. GetMineralTradeState: function () {
  640. return parseInt((this.MineralTradeState));
  641. },
  642. SetMineralTradeState: function (num) {
  643. this.MineralTradeState = num;
  644. },
  645. PlusMineralTradeState: function (num) {
  646. this.MineralTradeState += num;
  647. },
  648. GetLotteryTimes: function () {
  649. return parseInt((this.LotteryTimes));
  650. },
  651. SetLotteryTimes: function (num) {
  652. this.LotteryTimes = num;
  653. },
  654. PlusLotteryTimes: function (num) {
  655. this.LotteryTimes += num;
  656. },
  657. //初始化道路
  658. onSpawnHighway() {
  659. // 获取InitPoint层
  660. let InitPos = this._tiledMap.getLayer('InitPoint');
  661. InitPos.enabled = false;
  662. //创建公路数据
  663. if (this.readData && this.readData.allHighwayStylesAndIndex) {
  664. //保存到内存中
  665. this.GameData_highwayIndex = this.readData.highwayIndex;
  666. //先赋值道路的对象数组
  667. let _AllHighwayAIndex = Object.assign([], this.readData.allHighwayStylesAndIndex);
  668. //获取最后建造公路的层级
  669. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  670. for (let i = 0; i < _AllHighwayAIndex.length; i++) {
  671. let _tiledPos = GlobalD.TiledMap.analyticalIndexData(_AllHighwayAIndex[i].highwayInfoIndex);
  672. /**
  673. * 解决固定土地时候,道路 和房子 冲突
  674. *
  675. */
  676. if (GlobalD.game.getManageGameIndexArrayAt(cc.v2(_tiledPos.x, _tiledPos.y))) {
  677. continue;
  678. }
  679. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  680. highwayTemp.parent = this.HighwayLayer.node;
  681. let tiledTile = highwayTemp.addComponent('TiledTile');
  682. tiledTile.x = _tiledPos.x;
  683. tiledTile.y = _tiledPos.y;
  684. //地图设置可行走区域公路设置
  685. AStar.setMapSolid(_tiledPos.x, _tiledPos.y, 0);
  686. // _buildId ==0 是公路
  687. let _buildId = 0;
  688. let occupyTemp = cc.v2(_buildId, _AllHighwayAIndex[i].highwayInfoIndex);
  689. GlobalD.game.OccupyArray.push(occupyTemp);
  690. highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(_AllHighwayAIndex[i].highwayInfoType);
  691. GlobalD.game.AllHighwayStylesAndIndex.push(_AllHighwayAIndex[i]);
  692. }
  693. // console.log("end");
  694. } else {
  695. //如果用户没有存储数据
  696. //根据InitPoint 的数据,创建初始化的地图数据 的初始化数据
  697. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  698. for (var i = 0; i < 32; i++) {
  699. //去除中间两条路
  700. if (i == 16 || i == 17) continue;
  701. for (var j = 0; j < 32; j++) {
  702. let tilesPos = cc.v2(i, j);
  703. if (InitPos.getTileGIDAt(tilesPos)) {
  704. let index = GlobalD.TiledMap.getIndex(tilesPos);
  705. this.GameData_highwayIndex.push(index);
  706. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  707. highwayTemp.parent = this.HighwayLayer.node;
  708. let tiledTile = highwayTemp.addComponent('TiledTile');
  709. tiledTile.x = tilesPos.x;
  710. tiledTile.y = tilesPos.y;
  711. // cc.log('onSpawnHighway2');
  712. //地图设置可行走区域公路设置
  713. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  714. // _buildId ==0 是公路
  715. let _buildId = 0;
  716. let occupyTemp = cc.v2(_buildId, index);
  717. GlobalD.game.OccupyArray.push(occupyTemp);
  718. let tile = InitPos.getTiledTileAt(tilesPos.x, tilesPos.y, true);
  719. let _MoveType = reGameStates.HighwayType.moveX;
  720. // if (tile.gid == 15) {
  721. // _MoveType = reGameStates.HighwayType.moveX;
  722. // } else
  723. if (tile.gid == 16) {
  724. _MoveType = reGameStates.HighwayType.moveY;
  725. } else if (tile.gid == 11) {
  726. _MoveType = reGameStates.HighwayType.ZebraCrossingX;
  727. } else if (tile.gid == 12) {
  728. _MoveType = reGameStates.HighwayType.ZebraCrossingY;
  729. }
  730. // return;
  731. //更换公路样式
  732. GlobalD.game.onCreateDifferentRoadStyles({
  733. _buildId: _buildId,
  734. _highwayType: _MoveType,
  735. _roadIndex: index,
  736. _hightwayNode: highwayTemp
  737. });
  738. }
  739. }
  740. }
  741. }
  742. //外面的两条路
  743. let initRoad = this._tiledMap.getLayer('Road');
  744. initRoad.enabled = false;
  745. for (var i = 16; i < 18; i++) {
  746. for (var j = 0; j < 32; j++) {
  747. let tilesPos = cc.v2(i, j);
  748. // cc.log('road tilesPos',tilesPos)
  749. if (initRoad.getTileGIDAt(tilesPos)) {
  750. let index = GlobalD.TiledMap.getIndex(tilesPos);
  751. this.GameData_highwayIndex.push(index);
  752. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  753. highwayTemp.parent = this.HighwayLayer.node;
  754. let tiledTile = highwayTemp.addComponent('TiledTile');
  755. tiledTile.x = tilesPos.x;
  756. tiledTile.y = tilesPos.y;
  757. // cc.log('initRoad');
  758. //地图设置可行走区域公路设置
  759. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  760. let _buildId = 0;
  761. let occupyTemp = cc.v2(_buildId, index);
  762. GlobalD.game.OccupyArray.push(occupyTemp);
  763. //更换公路样式
  764. // if (j >= 25 && j <= 27 || j >= 19 && j <= 21 || j >= 9 && j <= 11)
  765. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
  766. // else
  767. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.moveY);
  768. }
  769. }
  770. }
  771. },
  772. //清除所有的数据
  773. onClearAllData() {
  774. this.unschedule(this.AutoSaveData);
  775. //任务索引
  776. cc.sys.localStorage.removeItem('userdata');
  777. //跳回登录场景
  778. // cc.director.loadScene('Login');
  779. },
  780. //保存数据请求
  781. /**
  782. *
  783. * 现在转服务器存储,原本所有相关操作通过服务器计算。
  784. * 一、目前涉及到的是 SNT 和 SNB 两个货币相关,和买田地操作 (可以理解为租聘,三种类型)
  785. *
  786. * isNetwork:true,上传到网络
  787. *
  788. */
  789. pushData: function (isNetwork) {
  790. // cc.log(this.EveryDayRewardsArray);
  791. // console.log(this.readData.BFirstLoadGame);
  792. var datas = {
  793. version: this.GameVersion,
  794. // 转后台处理
  795. BFirstLoadGame: 1,//只要push数据,就证明已经开始第一次游戏了
  796. /**游戏不修改,默认值 start */
  797. shareGive: this.shareGive,
  798. //签到分享给钻石金币
  799. signInGive: this.signInGive,
  800. //每个月 给的 低保
  801. EveryGive: this.EveryGive,
  802. //公共分享
  803. publicGive: this.publicGive,
  804. /**游戏不修改,默认值 end */
  805. //时间
  806. GameYear: this.GameYear,
  807. GameMonth: this.GameMonth,
  808. GameDay: this.GameDay,
  809. //这里只是单纯的存储起来
  810. CNT: this.CNT,
  811. SNB: this.SNB,
  812. Golden: this.Golden,
  813. Diamond: this.Diamond,
  814. WorkerLV: this.WorkerLV,
  815. WorkerNum: this.WorkerNum,
  816. WorkerCapacity: this.WorkerCapacity,
  817. GameDate: this.GameDate,
  818. LastTimeEveryDayRewardsDate: this.LastTimeEveryDayRewardsDate,
  819. LastTimeLuckDate: this.LastTimeLuckDate,
  820. EveryDayRewardsArray: this.EveryDayRewardsArray,//.join('_'),
  821. TerritoryStateArray: this.TerritoryStateArray,//.join('_'),
  822. //面板状态
  823. BuildingStateArray: this.BuildingStateArray,//.join('_'),
  824. BuildingLockStateArray: this.BuildingLockStateArray,//.join('_'),
  825. /**
  826. * SNB和CNT相关,需要后台筛选 田地 和 种子。单独处理
  827. */
  828. BuildingNumArray: this.BuildingNumArray,//.join('_'),
  829. DiamondNumArray: this.DiamondNumArray,//.join('_'),
  830. FoodTradeState: this.FoodTradeState,
  831. WoodTradeState: this.WoodTradeState,
  832. MineralTradeState: this.MineralTradeState,
  833. LotteryTimes: this.LotteryTimes,
  834. /**
  835. * 这些数据不用服务器初始化
  836. */
  837. //工人工作信息
  838. characterInfoArray: this.CharacterInfoArray,
  839. //道路数据
  840. highwayIndex: this.GameData_highwayIndex,
  841. allHighwayStylesAndIndex: GlobalD.game.AllHighwayStylesAndIndex,
  842. //建筑物数据
  843. buildingsInfo: this.GameData_buildings,
  844. //任务
  845. TaskIconCountClick: task.TaskIconCountClick
  846. }
  847. if (isNetwork) {
  848. var data = [];
  849. // data["openid"] = userData.openId;
  850. // data["userdata"] = JSON.stringify(datas);
  851. data["playerData"] = JSON.stringify(datas);
  852. /** 推送信息 */
  853. utils.post(utils.api.playerPushInfo, data, (res, playerPushInfoTemp) => {
  854. // console.log('playerPushInfoTemp', playerPushInfoTemp);
  855. })
  856. } else {
  857. console.log("存储本地:" + isNetwork);
  858. //保存用户数据到本地
  859. cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
  860. }
  861. },
  862. /**
  863. * 暂时不需要从这里读取
  864. */
  865. getLandConfig() {
  866. // "data": [
  867. // {
  868. // "id": 1,
  869. // "configLandId": 1,
  870. // "userId": "4",
  871. // "name": "1",
  872. // "isLease": 1,
  873. // "leaseTime": "2022-01-10 21:25:17",
  874. // "leaseMultiple": null,
  875. // "isPlant": 1,
  876. // "plantStart": "2022-01-10 21:25:22",
  877. // "plantMature": 1,
  878. // "landDescribe": "1",
  879. // "createTime": "2022-01-10 21:25:30",
  880. // "updateTime": "2022-01-10 21:25:33"
  881. // }
  882. // ],
  883. utils.get(utils.api.landConfig, {}, (res, value) => {
  884. if (0 === value.code) {
  885. this.ConfigLand = value.data;
  886. } else {
  887. console.warn("未能读取到config土地?");
  888. }
  889. })
  890. },
  891. //获取用户全部租赁土地
  892. getUserLandList() {
  893. // "data": [
  894. // {
  895. // "id": 1,
  896. // "configLandId": 1,
  897. // "userId": "4",
  898. // "name": "1",
  899. // "isLease": 1,
  900. // "leaseTime": "2022-01-10 21:25:17",
  901. // "leaseMultiple": null,
  902. // "isPlant": 1,
  903. // "plantStart": "2022-01-10 21:25:22",
  904. // "plantMature": 1,
  905. // "landDescribe": "1",
  906. // "createTime": "2022-01-10 21:25:30",
  907. // "updateTime": "2022-01-10 21:25:33"
  908. // }
  909. // ],
  910. utils.get(utils.api.userLandList, {}, (res, vaule) => {
  911. })
  912. },
  913. //获取用户已租赁土地的状态
  914. getLandState(context) {
  915. let { landId, callback } = context;
  916. // {
  917. // "id": 1,
  918. // "configLandId": 1,
  919. // "userId": "4",
  920. // "name": "1",
  921. // "isLease": 1,
  922. // "leaseTime": "2022-01-10 21:25:17",
  923. // "leaseMultiple": null,
  924. // "isPlant": 1,
  925. // "plantStart": "2022-01-10 21:25:22",
  926. // "plantMature": 1,
  927. // "landDescribe": "1",
  928. // "createTime": "2022-01-10 21:25:30",
  929. // "updateTime": "2022-01-10 21:25:33"
  930. // }
  931. utils.get(utils.api.landState, { landId: landId }, (res, vaule) => {
  932. callback(res, vaule);
  933. })
  934. },
  935. //获取商城种子
  936. getMallSeed(callback) {
  937. // "data": [
  938. // {
  939. // "id": 1,
  940. // "mallType": "0",
  941. // "picture": null,
  942. // "name": "白菜种子",
  943. // "maturity": 100,
  944. // "planting": 100,
  945. // "harvestQuantity": 2500,
  946. // "harvestCount": 2500,
  947. // "harvestName": "大白菜",
  948. // "price": 99,
  949. // "amount": 0,
  950. // "withered": 200,
  951. // "createTime": "2022-01-10 16:45:18",
  952. // "updateTime": "2022-01-10 16:45:26"
  953. // },
  954. // {
  955. // "id": 2,
  956. // "mallType": "0",
  957. // "picture": null,
  958. // "name": "辣椒种子",
  959. // "maturity": 100,
  960. // "planting": 100,
  961. // "harvestQuantity": 2500,
  962. // "harvestCount": 2500,
  963. // "harvestName": "辣椒",
  964. // "price": 99,
  965. // "amount": 0,
  966. // "withered": 200,
  967. // "createTime": "2022-01-10 16:45:23",
  968. // "updateTime": "2022-01-10 16:45:28"
  969. // }
  970. // ],
  971. utils.get(utils.api.mallSeed, {}, (res, vaule) => {
  972. callback(res, vaule);
  973. })
  974. },
  975. getWarehouseSeedAndFruit(callback) {
  976. // "data": {
  977. // "seed": [
  978. // {
  979. // amount: 0
  980. // createTime: "2022-01-10 16:45:18"
  981. // harvestCount: 2500
  982. // harvestName: "大白菜"
  983. // harvestQuantity: 2500
  984. // id: 1
  985. // mallType: "0"
  986. // maturity: 100
  987. // name: "白菜种子"
  988. // picture: null
  989. // planting: 100
  990. // priceCnt: 99.99
  991. // priceSnb: 495.99
  992. // updateTime: "2022-01-10 16:45:26"
  993. // withered: 200
  994. // }
  995. // ],
  996. // "fruit": [
  997. // {
  998. // createTime: "2022-01-10 16:45:18"
  999. // id: 1
  1000. // name: "白菜"
  1001. // picture: null
  1002. // priceCnt: 9.99
  1003. // priceSnb: 99
  1004. // updateTime: "2022-01-10 16:45:26"
  1005. // }
  1006. // ]
  1007. // },
  1008. utils.get(utils.api.getSeedAndFruit, {}, (res, vaule) => {
  1009. callback(res, vaule);
  1010. })
  1011. },
  1012. //获取种子状态
  1013. onGetSeedState(data, callback) {
  1014. utils.get(utils.api.getSeedState, data, (res, vaule) => {
  1015. callback(res, vaule);
  1016. })
  1017. },
  1018. //种植种子
  1019. // data:{landId:1,seedId:1}
  1020. onPlant(data, callback) {
  1021. utils.get(utils.api.plant, data, (res, vaule) => {
  1022. callback(res, vaule);
  1023. })
  1024. },
  1025. //收取果实
  1026. onAddFruit(data, callback) {
  1027. utils.post(utils.api.addFruit, data, (res, vaule) => {
  1028. callback(res, vaule);
  1029. })
  1030. },
  1031. //出售果实
  1032. onSaleFruit(data, callback) {
  1033. utils.post(utils.api.saleFruit, data, (res, vaule) => {
  1034. callback(res, vaule);
  1035. })
  1036. },
  1037. //赠送果实
  1038. onGrantFruit(data, callback) {
  1039. utils.post(utils.api.grantFruit, data, (res, vaule) => {
  1040. callback(res, vaule);
  1041. })
  1042. },
  1043. //神农呗购买种子
  1044. onBuySeedsWithSNB(snbAmount, seedId, callback) {
  1045. utils.post(utils.api.snbBuySeeds, { payAmount: snbAmount, seedId: seedId }, (res, vaule) => {
  1046. callback(res, vaule);
  1047. })
  1048. },
  1049. /**
  1050. * 支付cnt操作
  1051. * @param {*} amount // 需支付金额
  1052. * @param {*} payType 支付类型,1购买土地租凭, 2自然灾害防护,3野兽防护
  1053. * @param {*} itemType 操作物品的id 字符串
  1054. */
  1055. payCNT(cntAmount, payType, itemType, callback) {
  1056. console.log("购买金额:" + cntAmount + "购买类型PlayType:" + payType + "购买物品的id:" + itemType);
  1057. if (!GlobalD.dapp) {
  1058. console.warn("GlobalD.dapp未初始化,不能payCNT!");
  1059. return;
  1060. }
  1061. //
  1062. if(this.isPlayingCnt){
  1063. console.warn("同时触发支付CNT过快!");
  1064. return;
  1065. }
  1066. this.isPlayingCnt = true;
  1067. if (GlobalD.GameData.GetCNT() < cntAmount) {
  1068. console.log("cnt 不足,GetCNT:" + GlobalD.GameData.GetCNT() + "消费的cntAmount" + cntAmount);
  1069. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "CNT不足!", 2, () => {
  1070. console.log("finish toast! CNT不足!");
  1071. });
  1072. return;
  1073. }
  1074. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付处理中...", 20, () => {
  1075. console.log("finish toast!支付处理中...");
  1076. });
  1077. GlobalD.dapp.payCnt(cntAmount, payType, itemType).then((data) => {
  1078. const [err, tx] = data;
  1079. this.isPlayingCnt = false;
  1080. if (err === null) {
  1081. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  1082. console.log(tx) // 交易hash,唯一标识符
  1083. GlobalD.dapp.cntBalance().then((cntBalance) => {
  1084. //会延迟返回
  1085. // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  1086. GlobalD.GameData.SetCNT(cntBalance);
  1087. });
  1088. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付成功!", 5, () => {
  1089. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  1090. });
  1091. } else {
  1092. console.log(err)
  1093. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2, () => {
  1094. console.log("finish toast!");
  1095. });
  1096. }
  1097. if (callback) {
  1098. callback(data);
  1099. }
  1100. });
  1101. },
  1102. /**
  1103. * 神农呗转换CNT ,比例是5:1
  1104. * @param {*} snbAmount
  1105. * @param {*} callback
  1106. * @returns
  1107. */
  1108. onSnbToCnt(snbAmount, callback) {
  1109. if (!GlobalD.dapp) {
  1110. console.warn("GlobalD.dapp未初始化,不能onSnbToCnt!");
  1111. return;
  1112. }
  1113. if(this.isPlayingSnb){
  1114. console.warn("同时触发支付SNB过快!");
  1115. return;
  1116. }
  1117. this.isPlayingSnb = true;
  1118. if (GlobalD.GameData.GetSNB() <= 0) {
  1119. console.log("神农呗数量GetSNB:", GlobalD.GameData.GetSNB(), "替换snbAmount:", snbAmount);
  1120. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "神农呗数量不足!", 2, () => {
  1121. console.log("finish toast!");
  1122. });
  1123. return;
  1124. }
  1125. GlobalD.dapp.snbToCnt(snbAmount).then((data) => {
  1126. const [err, tx] = data;
  1127. this.isPlayingSnb = false;
  1128. if (err === null) {
  1129. // TODO 成功, 兑换为链上操作,需要提供回调接口给这边服务端确认交易成功后修改扣除SNB数量
  1130. console.log(tx) // 交易hash,唯一标识符
  1131. //扣除对应的神农呗,本地修改显示
  1132. GlobalD.GameData.SetSNB(GlobalD.GameData.GetSNB() - snbAmount);
  1133. GlobalD.dapp.cntBalance().then((cntBalance) => {
  1134. //会延迟返回
  1135. // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  1136. GlobalD.GameData.SetCNT(cntBalance);
  1137. });
  1138. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 2, () => {
  1139. console.log("finish toast!");
  1140. });
  1141. } else {
  1142. console.log(err)
  1143. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2, () => {
  1144. console.log("finish toast!");
  1145. });
  1146. }
  1147. if (callback) {
  1148. callback(data);
  1149. }
  1150. });
  1151. },
  1152. //获取 神农呗转 CNT 日志
  1153. onGetSnbToCntInfo(page, limit, callback) {
  1154. if (!GlobalD.dapp) {
  1155. console.warn("GlobalD.dapp未初始化,不能onGetSnbToCntInfo!");
  1156. return;
  1157. }
  1158. GlobalD.dapp.swapLog(page, limit).then((data) => {
  1159. const { err, res } = data;
  1160. if (err === null) {
  1161. console.log(res) //
  1162. }
  1163. if (callback) {
  1164. callback(data);
  1165. }
  1166. });
  1167. },
  1168. updateToast() {
  1169. if (this.toastEndTime <= 0 && this.isStart) {
  1170. this.toast.active = false
  1171. if (this.toastCallback != null && this.toastCallback != undefined) {
  1172. this.toastCallback();
  1173. // console.log(this.toastCallback);
  1174. }
  1175. this.isStart = false;
  1176. } else {
  1177. this.toastEndTime--;
  1178. }
  1179. },
  1180. showToast(parent, content, time, callback) {
  1181. this.toastCallback = callback;
  1182. this.isStart = true;
  1183. if (this.toast) {
  1184. this.toast.active = true;
  1185. this.toast.parent = parent;
  1186. this.toast.zIndex = 999;
  1187. let DetailLabel = this.toast.getChildByName('DetailLabel');
  1188. DetailLabel.getComponent(cc.Label).string = content;
  1189. this.toastEndTime = time;
  1190. } else {
  1191. cc.loader.loadRes("prefab/gameToast", function (err, texture) {
  1192. this.toast = cc.instantiate(texture);
  1193. this.toast.parent = parent;
  1194. this.toast.zIndex = 999;
  1195. let DetailLabel = this.toast.getChildByName('DetailLabel');
  1196. DetailLabel.getComponent(cc.Label).string = content;
  1197. this.toastEndTime = time;
  1198. this.scheduleObj = this.schedule(() => {
  1199. this.updateToast();
  1200. }, 1);
  1201. }.bind(this));
  1202. }
  1203. }
  1204. });