GameModel.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import { _decorator, Component, Node, Prefab, director, find } from 'cc';
  2. import { EventDispatcher } from '../../engines/events/EventDispatcher';
  3. import { DataModel } from '../../engines/models/DataModel';
  4. import { DataModelEventType } from '../../engines/models/DataModelEventType';
  5. import { NoticeManager } from '../../engines/notices/NoticeManager';
  6. import GameConfigManager from './GameConfigManager';
  7. import { GamePropertys } from './GamePropertys';
  8. import { WeaponCell } from './weapons/WeaponCell';
  9. const { ccclass, property } = _decorator;
  10. export class GameModel extends DataModel{
  11. /**
  12. * 武器格子列表
  13. */
  14. private __weaponCells:WeaponCell[]=[];
  15. /**
  16. * 购买记录
  17. */
  18. private __buyHistory:Map<number,number>=new Map<number,number>();
  19. /**
  20. * 当前武器格子
  21. */
  22. private __currentWeaponCell:WeaponCell;
  23. constructor(){
  24. super();
  25. }
  26. private lastTime:number=0;
  27. /**
  28. * 每秒总收益
  29. */
  30. public fullEarnings:number=0;
  31. /**
  32. * 收益计算间隔
  33. */
  34. get earningsInterval():number{
  35. return GameConfigManager.getGlobalValue("earningsInterval");
  36. }
  37. /**
  38. * 计算收益
  39. */
  40. CheckEarnings():void{
  41. let currentTime:number=director.getCurrentTime();
  42. if(currentTime-this.lastTime<1000){
  43. return;
  44. }
  45. this.lastTime=currentTime;
  46. let earningsIntervarS:number=(this.earningsInterval/1000);
  47. let fullEarnings:number=0;
  48. this.__weaponCells.forEach(weaponCell => {
  49. if(weaponCell.weaponId>=0){
  50. // weaponConfig=GameConfigManager.GetWeaponConfig(weaponCell.weaponId);
  51. // if(currentTime-weaponCell.lastOutputTime>this.earningsInterval){
  52. // weaponCell.lastOutputTime=currentTime;
  53. fullEarnings+=weaponCell.weaponConfig.earnings;
  54. // }
  55. }
  56. });
  57. if(this.__currentWeaponCell.weaponId>=0){
  58. // weaponConfig=GameConfigManager.GetWeaponConfig(this.__currentWeaponCell.weaponId);
  59. // if(currentTime-this.__currentWeaponCell.lastOutputTime>this.earningsInterval){
  60. // this.__currentWeaponCell.lastOutputTime=currentTime;
  61. fullEarnings+=this.__currentWeaponCell.weaponConfig.earnings;
  62. // }
  63. }
  64. if(fullEarnings>0){
  65. console.log("产出金币:"+fullEarnings);
  66. this.gold+=fullEarnings*earningsIntervarS;
  67. }
  68. this.fullEarnings=fullEarnings;
  69. }
  70. /**
  71. * 设置当前武器ID
  72. */
  73. set currentWeaponId(value:number){
  74. this.__currentWeaponCell.weaponId=value;
  75. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.currentWeaponId);
  76. }
  77. get currentWeaponId():number{
  78. return this.__currentWeaponCell.weaponId;
  79. }
  80. /**
  81. * 合成武器
  82. * @param sourceCellId
  83. * @param targetCellId
  84. */
  85. public SynthesisWeapon(sourceCellId:number,targetCellId:number):void{
  86. let sourceCell:WeaponCell=this.FindCell(sourceCellId);
  87. let targetCell:WeaponCell=this.FindCell(targetCellId);
  88. if(sourceCell.weaponId!=targetCell.weaponId){
  89. console.error("要合成的武器ID不同!");
  90. return;
  91. }
  92. if(GameConfigManager.WeaponIsMaxLevel(sourceCell.weaponId)){
  93. console.error("武器已到达最高等级!");
  94. }
  95. let newWeaponId:number=GameConfigManager.GetNextLevelWeaponId(sourceCell.weaponId);
  96. //删除
  97. sourceCell.weaponId=-1;
  98. targetCell.weaponId=newWeaponId;
  99. targetCell.lastOutputTime=director.getCurrentTime();
  100. //对比新枪和老记录的合成枪
  101. let newWeaponConfig=GameConfigManager.GetWeaponConfig(newWeaponId);
  102. let synthesisMaxWeaponConfig=GameConfigManager.GetWeaponConfig(this.synthesisMaxWeaponId);
  103. //新记录
  104. if(newWeaponConfig.level>synthesisMaxWeaponConfig.level){
  105. this.synthesisMaxWeaponId=newWeaponId;
  106. }
  107. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  108. }
  109. /**
  110. * 添加一个武器到武器格子
  111. * @param cellId
  112. * @param weaponID
  113. */
  114. public AddWeapon(cellId:number,weaponID:number):void{
  115. for (let index = 0; index < this.__weaponCells.length; index++) {
  116. const element = this.__weaponCells[index];
  117. if(element.cellId==cellId){
  118. element.weaponId=weaponID;
  119. element.lastOutputTime=director.getCurrentTime();
  120. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  121. return;
  122. }
  123. }
  124. }
  125. /**
  126. * 删除武器
  127. * @param cellId
  128. */
  129. public RemoveWeapon(cellId:number):void{
  130. for (let index = 0; index < this.__weaponCells.length; index++) {
  131. const element = this.__weaponCells[index];
  132. if(element.cellId==cellId){
  133. element.weaponId=-1;
  134. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  135. return;
  136. }
  137. }
  138. }
  139. /**
  140. * 卸下武器
  141. */
  142. public UnequipWeapon(cellId:number):void{
  143. let id:number=this.__currentWeaponCell.weaponId;
  144. this.currentWeaponId=-1;
  145. this.__currentWeaponCell.lastOutputTime=0;
  146. this.AddWeapon(cellId,id);
  147. }
  148. /**
  149. * 装配武器
  150. * @param weaponId
  151. */
  152. public EquipWeapon(sourceCellId:number):void{
  153. let cell:WeaponCell=this.FindCell(sourceCellId);
  154. if(cell==null){
  155. throw new Error("找不到武器槽:"+sourceCellId);
  156. }
  157. let oldWeaponId=this.currentWeaponId;
  158. this.currentWeaponId=cell.weaponId;
  159. this.__currentWeaponCell.lastOutputTime=cell.lastOutputTime;
  160. cell.weaponId=oldWeaponId;
  161. cell.lastOutputTime=0;
  162. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  163. }
  164. /**
  165. * 武器空槽位置
  166. */
  167. public get WeaponEmptyCellCount():number{
  168. let count:number=0;
  169. this.__weaponCells.forEach(element => {
  170. if(element.weaponId<0){
  171. count++;
  172. }
  173. });
  174. return count;
  175. }
  176. /**
  177. * 寻找武器空槽位
  178. */
  179. public FindWeaponEmptyCell():WeaponCell{
  180. for (let index = 0; index < this.__weaponCells.length; index++) {
  181. const element = this.__weaponCells[index];
  182. if(element.weaponId<0){
  183. return element;
  184. }
  185. }
  186. return null;
  187. }
  188. /**
  189. * 当前快捷可购买的武器
  190. */
  191. public get CurrentQuickBuyWeaponId():number{
  192. return GameConfigManager.GetQuickBuyWeaponId(this.synthesisMaxWeaponId);
  193. }
  194. /**
  195. * 查询购买价格
  196. * @param weapon
  197. */
  198. public GetQuickBuyPrice():number{
  199. return this.GetWeaponBuyPrice(this.CurrentQuickBuyWeaponId);
  200. }
  201. /**
  202. * 查询购买价格
  203. * @param weaponId
  204. */
  205. public GetWeaponBuyPrice(weaponId:number):number{
  206. //已购买的次数
  207. let buyCount:number=this.GetBuyCount(weaponId);
  208. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  209. //价格
  210. let price:number=weaponConfig.consumeGold;
  211. //系数
  212. let coefficient:number=weaponConfig.coefficient;
  213. if(buyCount<0){
  214. buyCount=0;
  215. }
  216. return Math.floor(price*Math.pow(coefficient,buyCount));
  217. }
  218. /**
  219. * 已购买次数
  220. * @param weaponId
  221. */
  222. public GetBuyCount(weaponId:number):number{
  223. let buyCount:number=0;
  224. if(this.__buyHistory.has(weaponId)){
  225. buyCount=this.__buyHistory.get(weaponId);
  226. }
  227. return buyCount;
  228. }
  229. /**
  230. * 购买武器
  231. * @param type 0 金币购买 1钻石购买 2视频购买
  232. * @param weaponId
  233. */
  234. public BuyWeapon(type:number,weaponId:number):void{
  235. let price:number;
  236. let weaponConfig:any
  237. //找到空槽位
  238. let weaponCell:WeaponCell=this.FindWeaponEmptyCell();
  239. if(weaponCell==null){
  240. NoticeManager.ShowPrompt("没有空槽位了!");
  241. return;
  242. }
  243. if(type==0){
  244. price=this.GetWeaponBuyPrice(weaponId);
  245. if(this.gold<price){
  246. NoticeManager.ShowPrompt("金币不足,不能购买武器!");
  247. return;
  248. }
  249. //扣钱
  250. let currentGold:number=this.gold;
  251. currentGold-=price;
  252. if(currentGold<0){
  253. currentGold=0;
  254. }
  255. this.gold=currentGold;
  256. //记录购买次数
  257. weaponConfig=GameConfigManager.GetWeaponConfig(weaponId);
  258. let buyCount:number=this.GetBuyCount(weaponId);
  259. buyCount++;
  260. if(buyCount>weaponConfig.frequencyLimit){
  261. buyCount=weaponConfig.frequencyLimit;
  262. }
  263. this.__buyHistory.set(weaponId,buyCount);
  264. }else if(type==1){
  265. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  266. price=weaponConfig.consumeDiamond;
  267. if(this.diamond<price){
  268. NoticeManager.ShowPrompt("宝石不足,不能购买武器!");
  269. return;
  270. }
  271. //扣钱
  272. let currentDiamond:number=this.gold;
  273. currentDiamond-=price;
  274. if(currentDiamond<0){
  275. currentDiamond=0;
  276. }
  277. this.diamond=currentDiamond;
  278. }
  279. //发货
  280. this.AddWeapon(weaponCell.cellId,weaponId);
  281. }
  282. /**
  283. * 通过武器槽ID查找
  284. * @param id
  285. */
  286. public FindCell(id:number):WeaponCell{
  287. for (let index = 0; index < this.__weaponCells.length; index++) {
  288. const element = this.__weaponCells[index];
  289. if(element.cellId==id){
  290. return element;
  291. }
  292. }
  293. return null;
  294. }
  295. /**
  296. * 武器格子列表
  297. */
  298. public get weaponCells():WeaponCell[]{
  299. return this.__weaponCells;
  300. }
  301. /**
  302. * 设置默认属性
  303. */
  304. SetDefaultPropertys():void{
  305. //默认武器
  306. this.__currentWeaponCell=new WeaponCell();
  307. this.__currentWeaponCell.cellId=-1;
  308. this.__currentWeaponCell.lastOutputTime=director.getCurrentTime();
  309. this.__currentWeaponCell.weaponId=10101;
  310. //默认关卡
  311. this.currentLevel=1;
  312. //默认栅栏
  313. this.currentFenceId=30001;
  314. //默认最大已合成枪ID
  315. this.synthesisMaxWeaponId=10101;
  316. this.gold=0;
  317. this.diamond=0;
  318. //12个默认格子
  319. let weaponCell:WeaponCell;
  320. for (let index = 0; index < 12; index++) {
  321. weaponCell=new WeaponCell();
  322. weaponCell.cellId=index;
  323. weaponCell.weaponId=10101;
  324. weaponCell.lastOutputTime=0;
  325. this.__weaponCells.push(weaponCell);
  326. }
  327. //保存到本地
  328. this.SaveToLoacl();
  329. }
  330. protected OnReadByLocal(data:any):void{
  331. //当前武器格
  332. this.__currentWeaponCell=new WeaponCell();
  333. for (const key in data.currentWeaponCell) {
  334. if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key)&&Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) {
  335. const element = data.currentWeaponCell[key];
  336. this.__currentWeaponCell[key]=element;
  337. }
  338. }
  339. //武器格子
  340. let weaponCells:any[]=data.weaponCells;
  341. this.__weaponCells=[];
  342. let weaponCell:WeaponCell;
  343. if(weaponCells!=null){
  344. weaponCells.forEach(element => {
  345. weaponCell=new WeaponCell();
  346. for (const key in element) {
  347. if (Object.prototype.hasOwnProperty.call(element, key)&&Object.prototype.hasOwnProperty.call(weaponCell, key)) {
  348. const item = element[key];
  349. weaponCell[key]=item;
  350. this.__weaponCells.push(weaponCell);
  351. }
  352. }
  353. });
  354. }
  355. }
  356. protected OnSaveToLocal(data:any):void{
  357. data.weaponCells=this.__weaponCells;
  358. data.currentWeaponCell=this.__currentWeaponCell;
  359. }
  360. private static instance:GameModel;
  361. public static get single():GameModel{
  362. if(this.instance==null){
  363. this.instance=new GameModel();
  364. }
  365. return this.instance;
  366. }
  367. /**
  368. * 当前关卡
  369. */
  370. get currentLevel():number{
  371. return this.GetProperty(GamePropertys.currentLevel);
  372. }
  373. set currentLevel(value:number){
  374. this.SetProperty(GamePropertys.currentLevel,value);
  375. }
  376. /**
  377. * 当前栅栏ID
  378. */
  379. get currentFenceId():number{
  380. return this.GetProperty(GamePropertys.currentFenceId);
  381. }
  382. set currentFenceId(value:number){
  383. this.SetProperty(GamePropertys.currentFenceId,value);
  384. }
  385. /**
  386. * 金币
  387. */
  388. get gold():number{
  389. return this.GetProperty(GamePropertys.gold);
  390. }
  391. set gold(value:number){
  392. this.SetProperty(GamePropertys.gold,value);
  393. }
  394. /**
  395. * 积分
  396. */
  397. get integral():number{
  398. return this.GetProperty(GamePropertys.integral);
  399. }
  400. set integral(value:number){
  401. this.SetProperty(GamePropertys.integral,value);
  402. }
  403. /**
  404. * 钻石
  405. */
  406. get diamond():number{
  407. return this.GetProperty(GamePropertys.diamond);
  408. }
  409. set diamond(value:number){
  410. this.SetProperty(GamePropertys.diamond,value);
  411. }
  412. /**
  413. * 击杀数量
  414. */
  415. get killCount():number{
  416. return this.GetProperty(GamePropertys.killCount);
  417. }
  418. set killCount(value:number){
  419. this.SetProperty(GamePropertys.killCount,value);
  420. }
  421. /**
  422. * 怒气值
  423. */
  424. get angerCount():number{
  425. return this.GetProperty(GamePropertys.angerCount);
  426. }
  427. set angerCount(value:number){
  428. this.SetProperty(GamePropertys.angerCount,value);
  429. }
  430. /**
  431. * 当前最大合成等级
  432. */
  433. get synthesisMaxWeaponId():number{
  434. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  435. }
  436. set synthesisMaxWeaponId(value:number){
  437. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  438. }
  439. /**
  440. * 上一次领取金币的时间
  441. */
  442. get lastGetGoldTime():number{
  443. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  444. }
  445. set lastGetGoldTime(value:number){
  446. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  447. }
  448. }