GameData.js 42 KB

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