UserData.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import { _decorator, game, sys, utils} from "cc";
  2. import { Singleton } from "../core/manager/Singleton";
  3. import { Constants } from "./Constants";
  4. import { Utils } from "../utils/Utils";
  5. import { csvMgr } from "../core/csv/CsvManager";
  6. import { stateMgr } from "../core/manager/StateManager";
  7. const { ccclass, property } = _decorator;
  8. /*
  9. @description 枪的属性值
  10. */
  11. export enum GunAttribute {
  12. /**伤害攻击*/
  13. attack = "attack",
  14. /**子弹速度*/
  15. bulletSpeed = "bulletSpeed",
  16. /**稳定性*/
  17. stability = "stability",
  18. /**弹夹容量*/
  19. magazine = "magazine",
  20. /**换弹速度*/
  21. reloadingSpeed = "reloadingSpeed",
  22. /**镜头缩放速度*/
  23. zoomingSpeed = "zoomingSpeed"
  24. }
  25. @ccclass("UserData")
  26. class UserData extends Singleton {
  27. //枪的属性列表
  28. public gunAttrKeys:string[] = Object.values(GunAttribute);
  29. //玩家是否连续通关 如果连续通关 这个值会大于1
  30. public passNum: number = 0;
  31. //玩家数据
  32. private _data: any = null;
  33. public get data() {
  34. if (!this._data) {
  35. this.loadFromCache();
  36. this._data = new Proxy(this._data, {
  37. set: (target, prop, value) => {
  38. target[prop] = value;
  39. if(prop === "gold") {//金币
  40. stateMgr.updateAllUI(Constants.gold);
  41. }else if(prop === "diamond") {//钻石
  42. stateMgr.updateAllUI(Constants.diamond);
  43. }
  44. this.saveToCache();
  45. return true;
  46. }
  47. });
  48. }
  49. return this._data;
  50. }
  51. //敌人表
  52. public _enemyTable: Array<any> = null;
  53. public get enemyTable(){
  54. if(!this._enemyTable){
  55. this._enemyTable = csvMgr.getTableArr('enemy');
  56. }
  57. return this._enemyTable;
  58. }
  59. //敌人使用的武器表
  60. public _enemyWeaponTable: Array<any> = null;
  61. public get enemyWeaponTable(){
  62. if(!this._enemyWeaponTable){
  63. this._enemyWeaponTable = csvMgr.getTableArr('enemy_guns');
  64. }
  65. return this._enemyWeaponTable;
  66. }
  67. //扔飞票的表
  68. public _boomerangTable: Array<any> = null;
  69. public get boomerangTable(){
  70. if(!this._boomerangTable){
  71. this._boomerangTable = csvMgr.getTableArr('boomerang');
  72. }
  73. return this._boomerangTable;
  74. }
  75. //扔飞票商店
  76. public _boomerangShopTable: Array<any> = null;
  77. public get boomerangShopTable(){
  78. if(!this._boomerangShopTable){
  79. this._boomerangShopTable = csvMgr.getTableArr('boomerang_shop');
  80. }
  81. return this._boomerangShopTable;
  82. }
  83. //商店
  84. public _shopTable: Array<any> = null;
  85. public get shopTable(){
  86. if(!this._shopTable){
  87. this._shopTable = csvMgr.getTableArr('shop');
  88. }
  89. return this._shopTable;
  90. }
  91. //玩家表
  92. public _playerTable: Array<any> = null;
  93. public get playerTable(){
  94. if(!this._playerTable){
  95. this._playerTable = csvMgr.getTableArr('player');
  96. }
  97. return this._playerTable;
  98. }
  99. //关卡数据表
  100. public _levelsTable: Array<any> = null;
  101. public get levelsTable(){
  102. if(!this._levelsTable){
  103. this._levelsTable = csvMgr.getTableArr('levels');
  104. }
  105. return this._levelsTable;
  106. }
  107. //item数据表
  108. public _itemTable: Array<any> = null;
  109. public get itemTable(){
  110. if(!this._itemTable){
  111. this._itemTable = csvMgr.getTableArr('item');
  112. }
  113. return this._itemTable;
  114. }
  115. //玩家所有枪
  116. public _playerGunsTable: Array<any> = null;
  117. public get playerGunsTable(){
  118. if(!this._playerGunsTable){
  119. this._playerGunsTable = csvMgr.getTableArr('player_guns');
  120. }
  121. return this._playerGunsTable;
  122. }
  123. //玩家所有枪的属性
  124. public _playerGunAttsTable: Array<any> = null;
  125. public get playerGunAttsTable(){
  126. if(!this._playerGunAttsTable){
  127. this._playerGunAttsTable = csvMgr.getTableArr('player_guns_atts');
  128. }
  129. return this._playerGunAttsTable;
  130. }
  131. //杂物比如沙袋油漆桶数据
  132. public _sundriesTable: Array<any> = null;
  133. public get sundriesTable(){
  134. if(!this._sundriesTable){
  135. this._sundriesTable = csvMgr.getTableArr('sundries');
  136. }
  137. return this._sundriesTable;
  138. }
  139. /**
  140. * 读取数据缓存
  141. */
  142. public loadFromCache(){
  143. let l = sys.localStorage.getItem(Constants.localCache.playerData);
  144. if(l){//解密出来转成序列化JSON
  145. this._data = JSON.parse(Utils.decrypt(l));
  146. }else{
  147. //玩家配置数据
  148. let configData:any = Utils.clone(this.playerTable[0]);
  149. //查询玩家所有枪
  150. this._data = {
  151. gold: 88880,//金币
  152. diamond: 88880,//钻石
  153. boomerang: 0,//飞镖个数
  154. level: 1,//关卡等级
  155. ...configData, //转化的基础配置数据
  156. guns: [],//玩家拥有的枪
  157. useIdx: 0,
  158. };
  159. this.getGunData(configData.defaultWeapon,true,'');
  160. }
  161. }
  162. /**
  163. * 查询一个枪的数据 顺带把枪的属性等级提升的部分参数带过来
  164. * @param id 枪的id
  165. * @param isSave 是否保存绑定在玩家身上
  166. * @param attr_name 属性名字这儿要升级 只有当第一个的没有拥有这把枪的时候 才不传值
  167. * 如果是已经 拥有了这把枪 要升级属性时,这个方法不要重复调用,因为会 加等级上的参数值 原因是这个要没把枪持久化数据保存
  168. * @returns 返回枪查询的枪的数据
  169. */
  170. private getGunData(id: string,isSave: boolean = false,attr_name: string = ''):any{
  171. //如果玩家已经拥有该枪 则直接返回
  172. let gData: any = this._data.guns.find(e=>e.id == id);
  173. if(!gData){
  174. gData = Utils.clone(this.playerGunsTable).find(e=>e.id == id);
  175. }
  176. if(!gData)return;
  177. //属性名字这儿要升级 只有没有拥有这把枪的时候attr_name才默认不传值
  178. let attrKeys:any[] = Utils.clone(this.gunAttrKeys);
  179. if(!Utils.isNull(attr_name)){
  180. attrKeys = attrKeys.filter(e=>e == attr_name);
  181. }
  182. if(gData.hasAttrs == null
  183. || !Utils.isNull(attr_name)){
  184. attrKeys.forEach(name => {
  185. const levelKey:string = `${name}Level`;
  186. let attrLevel:number = 1;
  187. if(gData[levelKey] != null){
  188. attrLevel = gData[levelKey];
  189. }
  190. let fun: Function = (i,l,name)=>{
  191. return this.playerGunAttsTable.find(
  192. e=>e.id == i
  193. && e.level == l
  194. && e.attribute_name == name);
  195. };
  196. let glData:any = fun(id,attrLevel,name);
  197. if(glData){
  198. let nextGLData:any = fun(id,attrLevel + 1,name);
  199. if(!nextGLData){nextGLData = glData};
  200. //unique_id 这个字段为了识别到时候同一个属性 升级极端情况下 材料消耗相同的时候的判断
  201. if(name == GunAttribute.attack){
  202. gData.attack += glData.value;//伤害
  203. gData.attackUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  204. gData.attackLevel= glData.level;//属性等级
  205. gData.attackExpenditure = glData.expenditure;//当前等级消耗
  206. gData.nextAttackExpenditure = nextGLData ? nextGLData.expenditure : null;//伤害下一个等级升级消耗
  207. }else if(name == GunAttribute.bulletSpeed){
  208. gData.bulletSpeed += glData.value;//射速
  209. gData.bulletSpeedUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  210. gData.bulletSpeedLevel= glData.level;//属性等级
  211. gData.bulletSpeedExpenditure = glData.expenditure;
  212. gData.nextBulletSpeedExpenditure = nextGLData ? nextGLData.expenditure : null;//射速下一个等级升级消耗
  213. }else if(name == GunAttribute.stability){
  214. gData.stability += glData.value;//稳定性
  215. gData.stabilityUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  216. gData.stabilityLevel= glData.level;//属性等级
  217. gData.stabilityExpenditure = glData.expenditure;
  218. gData.nextStabilityExpenditure = nextGLData ? nextGLData.expenditure : null;//稳定性下一个等级升级消耗
  219. }else if(name == GunAttribute.magazine){
  220. gData.magazine+= glData.value;//弹匣
  221. gData.magazineUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  222. gData.magazineLevel= glData.level;//属性等级
  223. gData.magazineExpenditure = glData.expenditure;
  224. gData.nextMagazineExpenditure = nextGLData ? nextGLData.expenditure : null;//弹匣下一个等级升级消耗
  225. }else if(name == GunAttribute.reloadingSpeed){
  226. gData.reloadingSpeed += glData.value;//装填速度
  227. gData.reloadingSpeedUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  228. gData.reloadingSpeedLevel= glData.level;//属性等级
  229. gData.reloadingSpeedExpenditure = glData.expenditure;
  230. gData.nextReloadingSpeedExpenditure = nextGLData ? nextGLData.expenditure : null;//装填速度下一个等级升级消耗
  231. }else if(name == GunAttribute.zoomingSpeed){
  232. gData.zoomingSpeed += glData.value;//缩放速度
  233. gData.zoomingSpeedUniqueId = nextGLData.unique_id;//属性等级下的唯一id
  234. gData.zoomingSpeedLevel= glData.level;//属性等级
  235. gData.zoomingSpeedExpenditure = glData.expenditure;
  236. gData.nextZoomingSpeedExpenditure = nextGLData ? nextGLData.expenditure : null;//缩放速度下一个等级升级消耗
  237. }
  238. }
  239. });
  240. }
  241. gData.hasAttrs = true;
  242. if(isSave){
  243. let indx: number = this._data.guns.findIndex(e=>e.id == gData.id);
  244. if(indx == -1){
  245. this._data.guns.push(gData);
  246. }
  247. this.saveToCache();
  248. }
  249. return gData;
  250. }
  251. /**
  252. * 查询玩家的一把武器的某个属性的总的值 和 武器某个属性升级的最大等级
  253. * @param gun 查询的枪
  254. * @param attribute_name 属性
  255. */
  256. public getGunMaxValue(gun: any,attribute_name: string): {
  257. totalValue: number,
  258. maxLevel: number,
  259. isMaxLevel?: boolean
  260. }{
  261. if(!gun) return {totalValue: 0, maxLevel: 0,isMaxLevel: false};
  262. //过滤匹配的数据
  263. const filteredData = this.playerGunAttsTable.filter(
  264. e => e.id === gun.id && e.attribute_name === attribute_name
  265. );
  266. //计算总值
  267. const totalValue = filteredData.reduce((sum, item) => sum + item.value, 0);
  268. //计算最大等级 如果数组为空则返回0
  269. const maxLevel = filteredData.length > 0
  270. ? Math.max(...filteredData.map(item => item.level))
  271. : 0;
  272. //基础值 + 升级累加值
  273. const attrLevelKey:string = `${attribute_name}Level`;
  274. const baseData:any = userIns.playerGunsTable.find(e=>e.id == gun.id);
  275. return {
  276. totalValue: baseData[attribute_name] + totalValue,
  277. maxLevel: maxLevel,
  278. isMaxLevel: gun[attrLevelKey] >= maxLevel
  279. };
  280. }
  281. /**
  282. * 升级某个枪的属性
  283. */
  284. public upgradeGun(gun: any,attribute_name: string){
  285. if(!gun)return;
  286. let hasGun:any = this.data.guns.find(e=>e.id == gun.id);
  287. if(!hasGun)return;
  288. //查询属性的等级
  289. const attrLevelKey:string = `${attribute_name}Level`;
  290. hasGun[attrLevelKey]+= 1;
  291. this.getGunData(gun.id,true,attribute_name);
  292. }
  293. /**
  294. * 得到当前用户使用的枪
  295. */
  296. public getCurUseGun(){
  297. let pData:any = {};
  298. if(this.data.useIdx < this.data.guns.length){
  299. let gun = this.data.guns[this.data.useIdx];
  300. Object.assign(pData,this._data,gun);
  301. }
  302. return pData;
  303. }
  304. /**
  305. * 用户切换使用的枪
  306. * @param id 枪的id
  307. */
  308. public changeGun(id: string){
  309. if(Utils.isNull(id))return;
  310. let idx:number = this.data.guns.findIndex(e=>e.id == id);
  311. if(idx != -1){
  312. this.data.useIdx = idx;
  313. this.getGunData(id,true);
  314. }
  315. }
  316. /**
  317. * 解锁全部枪玩家的枪
  318. */
  319. public unlockAllGuns(){
  320. Utils.clone(this.playerGunsTable).forEach(e => {
  321. this.getGunData(e.id,true);
  322. });
  323. }
  324. /**
  325. * 是否拥有这把枪
  326. */
  327. public isHasGun(id: string){
  328. if(Utils.isNull(id))return false;
  329. let gun:any = this.data.guns.find(e=>e.id == id);
  330. return gun != null;
  331. }
  332. /**
  333. * 解锁武器
  334. * @param id 枪的id
  335. */
  336. public unlockGun(id: string){
  337. if(Utils.isNull(id) || this.isHasGun(id))return;
  338. this.getGunData(id,true);
  339. }
  340. /**
  341. * 得到当前关卡下的敌人数据
  342. */
  343. public getCurLevelData(){
  344. const safeIndex = Math.min(this.data.level - 1, this.levelsTable.length -1);
  345. return this.levelsTable[safeIndex];
  346. }
  347. /**
  348. * 保存到缓存中
  349. */
  350. public saveToCache(){
  351. const data = JSON.stringify(this.data);
  352. //序列化JSON字符串过后加密存储
  353. sys.localStorage.setItem(Constants.localCache.playerData, Utils.encrypt(data));
  354. }
  355. /**
  356. * 删除缓存
  357. */
  358. public removeData(){
  359. this._data = null;
  360. sys.localStorage.removeItem(Constants.localCache.playerData);
  361. this.loadFromCache();
  362. }
  363. }
  364. //全局单例
  365. export const userIns = UserData.ins();