GameData.js 32 KB

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