GameModel.ts 18 KB

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