GameModel.ts 19 KB

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