GameData.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. onLoad() {
  44. //初始化GameData全局变量
  45. GlobalD.GameData = this;
  46. this.GameConfig();
  47. },
  48. GameConfig() {
  49. this.AddBuildingCost = 5;
  50. this.RemoveBuildingCost = 5;
  51. //
  52. this.Dapp = {
  53. UserInfo: null
  54. }
  55. },
  56. start() {
  57. this._tiledMap = GlobalD.TiledMap._tiledMap;
  58. //调用初始化dapp
  59. this.isDebugMode(GlobalD.dapp);
  60. },
  61. /**
  62. * 根据 不存在dapp 的话,使用本地测试
  63. * @param {*} bInit
  64. */
  65. isDebugMode: function (bInit) {
  66. if (bInit) {
  67. //读取网络数据
  68. try {
  69. GlobalD.dapp.cntBalance().then((cntBalance) => {
  70. console.log(cntBalance) // string, 精度18,需要自行处理省略几位小数
  71. GlobalD.GameData.SetCNT(cntBalance);
  72. });
  73. } catch (err) {
  74. console.error(err) // 初始化失败,运行环境不是钱包环境
  75. }
  76. this.readData = userData.readData;
  77. cc.log('playerPullInfo 读取到数据?:', this.readData)
  78. this.InitNextworkData();
  79. this.InitSceneInfo();
  80. // 自动存储数据
  81. /**
  82. * todo 如果退出游戏,触发一次存储。
  83. */
  84. this.AutoSaveData = function () {
  85. this.pushData();
  86. };
  87. this.schedule(this.AutoSaveData, 20);
  88. } else {
  89. // 清除
  90. cc.log("本地数据重新开始,并且不初始化");
  91. // this.onClearAllData();
  92. this.Init();
  93. this.InitSceneInfo();
  94. }
  95. },
  96. getData: function () {
  97. //如果开局没有读取到网络数据,就这里初始化
  98. if (userData.readData == null) {
  99. cc.log("本地数据:", cc.sys.localStorage.getItem('userdata'));
  100. let userdata = cc.sys.localStorage.getItem('userdata');
  101. if (userdata) {
  102. this.readData = JSON.parse(userdata);
  103. }
  104. this.Init();
  105. this.InitSceneInfo();
  106. } else {
  107. this.readData = userData.readData;
  108. cc.log('读取到数据?:', this.readData)
  109. this.Init();
  110. this.InitSceneInfo();
  111. }
  112. },
  113. InitSceneInfo() {
  114. //根据顺序生成
  115. //初始化道路
  116. this.onSpawnHighway();
  117. //初始化时间ui
  118. cc.find("GameNode/ManageTimer").getComponent('ManageTimer').Init();
  119. //初始化金钱ui
  120. cc.find("GameNode/ManageGolden").getComponent('ManageGolden').InitManageGlodenUI();
  121. // //初始化地图物件
  122. // cc.find("GameNode/ManageMap").getComponent('ManageMap').InitManageMap();
  123. GlobalD.TiledMap.onInitSolid();
  124. //初始化生成房屋
  125. this.ManageUI.getComponent('ManageBuildings').InitBuildings();
  126. //初始化生成人物
  127. cc.find('GameNode/ManageWorker').getComponent('ManageWorker').InitWorkerAI();
  128. let _BFirstLoadGame = this.readData ? this.readData.BFirstLoadGame : 0;
  129. //初始化新手教学
  130. GlobalD.ManageTask.InitTask(_BFirstLoadGame);
  131. this.ManageUI.getComponent('ManageUI').Init();
  132. },
  133. InitNextworkData() {
  134. console.log("this.readData", this.readData);
  135. this.Golden = this.readData.Golden;
  136. this.Diamond = this.readData.Diamond;
  137. this.shareGive = this.readData.shareGive;
  138. //签到分享给钻石金币
  139. this.signInGive = this.readData.signInGive;
  140. //每个月 给的 低保
  141. this.EveryGive = this.readData.EveryGive;
  142. //公共分享
  143. this.publicGive = this.readData.publicGive;
  144. // cc.log('初始化网络数据');
  145. //读取日期数据
  146. this.GameYear = this.readData.GameYear;
  147. this.GameMonth = this.readData.GameMonth;
  148. this.GameDay = this.readData.GameDay;
  149. /**
  150. * 接口
  151. * todo cnt和snb 由钱包读取
  152. */
  153. this.CNT = 0;
  154. this.SNB = 0;
  155. //读取金币
  156. this.Golden = this.readData.Golden;
  157. //读取钻石
  158. this.Diamond = this.readData.Diamond;
  159. // cc.log('this.readData.Diamond', this.readData.Diamond);
  160. //读取工人等级
  161. this.WorkerLV = this.readData.WorkerLV;
  162. //工人数量
  163. this.WorkerNum = this.readData.WorkerNum;
  164. //工人容量
  165. this.WorkerCapacity = this.readData.WorkerCapacity;
  166. this.GameDate = this.readData.GameDate;
  167. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  168. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  169. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  170. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  171. //面板状态
  172. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  173. //解锁设置为 不用解锁
  174. // this.BuildingLockStateArray.forEach((value, index, array) => {
  175. // array[index] = 1;
  176. // })
  177. this.BuildingLockStateArray = this.readData.BuildingLockStateArray;//.split('_');
  178. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  179. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  180. this.FoodTradeState = this.readData.FoodTradeState;
  181. this.WoodTradeState = this.readData.WoodTradeState;
  182. this.MineralTradeState = this.readData.MineralTradeState;
  183. //工人工作信息
  184. if (this.readData.characterInfoArray) {
  185. this.CharacterInfoArray = this.readData.characterInfoArray;
  186. } else {
  187. this.CharacterInfoArray = [];
  188. }
  189. //道路数据
  190. if (this.readData.highwayIndex)
  191. this.GameData_highwayIndex = this.readData.highwayIndex;
  192. //建筑物数据
  193. if (this.readData.buildingsInfo)
  194. this.GameData_buildings = this.readData.buildingsInfo;
  195. this.LotteryTimes = this.readData.LotteryTimes;
  196. //任务
  197. if (this.readData.TaskIconCountClick)
  198. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  199. },
  200. /**
  201. * 初始化
  202. */
  203. Init: function () {
  204. let _BFirstLoadGame = false;// this.readData ? this.readData.BFirstLoadGame : 0;
  205. //date
  206. this.GameYear = 0;
  207. this.GameMonth = 0;
  208. this.GameDay = 0;
  209. this.GameDate = '0000/00/01';
  210. /**
  211. * 新增 CNT 和 SNB
  212. */
  213. this.CNT = 0;
  214. this.SNB = 0;
  215. this.Golden = 2000;
  216. this.Diamond = 100;
  217. this.WorkerLV = 0;
  218. this.WorkerNum = 0;
  219. this.CharacterInfoArray = [];
  220. this.WorkerCapacity = 5;
  221. this.TerritoryStateArray = [1, 1, 1, 1, 1, 1, 1, 1];//测试,全开地图
  222. /**
  223. * 添加建筑往后添加,到时候ManageBuildings 脚本 会初始化根据顺序
  224. * BuildingStateArray---相关 GetBuildingStateArray 获取建筑状态
  225. * BuildingNumArray ---相关 GetBuildingNumArray 获取建筑数量
  226. * BuildingFrameArray ---相关 ManageUI 添加面板是否解锁之类的相关预制
  227. */
  228. this.BuildingStateArray = [
  229. 1, //公路
  230. 1, //路铲
  231. 1, //拆迁
  232. 1, //农舍
  233. 1, //单元楼
  234. 1, //别墅
  235. 1, //农田
  236. 1,//伐木场
  237. 1, //矿坑
  238. 1, //加工厂
  239. 1,//冷饮摊
  240. 1,//贩卖机
  241. 1,//面包房
  242. 1,//早餐车
  243. 1, //饮茶店
  244. 1,//点心店
  245. 1, //美食店
  246. 1,//西餐厅
  247. 1, //花店
  248. 1, //美发店
  249. 1,//洋装店
  250. 1, //珠宝店
  251. 1, //电影院
  252. 1,//路灯
  253. 1, //绿化带
  254. 1, //花坛
  255. 1,//喷泉
  256. 1, //警察局
  257. 1,//游乐场
  258. 1, //蓝色城堡
  259. 1, //粉色城堡
  260. 1, //Holy Farmland
  261. 1 //Holy Farmland seed
  262. ];
  263. this.BuildingLockStateArray = [
  264. 1, //公路
  265. 1, //路铲
  266. 1, //拆迁
  267. 1, //农舍
  268. 0, //单元楼
  269. 0, //别墅
  270. 1, //农田
  271. 0,//伐木场
  272. 0, //矿坑
  273. 1, //加工厂
  274. 0,//冷饮摊
  275. 0,//贩卖机
  276. 0,//面包房
  277. 0,//早餐车
  278. 0, //饮茶店
  279. 0,//点心店
  280. 0, //美食店
  281. 0,//西餐厅
  282. 0, //花店
  283. 0, //美发店
  284. 0,//洋装店
  285. 0, //珠宝店
  286. 0, //电影院
  287. 1,//路灯
  288. 1, //绿化带
  289. 1, //花坛
  290. 0,//喷泉
  291. 0, //警察局
  292. 0,//游乐场
  293. 0, //蓝色城堡
  294. 0, //粉色城堡
  295. 1, //Holy Farmland
  296. 1, //Holy Farmland seed
  297. ];
  298. //建筑物数量
  299. this.BuildingNumArray = [
  300. 0, //公路
  301. 0, //路铲
  302. 0, //拆迁
  303. 4, //农舍
  304. 1, //单元楼
  305. 1, //别墅
  306. 4, //农田
  307. 1,//伐木场
  308. 1, //矿坑
  309. 2, //加工厂
  310. 2,//冷饮摊
  311. 1,//贩卖机
  312. 1,//面包房
  313. 1,//早餐车
  314. 1, //饮茶店
  315. 1,//点心店
  316. 1, //美食店
  317. 1,//西餐厅
  318. 1, //花店
  319. 1, //美发店
  320. 1,//洋装店
  321. 1, //珠宝店
  322. 1, //电影院
  323. 1,//路灯
  324. 1, //绿化带
  325. 1, //花坛
  326. 1,//喷泉
  327. 1, //警察局
  328. 1,//游乐场
  329. 1, //蓝色城堡
  330. 1, //粉色城堡
  331. 1, //Holy Farmland
  332. 1, //Holy Farmland seed
  333. ];
  334. this.DiamondNumArray = AConfig.DiamondArray;
  335. //todo 钻石消耗默认值
  336. this.DiamondNumArray.forEach((value, index, array) => {
  337. array[index] = 0;
  338. })
  339. cc.log('钻石消耗默认值', this.DiamondNumArray);
  340. this.EveryDayRewardsArray = [0, 0, 0, 0, 0, 0, 0];
  341. this.LastTimeEveryDayRewardsDate = '0000/00/00';
  342. this.LastTimeLuckDate = '0000/00/00';
  343. this.FoodTradeState = 1;
  344. this.WoodTradeState = 1;
  345. this.MineralTradeState = 1;
  346. //转盘分享给钻石金币
  347. this.shareGive = [200, 5];
  348. //签到分享给钻石金币
  349. this.signInGive = [200, 5];
  350. //每个月 给的 低保
  351. this.EveryGive = [500, 10];
  352. //公共分享
  353. this.publicGive = [200, 50];
  354. //每天抽奖次数
  355. this.LotteryTimes = 10;
  356. // 1 === _BFirstLoadGame
  357. if (_BFirstLoadGame) {
  358. console.log("this.readData", this.readData);
  359. // cc.log('初始化网络数据');
  360. //读取日期数据
  361. this.GameYear = this.readData.GameYear;
  362. this.GameMonth = this.readData.GameMonth;
  363. this.GameDay = this.readData.GameDay;
  364. /**
  365. * 接口
  366. * todo cnt和snb 由钱包读取
  367. */
  368. this.CNT = 0;
  369. this.SNB = 0;
  370. //读取金币
  371. this.Golden = this.readData.Golden;
  372. //读取钻石
  373. this.Diamond = this.readData.Diamond;
  374. // cc.log('this.readData.Diamond', this.readData.Diamond);
  375. //读取工人等级
  376. this.WorkerLV = this.readData.WorkerLV;
  377. //工人数量
  378. this.WorkerNum = this.readData.WorkerNum;
  379. //工人容量
  380. this.WorkerCapacity = this.readData.WorkerCapacity;
  381. this.GameDate = this.readData.GameDate;
  382. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  383. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  384. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  385. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  386. //面板状态
  387. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  388. //解锁设置为 不用解锁
  389. this.BuildingLockStateArray.forEach((value, index, array) => {
  390. array[index] = 1;
  391. })
  392. // this.BuildingLockStateArray = this.readData.BuildingLockStateArray.split('_');
  393. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  394. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  395. this.FoodTradeState = this.readData.FoodTradeState;
  396. this.WoodTradeState = this.readData.WoodTradeState;
  397. this.MineralTradeState = this.readData.MineralTradeState;
  398. //工人工作信息
  399. if (this.readData.characterInfoArray)
  400. this.CharacterInfoArray = this.readData.characterInfoArray;
  401. //道路数据
  402. if (this.readData.highwayIndex)
  403. this.GameData_highwayIndex = this.readData.highwayIndex;
  404. //建筑物数据
  405. if (this.readData.buildingsInfo)
  406. this.GameData_buildings = this.readData.buildingsInfo;
  407. this.LotteryTimes = this.readData.LotteryTimes;
  408. //任务
  409. if (this.readData.TaskIconCountClick)
  410. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  411. //测试5W
  412. // this.PlusDiamond(50000);
  413. }
  414. },
  415. //GET / SET /Plus
  416. GetGameDate: function () {
  417. return this.GameDate;
  418. },
  419. SetGameDate: function (num) {
  420. this.GameDate = num;
  421. },
  422. PlusGameDate: function (num) {
  423. this.GameDate += num;
  424. },
  425. GetGolden: function () {
  426. return parseInt(this.Golden);
  427. },
  428. GetGoldenCallBack(_CallBack) {
  429. if (_CallBack)
  430. _CallBack({ resGolden: parseInt(this.Golden) });
  431. },
  432. SetGolden: function (Num) {
  433. let LastMoney = this.Golden;
  434. this.Golden = Num;
  435. let CurrentMoney = Num;
  436. this.ManageUI.getComponent('ManageUI').GoldenChangeCallBack(this.GetGolden(), LastMoney, CurrentMoney);
  437. },
  438. PlusGolden: function (Num) {
  439. if (this.Golden + Num < 0) {
  440. this.SetGolden(0);
  441. }
  442. else {
  443. this.SetGolden(this.Golden + Num);
  444. }
  445. // task.task50W();
  446. },
  447. GetDiamond: function () {
  448. return parseInt(this.Diamond);
  449. },
  450. SetDiamond: function (num) {
  451. let LastMoney = this.Diamond;
  452. this.Diamond = num;
  453. let CurrentMoney = num;
  454. this.ManageUI.getComponent('ManageUI').DiamondChangeCallBack(this.GetDiamond(), LastMoney, CurrentMoney);
  455. },
  456. PlusDiamond: function (num) {
  457. if (this.Diamond + num < 0) {
  458. this.SetDiamond(0);
  459. }
  460. else {
  461. this.SetDiamond(this.Diamond + num);
  462. }
  463. },
  464. /**
  465. * 游戏里面的cnt 和神农呗
  466. */
  467. GetCNT: function () {
  468. return this.CNT;
  469. },
  470. SetCNT: function (Num) {
  471. this.CNT = Num;
  472. this.ManageUI.getComponent('ManageUI').CNTChangeCallBack(this.CNT);
  473. },
  474. GetSNB: function () {
  475. return this.SNB;
  476. },
  477. SetSNB: function (Num) {
  478. this.SNB = Num;
  479. this.ManageUI.getComponent('ManageUI').SNBChangeCallBack(this.SNB);
  480. },
  481. GetWorkerLV: function () {
  482. return parseInt(this.WorkerLV);
  483. },
  484. SetWorkerLV: function (num) {
  485. this.WorkerLV = num;
  486. },
  487. PlusWorkerLV: function (num) {
  488. this.WorkerLV += num;
  489. },
  490. GetWorkerNum: function () {
  491. return parseInt(this.WorkerNum);
  492. },
  493. SetWorkerNum: function (num) {
  494. this.WorkerNum = num;
  495. },
  496. PlusWorkerNum: function (num) {
  497. this.WorkerNum += num;
  498. },
  499. GetLastTimeEveryDayRewardsDate: function () {
  500. return this.LastTimeEveryDayRewardsDate;
  501. },
  502. //获取转盘 时间
  503. GetLastTimeLuckDate: function () {
  504. return this.LastTimeLuckDate;
  505. },
  506. //设置转盘时间
  507. SetLastTimeLuckDate: function (DateString) {
  508. this.LastTimeLuckDate = DateString;
  509. },
  510. SetLastTimeEveryDayRewardsDate: function (DateString) {
  511. this.LastTimeEveryDayRewardsDate = DateString;
  512. },
  513. //工人信息数组
  514. GetWorkerCharacterInfoArray: function () {
  515. return this.CharacterInfoArray;
  516. },
  517. SetWorkerCharacterInfoArray: function (item) {
  518. this.CharacterInfoArray = item;
  519. },
  520. /*** */
  521. GetWorkerCapacity: function () {
  522. return parseInt((this.WorkerCapacity));
  523. },
  524. SetWorkerCapacity: function (num) {
  525. this.WorkerCapacity = num;
  526. },
  527. PlusWorkerCapacity: function (num) {
  528. this.WorkerCapacity += num;
  529. },
  530. GetTerritoryStateArray: function () {
  531. return this.TerritoryStateArray;
  532. },
  533. SetTerritoryStateArray: function (aTerritoryStateArray) {
  534. this.TerritoryStateArray = aTerritoryStateArray;
  535. },
  536. PlusTerritoryStateArray: function (Item) {
  537. this.TerritoryStateArray.push(Item);
  538. },
  539. GetBuildingStateArray: function () {
  540. return this.BuildingStateArray;
  541. },
  542. SetBuildingStateArray: function (aBuildingStateArray) {
  543. this.BuildingStateArray = aBuildingStateArray;
  544. },
  545. PlusBuildingStateArray: function (Item) {
  546. this.BuildingStateArray.push(Item);
  547. },
  548. GetBuildingLockStateArray: function () {
  549. return this.BuildingLockStateArray;
  550. },
  551. SetBuildingLockStateArray: function (aArray) {
  552. this.BuildingLockStateArray = aArray;
  553. },
  554. PlusBuildingLockStateArray: function (Item) {
  555. this.BuildingLockStateArray.push(Item);
  556. },
  557. GetBuildingNumArray: function () {
  558. return this.BuildingNumArray;
  559. },
  560. SetBuildingNumArray: function (aBuildingNumArray) {
  561. this.BuildingNumArray = aBuildingNumArray;
  562. },
  563. PlusBuildingNumArray: function (Item) {
  564. this.BuildingNumArray.push(Item);
  565. },
  566. //操作钻石数据
  567. GetDiamondNumArray: function () {
  568. return this.DiamondNumArray;
  569. },
  570. SetDiamondNumArray: function (aDiamondNumArray) {
  571. this.DiamondNumArray = aDiamondNumArray;
  572. },
  573. GetEveryDayRewardsArray: function () {
  574. return this.EveryDayRewardsArray;
  575. },
  576. SetEveryDayRewardsArray: function (aArray) {
  577. this.EveryDayRewardsArray = aArray;
  578. },
  579. PlusEveryDayRewardsArray: function (Item) {
  580. this.EveryDayRewardsArray.push(Item);
  581. },
  582. GetFoodTradeState: function () {
  583. return parseInt((this.FoodTradeState));
  584. },
  585. SetFoodTradeState: function (num) {
  586. this.FoodTradeState = num;
  587. },
  588. PlusFoodTradeState: function (num) {
  589. this.FoodTradeState += num;
  590. },
  591. GetWoodTradeState: function () {
  592. return parseInt((this.WoodTradeState));
  593. },
  594. SetWoodTradeState: function (num) {
  595. this.WoodTradeState = num;
  596. },
  597. PlusWoodTradeState: function (num) {
  598. this.WoodTradeState += num;
  599. },
  600. GetMineralTradeState: function () {
  601. return parseInt((this.MineralTradeState));
  602. },
  603. SetMineralTradeState: function (num) {
  604. this.MineralTradeState = num;
  605. },
  606. PlusMineralTradeState: function (num) {
  607. this.MineralTradeState += num;
  608. },
  609. GetLotteryTimes: function () {
  610. return parseInt((this.LotteryTimes));
  611. },
  612. SetLotteryTimes: function (num) {
  613. this.LotteryTimes = num;
  614. },
  615. PlusLotteryTimes: function (num) {
  616. this.LotteryTimes += num;
  617. },
  618. //初始化道路
  619. onSpawnHighway() {
  620. // 获取InitPoint层
  621. let InitPos = this._tiledMap.getLayer('InitPoint');
  622. InitPos.enabled = false;
  623. //创建公路数据
  624. if (this.readData && this.readData.allHighwayStylesAndIndex) {
  625. //保存到内存中
  626. this.GameData_highwayIndex = this.readData.highwayIndex;
  627. //先赋值道路的对象数组
  628. let _AllHighwayAIndex = GlobalD.game.AllHighwayStylesAndIndex = this.readData.allHighwayStylesAndIndex;
  629. //获取最后建造公路的层级
  630. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  631. for (let i = 0; i < _AllHighwayAIndex.length; i++) {
  632. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  633. highwayTemp.parent = this.HighwayLayer.node;
  634. let tiledTile = highwayTemp.addComponent('TiledTile');
  635. let _tiledPos = GlobalD.TiledMap.analyticalIndexData(_AllHighwayAIndex[i].highwayInfoIndex);
  636. tiledTile.x = _tiledPos.x;
  637. tiledTile.y = _tiledPos.y;
  638. //地图设置可行走区域公路设置
  639. AStar.setMapSolid(_tiledPos.x, _tiledPos.y, 0);
  640. // _buildId ==0 是公路
  641. let _buildId = 0;
  642. let occupyTemp = cc.v2(_buildId, _AllHighwayAIndex[i].highwayInfoIndex);
  643. GlobalD.game.OccupyArray.push(occupyTemp);
  644. highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(_AllHighwayAIndex[i].highwayInfoType);
  645. }
  646. } else {
  647. //如果用户没有存储数据
  648. //根据InitPoint 的数据,创建初始化的地图数据 的初始化数据
  649. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  650. for (var i = 0; i < 32; i++) {
  651. //去除中间两条路
  652. if (i == 16 || i == 17) continue;
  653. for (var j = 0; j < 32; j++) {
  654. let tilesPos = cc.v2(i, j);
  655. if (InitPos.getTileGIDAt(tilesPos)) {
  656. let index = GlobalD.TiledMap.getIndex(tilesPos);
  657. this.GameData_highwayIndex.push(index);
  658. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  659. highwayTemp.parent = this.HighwayLayer.node;
  660. let tiledTile = highwayTemp.addComponent('TiledTile');
  661. tiledTile.x = tilesPos.x;
  662. tiledTile.y = tilesPos.y;
  663. // cc.log('onSpawnHighway2');
  664. //地图设置可行走区域公路设置
  665. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  666. // _buildId ==0 是公路
  667. let _buildId = 0;
  668. let occupyTemp = cc.v2(_buildId, index);
  669. GlobalD.game.OccupyArray.push(occupyTemp);
  670. let tile = InitPos.getTiledTileAt(tilesPos.x, tilesPos.y, true);
  671. let _MoveType = reGameStates.HighwayType.moveX;
  672. // if (tile.gid == 15) {
  673. // _MoveType = reGameStates.HighwayType.moveX;
  674. // } else
  675. if (tile.gid == 16) {
  676. _MoveType = reGameStates.HighwayType.moveY;
  677. } else if (tile.gid == 11) {
  678. _MoveType = reGameStates.HighwayType.ZebraCrossingX;
  679. } else if (tile.gid == 12) {
  680. _MoveType = reGameStates.HighwayType.ZebraCrossingY;
  681. }
  682. // return;
  683. //更换公路样式
  684. GlobalD.game.onCreateDifferentRoadStyles({
  685. _buildId: _buildId,
  686. _highwayType: _MoveType,
  687. _roadIndex: index,
  688. _hightwayNode: highwayTemp
  689. });
  690. }
  691. }
  692. }
  693. }
  694. //外面的两条路
  695. let initRoad = this._tiledMap.getLayer('Road');
  696. initRoad.enabled = false;
  697. for (var i = 16; i < 18; i++) {
  698. for (var j = 0; j < 32; j++) {
  699. let tilesPos = cc.v2(i, j);
  700. // cc.log('road tilesPos',tilesPos)
  701. if (initRoad.getTileGIDAt(tilesPos)) {
  702. let index = GlobalD.TiledMap.getIndex(tilesPos);
  703. this.GameData_highwayIndex.push(index);
  704. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  705. highwayTemp.parent = this.HighwayLayer.node;
  706. let tiledTile = highwayTemp.addComponent('TiledTile');
  707. tiledTile.x = tilesPos.x;
  708. tiledTile.y = tilesPos.y;
  709. // cc.log('initRoad');
  710. //地图设置可行走区域公路设置
  711. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  712. let _buildId = 0;
  713. let occupyTemp = cc.v2(_buildId, index);
  714. GlobalD.game.OccupyArray.push(occupyTemp);
  715. //更换公路样式
  716. // if (j >= 25 && j <= 27 || j >= 19 && j <= 21 || j >= 9 && j <= 11)
  717. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
  718. // else
  719. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.moveY);
  720. }
  721. }
  722. }
  723. },
  724. //清除所有的数据
  725. onClearAllData() {
  726. this.unschedule(this.AutoSaveData);
  727. //任务索引
  728. cc.sys.localStorage.removeItem('userdata');
  729. //跳回登录场景
  730. // cc.director.loadScene('Login');
  731. },
  732. //保存数据请求
  733. /**
  734. *
  735. * 现在转服务器存储,原本所有相关操作通过服务器计算。
  736. * 一、目前涉及到的是 SNT 和 SNB 两个货币相关,和买田地操作 (可以理解为租聘,三种类型)
  737. *
  738. */
  739. pushData: function () {
  740. // cc.log(this.EveryDayRewardsArray);
  741. // console.log(this.readData.BFirstLoadGame);
  742. var datas = {
  743. version: this.GameVersion,
  744. // 转后台处理
  745. BFirstLoadGame: 1,//只要push数据,就证明已经开始第一次游戏了
  746. /**游戏不修改,默认值 start */
  747. shareGive: this.shareGive,
  748. //签到分享给钻石金币
  749. signInGive: this.signInGive,
  750. //每个月 给的 低保
  751. EveryGive: this.EveryGive,
  752. //公共分享
  753. publicGive: this.publicGive,
  754. /**游戏不修改,默认值 end */
  755. //时间
  756. GameYear: this.GameYear,
  757. GameMonth: this.GameMonth,
  758. GameDay: this.GameDay,
  759. //这里只是单纯的存储起来
  760. CNT: this.CNT,
  761. SNB: this.SNB,
  762. Golden: this.Golden,
  763. Diamond: this.Diamond,
  764. WorkerLV: this.WorkerLV,
  765. WorkerNum: this.WorkerNum,
  766. WorkerCapacity: this.WorkerCapacity,
  767. GameDate: this.GameDate,
  768. LastTimeEveryDayRewardsDate: this.LastTimeEveryDayRewardsDate,
  769. LastTimeLuckDate: this.LastTimeLuckDate,
  770. EveryDayRewardsArray: this.EveryDayRewardsArray,//.join('_'),
  771. TerritoryStateArray: this.TerritoryStateArray,//.join('_'),
  772. //面板状态
  773. BuildingStateArray: this.BuildingStateArray,//.join('_'),
  774. BuildingLockStateArray: this.BuildingLockStateArray,//.join('_'),
  775. /**
  776. * SNB和CNT相关,需要后台筛选 田地 和 种子。单独处理
  777. */
  778. BuildingNumArray: this.BuildingNumArray,//.join('_'),
  779. DiamondNumArray: this.DiamondNumArray,//.join('_'),
  780. FoodTradeState: this.FoodTradeState,
  781. WoodTradeState: this.WoodTradeState,
  782. MineralTradeState: this.MineralTradeState,
  783. LotteryTimes: this.LotteryTimes,
  784. /**
  785. * 这些数据不用服务器初始化
  786. */
  787. //工人工作信息
  788. characterInfoArray: this.CharacterInfoArray,
  789. //道路数据
  790. highwayIndex: this.GameData_highwayIndex,
  791. allHighwayStylesAndIndex: GlobalD.game.AllHighwayStylesAndIndex,
  792. //建筑物数据
  793. buildingsInfo: this.GameData_buildings,
  794. //任务
  795. TaskIconCountClick: task.TaskIconCountClick
  796. }
  797. var data = [];
  798. // data["openid"] = userData.openId;
  799. // data["userdata"] = JSON.stringify(datas);
  800. data["playerData"] = JSON.stringify(datas);
  801. /** 推送信息 */
  802. utils.post(utils.api.playerPushInfo, data, (res, playerPushInfoTemp) => {
  803. // console.log('playerPushInfoTemp', playerPushInfoTemp);
  804. })
  805. return;
  806. //保存用户数据到本地
  807. cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
  808. },
  809. });