GameModel.ts 15 KB

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