GameData.js 36 KB

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