Player.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { Zone } from "../../../area/Area";
  2. import i18n from "../../../i18n/i18n";
  3. import Main from "../../../main/Main";
  4. import { ReveData } from "../../../util/CHttp";
  5. import CHttpEvent from "../../../util/CHttpEvent";
  6. import FFCalAttr, { FFAttr } from "../FFCalAttr";
  7. /**
  8. * 设置数据
  9. */
  10. export interface SettingData {
  11. music_ON_OFF_1: boolean,
  12. music_Slider_1: number,
  13. music_ON_OFF_2: boolean,
  14. music_Slider_2: number,
  15. }
  16. /**
  17. * 账号数据
  18. */
  19. export interface UserData {
  20. id: number
  21. token: string
  22. zone: Zone
  23. }
  24. /**
  25. * 角色数据
  26. */
  27. export interface RoleAttr {
  28. id: string
  29. token: string
  30. name: string
  31. renameCount: number
  32. // level:number
  33. money: number
  34. gold: number
  35. zdl: number//战斗力
  36. icon: number//战队头像
  37. frame: number//战队框
  38. title: number//称号
  39. leader: number//队长
  40. power: number//体力
  41. openSkill: number//0,是否开启了技能
  42. // stageId:number//当前达到的关卡
  43. }
  44. export interface RoleIcon {
  45. data: Array<number>
  46. open: Array<number>
  47. }
  48. export interface FoodAttr {
  49. atk: number
  50. def: number
  51. hp: number
  52. sp: number
  53. water: number
  54. fire: number
  55. light: number
  56. earth: number
  57. dark: number
  58. wind: number
  59. thunder: number
  60. date: number
  61. count: number
  62. }
  63. /**
  64. * 道具
  65. */
  66. export interface GoodAttr {
  67. count: number
  68. }
  69. /**
  70. * 伙伴
  71. */
  72. export interface PetAttr {
  73. id: number,
  74. equips: Array<EquipAttr>
  75. }
  76. /**
  77. * 装备
  78. */
  79. export interface EquipAttr {
  80. __index: number,
  81. PI: number
  82. id: number
  83. qhMax: number
  84. star: number
  85. attr: Array<any>
  86. }
  87. /**
  88. * 关卡中的获得物品记录
  89. * good = {
  90. * "1009":1,
  91. * "1010":1
  92. * }
  93. */
  94. export interface StageGoodAttr {
  95. starCount: number,
  96. good: any
  97. }
  98. /**
  99. * 关卡
  100. */
  101. export interface StageAttr {
  102. stageIndex: number,
  103. element: Array<string>;
  104. extraStage: Array<number>;
  105. data: Map<number, StageGoodAttr>;
  106. }
  107. /**
  108. * 神卡
  109. */
  110. export interface GodCardAttr {
  111. id: number
  112. level: number //升星等级
  113. feature: number //获得的特征数量
  114. reborn: number //重生次数
  115. }
  116. /**
  117. * 角色数据
  118. */
  119. export default class Player implements CHttpEvent {
  120. /**
  121. * 游戏系统设置
  122. */
  123. public setting: SettingData;
  124. /**
  125. * 角色基础数据
  126. */
  127. public role: RoleAttr;
  128. /**
  129. * 头像/框/称号
  130. */
  131. public roleIcon: RoleIcon;
  132. /**
  133. * 伙伴数据
  134. */
  135. public pet: Map<string, PetAttr>;
  136. /**
  137. * 装备
  138. */
  139. public equip: Array<EquipAttr>;
  140. /**
  141. * 神卡数据
  142. */
  143. public godCard: Map<string, GodCardAttr>;
  144. /**
  145. * 道具背包
  146. */
  147. public pack: Map<string, number>;
  148. /**
  149. * 关卡数据
  150. */
  151. public stage: StageAttr;
  152. /**
  153. * 吃食物增加属性
  154. */
  155. public foodAttr: FoodAttr;
  156. /**
  157. * 第一次充值赠送的id
  158. */
  159. public firstPayGave:Array<number>;
  160. public isNew: boolean = true;
  161. constructor(data: any) {
  162. this.readSetting();
  163. if (data) {
  164. this.role = data.role;
  165. this.foodAttr = data.foodAttr
  166. if (!this.role.name) {
  167. this.role.name = i18n.t('冒险者')+this.role.id
  168. }
  169. this.roleIcon = data.roleIcon
  170. this.pet = data.pet;
  171. this.godCard = data.godCard
  172. this.flushID();
  173. this.equip = data.equip;
  174. this.pack = data.pack;
  175. this.firstPayGave = data.firstPayGave;
  176. this.isNew = data.isNew;
  177. // cc.log('装备数量:',this.equip.length)
  178. this.flushIndex();
  179. }
  180. }
  181. private settingKey = '__setting';
  182. public readSetting() {
  183. let str = cc.sys.localStorage.getItem(this.settingKey)
  184. if (str) {
  185. this.setting = JSON.parse(str);
  186. } else {
  187. this.setting = {
  188. music_ON_OFF_1: true,
  189. music_Slider_1: 0.5,
  190. music_ON_OFF_2: true,
  191. music_Slider_2: 0.5,
  192. }
  193. }
  194. }
  195. public saveSetting() {
  196. cc.sys.localStorage.setItem(this.settingKey, JSON.stringify(this.setting));
  197. }
  198. private flushID() {
  199. for (const id in this.pet) {
  200. let pet = this.pet[id];
  201. pet.id = parseInt(id);
  202. }
  203. }
  204. public getPet(id) {
  205. return this.pet[id]
  206. }
  207. private flushIndex() {
  208. for (let i = 0; i < this.equip.length; i++) {
  209. let element = this.equip[i];
  210. element.__index = i;
  211. }
  212. }
  213. public getGoodCount(id) {
  214. let count = this.pack['' + id]
  215. if (count == undefined) {
  216. return 0
  217. }
  218. return count
  219. }
  220. /**
  221. * 监听网络
  222. * @param reveData
  223. */
  224. public httpEvent(reveData: ReveData) {
  225. // cc.log('Player reve : ',this);
  226. // cc.log('reve : ',reveData);
  227. let data = reveData.data;
  228. //更新基础属性
  229. if (data._role != undefined) {
  230. if (data._role.power != undefined) {
  231. this.role.power = data._role.power;
  232. }
  233. if (data._role._money != undefined) {
  234. this.role.money += data._role._money;
  235. }
  236. if (data._role._gold != undefined) {
  237. this.role.gold += data._role._gold;
  238. }
  239. if (data._role.gold != undefined) {
  240. this.role.gold = data._role.gold;
  241. }
  242. if (data._role.renameCount != undefined) {
  243. this.role.renameCount += data._role.renameCount;
  244. }
  245. if(data._firstPayGave){
  246. this.firstPayGave = data._firstPayGave;
  247. }
  248. }
  249. //更新背包
  250. if (data._pack != undefined) {
  251. for (let i = 0; i < data._pack.length; i++) {
  252. const element = data._pack[i];
  253. let v = this.pack["" + element.id];
  254. if (v != undefined) {
  255. v += element.count;
  256. this.pack["" + element.id] = v;
  257. } else {
  258. this.pack["" + element.id] = element.count;
  259. }
  260. }
  261. }
  262. //移除装备
  263. let isFlush = false;
  264. if (data._equipRemove != undefined) {
  265. let tmp = []
  266. for (let i = 0; i < this.equip.length; i++) {
  267. if (data._equipRemove.indexOf(i) < 0) {
  268. const element = this.equip[i];
  269. tmp.push(element)
  270. }
  271. }
  272. this.equip = tmp
  273. isFlush = true;
  274. }
  275. //添加装备
  276. if (data._equip != undefined) {
  277. for (let i = 0; i < data._equip.length; i++) {
  278. const element = data._equip[i];
  279. this.equip.push(element);
  280. }
  281. isFlush = true;
  282. }
  283. //添加头像/头像框/称号
  284. if (data._roleIcon != undefined) {
  285. for (let i = 0; i < data._roleIcon.length; i++) {
  286. const element = data._roleIcon[i];
  287. this.roleIcon.data.push(element);
  288. }
  289. }
  290. // //更新装备
  291. // if(data._equipUpdate != undefined){
  292. // for (let i = 0; i < data._equipUpdate.length; i++) {
  293. // const element = data._equipUpdate[i];
  294. // this.equip.push(element);
  295. // }
  296. // isFlush = true;
  297. // }
  298. if (isFlush) {
  299. this.flushIndex();
  300. }
  301. //更新伙伴
  302. if (data._pet != undefined) {
  303. for (let id in data._pet) {
  304. let element: PetAttr = data._pet[id];
  305. let v = this.pet[id];
  306. if (v == undefined) {
  307. this.pet[id] = element;
  308. element.id = parseInt(id);
  309. } else {
  310. v.equips = element.equips;
  311. }
  312. }
  313. }
  314. //添加神卡
  315. if (data._godCard != undefined) {
  316. for (let i = 0; i < data._godCard.length; i++) {
  317. const element = data._godCard[i];
  318. this.godCard[element.id] = element;
  319. }
  320. }
  321. }
  322. public getFight(main: Main): Array<FFAttr> {
  323. let fds = [];
  324. for (const id in this.pet) {
  325. let data = this.pet[id];
  326. fds.push(FFCalAttr.getAttr(main, data));
  327. }
  328. return fds;
  329. }
  330. /**
  331. * 刷新战斗力
  332. */
  333. public flushZdl(main) {
  334. let petMap = this.pet;
  335. let zdl = 0
  336. for (const id in petMap) {
  337. let petAttr = petMap[id];
  338. let attrData = FFCalAttr.getAttr(main, petAttr)
  339. zdl += attrData.zdl
  340. }
  341. this.role.zdl = zdl
  342. return zdl
  343. }
  344. }