GameModel.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. import { _decorator, Component, Node, Prefab, director, find } from 'cc';
  2. import BufferManager from '../../engines/buffers/BufferManager';
  3. import IBuffer from '../../engines/buffers/IBuffer';
  4. import { EventDispatcher } from '../../engines/events/EventDispatcher';
  5. import { DataModel } from '../../engines/models/DataModel';
  6. import { DataModelEventType } from '../../engines/models/DataModelEventType';
  7. import { NoticeManager } from '../../engines/notices/NoticeManager';
  8. import { PlatformManager } from '../../Platform/PlatformManager';
  9. import { branchIdType } from '../../Platform/WeChat/branchIdType';
  10. import { WeChatPlatform } from '../../Platform/WeChat/WeChatPlatform';
  11. import AccelerateBuffer from '../buffers/AccelerateBuffer';
  12. import AutoSyntheticBuffer from '../buffers/AutoSyntheticBuffer';
  13. import GameConfigManager from './GameConfigManager';
  14. import { GamePropertys } from './GamePropertys';
  15. import { WeaponCell } from './weapons/WeaponCell';
  16. const { ccclass, property } = _decorator;
  17. export class GameModel extends DataModel {
  18. /**
  19. * 武器格子列表
  20. */
  21. private __weaponCells: WeaponCell[] = [];
  22. /**
  23. * 购买记录
  24. */
  25. private __buyHistory: Map<number, number> = new Map<number, number>();
  26. /**
  27. * 当前武器格子
  28. */
  29. private __currentWeaponCell: WeaponCell;
  30. constructor() {
  31. super();
  32. }
  33. /**
  34. * 设置默认属性
  35. */
  36. SetDefaultPropertys(): void {
  37. //默认武器
  38. this.__currentWeaponCell = new WeaponCell();
  39. this.__currentWeaponCell.cellId = -1;
  40. this.__currentWeaponCell.lastOutputTime = director.getCurrentTime();
  41. this.__currentWeaponCell.weaponId = 10101;
  42. //默认关卡
  43. this.currentLevel = 1;
  44. //默认栅栏
  45. this.currentFenceId = 30001;
  46. //默认最大已合成枪ID
  47. this.synthesisMaxWeaponId = 10101;
  48. this.gold = 0;
  49. this.diamond = 0;
  50. //12个默认格子
  51. let weaponCell: WeaponCell;
  52. for (let index = 0; index < 12; index++) {
  53. weaponCell = new WeaponCell();
  54. weaponCell.cellId = index;
  55. weaponCell.weaponId = index < 2 ? 10101 : -1;
  56. weaponCell.lastOutputTime = 0;
  57. this.__weaponCells.push(weaponCell);
  58. }
  59. //保存到本地
  60. this.SaveToLoacl();
  61. }
  62. private lastTime: number = 0;
  63. /**
  64. * 计算收益
  65. */
  66. CheckEarnings(): void {
  67. let currentTime: number = director.getCurrentTime();
  68. if (currentTime - this.lastTime < this.earningTime) {
  69. return;
  70. }
  71. this.lastTime = currentTime;
  72. let fullEarnings: number = this.fullEarnings;
  73. if (fullEarnings > 0) {
  74. console.log("产出金币:" + fullEarnings);
  75. this.gold += fullEarnings * (this.earningTime / 1000);
  76. }
  77. if (this.DataChanged) {
  78. this.SaveToLoacl();
  79. }
  80. }
  81. /**
  82. * 每秒总收益
  83. */
  84. get fullEarnings(): number {
  85. let result: number = 0;
  86. this.__weaponCells.forEach(weaponCell => {
  87. if (weaponCell.weaponId >= 0) {
  88. result += weaponCell.weaponConfig.earnings;
  89. }
  90. });
  91. if (this.__currentWeaponCell.weaponId >= 0) {
  92. result += this.__currentWeaponCell.weaponConfig.earnings;
  93. }
  94. return result;
  95. }
  96. /**
  97. * 收益计算间隔
  98. */
  99. get earningsInterval(): number {
  100. return GameConfigManager.getGlobalValue("earningsInterval");
  101. }
  102. /**
  103. * 收益间隔时间
  104. */
  105. get earningTime(): number {
  106. let bufferList: IBuffer[] = BufferManager.GetBufferGroup("Accelerate");
  107. if (bufferList == null || bufferList.length == 0) {
  108. return this.earningsInterval;
  109. }
  110. let buffer: AccelerateBuffer = bufferList[0] as AccelerateBuffer;
  111. return this.earningsInterval * buffer.rate;
  112. }
  113. /**
  114. * 自动合成武器
  115. */
  116. public AutoSynthetic(max: number = 1): void {
  117. let a: WeaponCell;
  118. let b: WeaponCell;
  119. let synIndex: number = 0;
  120. for (let aIndex = 0; aIndex < this.weaponCells.length; aIndex++) {
  121. a = this.weaponCells[aIndex];
  122. if (a.weaponId < 0 || GameConfigManager.WeaponIsMaxLevel(a.weaponId)) {
  123. continue;
  124. }
  125. for (let bIndex = aIndex + 1; bIndex < this.weaponCells.length; bIndex++) {
  126. b = this.weaponCells[bIndex];
  127. if (b.weaponId < 0 || GameConfigManager.WeaponIsMaxLevel(b.weaponId)) {
  128. continue;
  129. }
  130. //可以合成
  131. if (a.weaponId == b.weaponId) {
  132. this.SynthesisWeapon(a.cellId, b.cellId);
  133. synIndex++;
  134. //如果大于最大数
  135. if (synIndex >= max) {
  136. return;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. private DataChanged: boolean;
  143. DispatchEvent(key: string, data?: any): void {
  144. super.DispatchEvent(key, data);
  145. this.DataChanged = true;
  146. }
  147. /**
  148. * 设置当前武器ID
  149. */
  150. set currentWeaponId(value: number) {
  151. this.__currentWeaponCell.weaponId = value;
  152. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.currentWeaponId);
  153. }
  154. get currentWeaponId(): number {
  155. return this.__currentWeaponCell.weaponId;
  156. }
  157. /**
  158. * 合成武器
  159. * @param sourceCellId
  160. * @param targetCellId
  161. */
  162. public SynthesisWeapon(sourceCellId: number, targetCellId: number): void {
  163. let sourceCell: WeaponCell = this.FindCell(sourceCellId);
  164. let targetCell: WeaponCell = this.FindCell(targetCellId);
  165. if (sourceCell.weaponId != targetCell.weaponId) {
  166. console.error("要合成的武器ID不同!");
  167. return;
  168. }
  169. if (GameConfigManager.WeaponIsMaxLevel(sourceCell.weaponId)) {
  170. console.error("武器已到达最高等级!");
  171. }
  172. let newWeaponId: number = GameConfigManager.GetNextLevelWeaponId(sourceCell.weaponId);
  173. //删除
  174. sourceCell.weaponId = -1;
  175. targetCell.weaponId = newWeaponId;
  176. targetCell.lastOutputTime = director.getCurrentTime();
  177. //对比新枪和老记录的合成枪
  178. let newWeaponConfig = GameConfigManager.GetWeaponConfig(newWeaponId);
  179. let synthesisMaxWeaponConfig = GameConfigManager.GetWeaponConfig(this.synthesisMaxWeaponId);
  180. //新记录
  181. if (newWeaponConfig.level > synthesisMaxWeaponConfig.level) {
  182. this.synthesisMaxWeaponId = newWeaponId;
  183. }
  184. //打点合成最高等级的枪
  185. let weChat = PlatformManager.impl as WeChatPlatform;
  186. if (weChat instanceof WeChatPlatform) {
  187. let level = synthesisMaxWeaponConfig.level;
  188. if (newWeaponConfig.level > synthesisMaxWeaponConfig.level) {
  189. level = newWeaponConfig.level;
  190. }
  191. weChat.branchAnalytics(branchIdType.Synthetic, String(level))
  192. }
  193. //如果比手上的好
  194. if (this.__currentWeaponCell.weaponConfig && newWeaponConfig.level > this.__currentWeaponCell.weaponConfig.level) {
  195. this.EquipWeapon(targetCellId);
  196. return;
  197. }
  198. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.WeaponCell);
  199. }
  200. /**
  201. * 添加一个武器到武器格子
  202. * @param cellId
  203. * @param weaponID
  204. */
  205. public AddWeapon(cellId: number, weaponID: number): void {
  206. for (let index = 0; index < this.__weaponCells.length; index++) {
  207. const element = this.__weaponCells[index];
  208. if (element.cellId == cellId) {
  209. element.weaponId = weaponID;
  210. element.lastOutputTime = director.getCurrentTime();
  211. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.WeaponCell);
  212. return;
  213. }
  214. }
  215. }
  216. /**
  217. * 删除武器
  218. * @param cellId
  219. */
  220. public RemoveWeapon(cellId: number): void {
  221. for (let index = 0; index < this.__weaponCells.length; index++) {
  222. const element = this.__weaponCells[index];
  223. if (element.cellId == cellId) {
  224. element.weaponId = -1;
  225. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.WeaponCell);
  226. return;
  227. }
  228. }
  229. }
  230. /**
  231. * 卸下武器
  232. */
  233. public UnequipWeapon(cellId: number): void {
  234. let id: number = this.__currentWeaponCell.weaponId;
  235. this.currentWeaponId = -1;
  236. this.__currentWeaponCell.lastOutputTime = 0;
  237. this.AddWeapon(cellId, id);
  238. }
  239. /**
  240. * 装配武器
  241. * @param weaponId
  242. */
  243. public EquipWeapon(sourceCellId: number): void {
  244. let cell: WeaponCell = this.FindCell(sourceCellId);
  245. if (cell == null) {
  246. throw new Error("找不到武器槽:" + sourceCellId);
  247. }
  248. let oldWeaponId = this.currentWeaponId;
  249. this.currentWeaponId = cell.weaponId;
  250. this.__currentWeaponCell.lastOutputTime = cell.lastOutputTime;
  251. cell.weaponId = oldWeaponId;
  252. cell.lastOutputTime = 0;
  253. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.WeaponCell);
  254. }
  255. /**
  256. * 武器空槽位置
  257. */
  258. public get WeaponEmptyCellCount(): number {
  259. let count: number = 0;
  260. this.__weaponCells.forEach(element => {
  261. if (element.weaponId < 0) {
  262. count++;
  263. }
  264. });
  265. return count;
  266. }
  267. /**
  268. * 寻找武器空槽位
  269. */
  270. public FindWeaponEmptyCell(): WeaponCell {
  271. for (let index = 0; index < this.__weaponCells.length; index++) {
  272. const element = this.__weaponCells[index];
  273. if (element.weaponId < 0) {
  274. return element;
  275. }
  276. }
  277. return null;
  278. }
  279. /**
  280. * 当前快捷可购买的武器
  281. */
  282. public get CurrentQuickBuyWeaponId(): number {
  283. return GameConfigManager.GetQuickBuyWeaponId(this.synthesisMaxWeaponId);
  284. }
  285. /**
  286. * 查询购买价格
  287. * @param weapon
  288. */
  289. public GetQuickBuyPrice(): number {
  290. return this.GetWeaponBuyPrice(this.CurrentQuickBuyWeaponId);
  291. }
  292. /**
  293. * 查询购买价格
  294. * @param weaponId
  295. */
  296. public GetWeaponBuyPrice(weaponId: number): number {
  297. //已购买的次数
  298. let buyCount: number = this.GetBuyCount(weaponId);
  299. let weaponConfig: any = GameConfigManager.GetWeaponConfig(weaponId);
  300. //价格
  301. let price: number = weaponConfig.consumeGold;
  302. //系数
  303. let coefficient: number = weaponConfig.coefficient;
  304. if (buyCount < 0) {
  305. buyCount = 0;
  306. }
  307. return Math.floor(price * Math.pow(coefficient, buyCount));
  308. }
  309. /**
  310. * 已购买次数
  311. * @param weaponId
  312. */
  313. public GetBuyCount(weaponId: number): number {
  314. let buyCount: number = 0;
  315. if (this.__buyHistory.has(weaponId)) {
  316. buyCount = this.__buyHistory.get(weaponId);
  317. }
  318. return buyCount;
  319. }
  320. /**
  321. * 购买武器
  322. * @param type 0 金币购买 1钻石购买 2视频购买
  323. * @param weaponId
  324. */
  325. public BuyWeapon(type: number, weaponId: number): boolean {
  326. let price: number;
  327. let weaponConfig: any
  328. //找到空槽位
  329. let weaponCell: WeaponCell = this.FindWeaponEmptyCell();
  330. if (weaponCell == null) {
  331. NoticeManager.ShowPrompt("没有空槽位了!");
  332. return false;
  333. }
  334. if (type == 0) {
  335. price = this.GetWeaponBuyPrice(weaponId);
  336. if (this.gold < price) {
  337. NoticeManager.ShowPrompt("金币不足,不能购买武器!");
  338. return false;
  339. }
  340. //扣钱
  341. let currentGold: number = this.gold;
  342. currentGold -= price;
  343. if (currentGold < 0) {
  344. currentGold = 0;
  345. }
  346. this.gold = currentGold;
  347. //记录购买次数
  348. weaponConfig = GameConfigManager.GetWeaponConfig(weaponId);
  349. let buyCount: number = this.GetBuyCount(weaponId);
  350. buyCount++;
  351. if (buyCount > weaponConfig.frequencyLimit) {
  352. buyCount = weaponConfig.frequencyLimit;
  353. }
  354. this.__buyHistory.set(weaponId, buyCount);
  355. } else if (type == 1) {
  356. let weaponConfig: any = GameConfigManager.GetWeaponConfig(weaponId);
  357. price = weaponConfig.consumeDiamond;
  358. if (this.diamond < price) {
  359. NoticeManager.ShowPrompt("宝石不足,不能购买武器!");
  360. return false;
  361. }
  362. //扣钱
  363. let currentDiamond: number = this.gold;
  364. currentDiamond -= price;
  365. if (currentDiamond < 0) {
  366. currentDiamond = 0;
  367. }
  368. this.diamond = currentDiamond;
  369. }
  370. //发货
  371. this.AddWeapon(weaponCell.cellId, weaponId);
  372. return true;
  373. }
  374. /**
  375. * 通过武器槽ID查找
  376. * @param id
  377. */
  378. public FindCell(id: number): WeaponCell {
  379. for (let index = 0; index < this.__weaponCells.length; index++) {
  380. const element = this.__weaponCells[index];
  381. if (element.cellId == id) {
  382. return element;
  383. }
  384. }
  385. return null;
  386. }
  387. /**
  388. * 武器格子列表
  389. */
  390. public get weaponCells(): WeaponCell[] {
  391. return this.__weaponCells;
  392. }
  393. protected OnReadByLocal(data: any): void {
  394. let currentTime: number = director.getCurrentTime();
  395. //当前武器格
  396. this.__currentWeaponCell = new WeaponCell();
  397. for (const key in data.currentWeaponCell) {
  398. if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key) && Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) {
  399. const element = data.currentWeaponCell[key];
  400. this.__currentWeaponCell[key] = element;
  401. }
  402. }
  403. this.__currentWeaponCell.lastOutputTime = currentTime;
  404. //武器格子
  405. let weaponCells: any[] = data.weaponCells;
  406. this.__weaponCells = [];
  407. let weaponCell: WeaponCell;
  408. if (weaponCells != null) {
  409. weaponCells.forEach(element => {
  410. weaponCell = new WeaponCell();
  411. for (const key in element) {
  412. if (Object.prototype.hasOwnProperty.call(element, key) && Object.prototype.hasOwnProperty.call(weaponCell, key)) {
  413. const item = element[key];
  414. weaponCell[key] = item;
  415. }
  416. }
  417. weaponCell.lastOutputTime = currentTime;
  418. this.__weaponCells.push(weaponCell);
  419. });
  420. }
  421. //自动合成Buffer剩余时间
  422. if (data.autoSynthesisTime > 0) {
  423. let buffer: AutoSyntheticBuffer = new AutoSyntheticBuffer("AutoSynthesis", data.autoSynthesisTime);
  424. BufferManager.RunBuffer(buffer);
  425. }
  426. //加速Buffer
  427. if (data.accelerateTime > 0) {
  428. let buffer: AccelerateBuffer = new AccelerateBuffer("Accelerate", data.accelerateTime);
  429. BufferManager.RunBuffer(buffer);
  430. }
  431. }
  432. protected OnSaveToLocal(data: any): void {
  433. data.weaponCells = this.__weaponCells;
  434. data.currentWeaponCell = this.__currentWeaponCell;
  435. //自动合成Buffer剩余时间
  436. let buffers: IBuffer[] = BufferManager.GetBufferGroup("AutoSynthesis");
  437. if (buffers != null && buffers.length > 0) {
  438. let buffer: IBuffer = buffers[0];
  439. data.autoSynthesisTime = buffer.GetTimeRemaining();
  440. } else {
  441. data.autoSynthesisTime = 0;
  442. }
  443. //加速Buffer
  444. buffers = BufferManager.GetBufferGroup("Accelerate");
  445. if (buffers != null && buffers.length > 0) {
  446. let accelerateBuffer: AccelerateBuffer = buffers[0] as AccelerateBuffer;
  447. data.accelerateTime = accelerateBuffer.GetTimeRemaining();
  448. } else {
  449. data.accelerateTime = 0;
  450. }
  451. }
  452. private static instance: GameModel;
  453. public static get single(): GameModel {
  454. if (this.instance == null) {
  455. this.instance = new GameModel();
  456. }
  457. return this.instance;
  458. }
  459. /**
  460. * 自动合成Buffer剩余时间
  461. */
  462. get autoSynthesisTime(): number {
  463. return this.GetProperty(GamePropertys.autoSynthesisTime);
  464. }
  465. /**
  466. * 加速Buffer剩余时间
  467. */
  468. get accelerateTime(): number {
  469. return this.GetProperty(GamePropertys.accelerateTime);
  470. }
  471. /**
  472. * 当前关卡
  473. */
  474. get currentLevel(): number {
  475. return this.GetProperty(GamePropertys.currentLevel);
  476. }
  477. set currentLevel(value: number) {
  478. this.SetProperty(GamePropertys.currentLevel, value);
  479. }
  480. /**
  481. * 试用栅栏ID
  482. */
  483. trialFenceId: number = -1;
  484. /**
  485. * 当前栅栏ID
  486. */
  487. get currentFenceId(): number {
  488. return this.GetProperty(GamePropertys.currentFenceId);
  489. }
  490. set currentFenceId(value: number) {
  491. this.SetProperty(GamePropertys.currentFenceId, value);
  492. }
  493. /**
  494. * 栅栏ID (包括试用)
  495. */
  496. get fenceId(): number {
  497. if (this.trialFenceId > 0) {
  498. return this.trialFenceId;
  499. }
  500. return this.currentFenceId;
  501. }
  502. /**
  503. * 金币
  504. */
  505. get gold(): number {
  506. return this.GetProperty(GamePropertys.gold);
  507. }
  508. set gold(value: number) {
  509. this.SetProperty(GamePropertys.gold, value);
  510. }
  511. /**
  512. * 积分
  513. */
  514. get integral(): number {
  515. return this._interal
  516. }
  517. private _interal: number;
  518. set integral(value: number) {
  519. this._interal = value;
  520. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED, GamePropertys.integral);
  521. }
  522. /**
  523. * 钻石
  524. */
  525. get diamond(): number {
  526. return this.GetProperty(GamePropertys.diamond);
  527. }
  528. set diamond(value: number) {
  529. this.SetProperty(GamePropertys.diamond, value);
  530. }
  531. /**
  532. * 击杀数量
  533. */
  534. get killCount(): number {
  535. return this.GetProperty(GamePropertys.killCount);
  536. }
  537. set killCount(value: number) {
  538. this.SetProperty(GamePropertys.killCount, value);
  539. }
  540. /**
  541. * 怒气值
  542. */
  543. get angerCount(): number {
  544. return this.GetProperty(GamePropertys.angerCount);
  545. }
  546. set angerCount(value: number) {
  547. this.SetProperty(GamePropertys.angerCount, value);
  548. }
  549. /**
  550. * 当前最大合成等级
  551. */
  552. get synthesisMaxWeaponId(): number {
  553. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  554. }
  555. set synthesisMaxWeaponId(value: number) {
  556. this.SetProperty(GamePropertys.synthesisMaxWeaponId, value);
  557. }
  558. /**
  559. * 上一次领取金币的时间
  560. */
  561. get lastGetGoldTime(): number {
  562. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  563. }
  564. set lastGetGoldTime(value: number) {
  565. this.SetProperty(GamePropertys.synthesisMaxWeaponId, value);
  566. }
  567. }