GameData.js 39 KB

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