GameModel.ts 18 KB

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