GameModel.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  145. }
  146. /**
  147. * 添加一个武器到武器格子
  148. * @param cellId
  149. * @param weaponID
  150. */
  151. public AddWeapon(cellId:number,weaponID:number):void{
  152. for (let index = 0; index < this.__weaponCells.length; index++) {
  153. const element = this.__weaponCells[index];
  154. if(element.cellId==cellId){
  155. element.weaponId=weaponID;
  156. element.lastOutputTime=director.getCurrentTime();
  157. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  158. return;
  159. }
  160. }
  161. }
  162. /**
  163. * 删除武器
  164. * @param cellId
  165. */
  166. public RemoveWeapon(cellId:number):void{
  167. for (let index = 0; index < this.__weaponCells.length; index++) {
  168. const element = this.__weaponCells[index];
  169. if(element.cellId==cellId){
  170. element.weaponId=-1;
  171. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  172. return;
  173. }
  174. }
  175. }
  176. /**
  177. * 卸下武器
  178. */
  179. public UnequipWeapon(cellId:number):void{
  180. let id:number=this.__currentWeaponCell.weaponId;
  181. this.currentWeaponId=-1;
  182. this.__currentWeaponCell.lastOutputTime=0;
  183. this.AddWeapon(cellId,id);
  184. }
  185. /**
  186. * 装配武器
  187. * @param weaponId
  188. */
  189. public EquipWeapon(sourceCellId:number):void{
  190. let cell:WeaponCell=this.FindCell(sourceCellId);
  191. if(cell==null){
  192. throw new Error("找不到武器槽:"+sourceCellId);
  193. }
  194. let oldWeaponId=this.currentWeaponId;
  195. this.currentWeaponId=cell.weaponId;
  196. this.__currentWeaponCell.lastOutputTime=cell.lastOutputTime;
  197. cell.weaponId=oldWeaponId;
  198. cell.lastOutputTime=0;
  199. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  200. }
  201. /**
  202. * 武器空槽位置
  203. */
  204. public get WeaponEmptyCellCount():number{
  205. let count:number=0;
  206. this.__weaponCells.forEach(element => {
  207. if(element.weaponId<0){
  208. count++;
  209. }
  210. });
  211. return count;
  212. }
  213. /**
  214. * 寻找武器空槽位
  215. */
  216. public FindWeaponEmptyCell():WeaponCell{
  217. for (let index = 0; index < this.__weaponCells.length; index++) {
  218. const element = this.__weaponCells[index];
  219. if(element.weaponId<0){
  220. return element;
  221. }
  222. }
  223. return null;
  224. }
  225. /**
  226. * 当前快捷可购买的武器
  227. */
  228. public get CurrentQuickBuyWeaponId():number{
  229. return GameConfigManager.GetQuickBuyWeaponId(this.synthesisMaxWeaponId);
  230. }
  231. /**
  232. * 查询购买价格
  233. * @param weapon
  234. */
  235. public GetQuickBuyPrice():number{
  236. return this.GetWeaponBuyPrice(this.CurrentQuickBuyWeaponId);
  237. }
  238. /**
  239. * 查询购买价格
  240. * @param weaponId
  241. */
  242. public GetWeaponBuyPrice(weaponId:number):number{
  243. //已购买的次数
  244. let buyCount:number=this.GetBuyCount(weaponId);
  245. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  246. //价格
  247. let price:number=weaponConfig.consumeGold;
  248. //系数
  249. let coefficient:number=weaponConfig.coefficient;
  250. if(buyCount<0){
  251. buyCount=0;
  252. }
  253. return Math.floor(price*Math.pow(coefficient,buyCount));
  254. }
  255. /**
  256. * 已购买次数
  257. * @param weaponId
  258. */
  259. public GetBuyCount(weaponId:number):number{
  260. let buyCount:number=0;
  261. if(this.__buyHistory.has(weaponId)){
  262. buyCount=this.__buyHistory.get(weaponId);
  263. }
  264. return buyCount;
  265. }
  266. /**
  267. * 购买武器
  268. * @param type 0 金币购买 1钻石购买 2视频购买
  269. * @param weaponId
  270. */
  271. public BuyWeapon(type:number,weaponId:number):boolean{
  272. let price:number;
  273. let weaponConfig:any
  274. //找到空槽位
  275. let weaponCell:WeaponCell=this.FindWeaponEmptyCell();
  276. if(weaponCell==null){
  277. NoticeManager.ShowPrompt("没有空槽位了!");
  278. return false;
  279. }
  280. if(type==0){
  281. price=this.GetWeaponBuyPrice(weaponId);
  282. if(this.gold<price){
  283. NoticeManager.ShowPrompt("金币不足,不能购买武器!");
  284. return false;
  285. }
  286. //扣钱
  287. let currentGold:number=this.gold;
  288. currentGold-=price;
  289. if(currentGold<0){
  290. currentGold=0;
  291. }
  292. this.gold=currentGold;
  293. //记录购买次数
  294. weaponConfig=GameConfigManager.GetWeaponConfig(weaponId);
  295. let buyCount:number=this.GetBuyCount(weaponId);
  296. buyCount++;
  297. if(buyCount>weaponConfig.frequencyLimit){
  298. buyCount=weaponConfig.frequencyLimit;
  299. }
  300. this.__buyHistory.set(weaponId,buyCount);
  301. }else if(type==1){
  302. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  303. price=weaponConfig.consumeDiamond;
  304. if(this.diamond<price){
  305. NoticeManager.ShowPrompt("宝石不足,不能购买武器!");
  306. return false;
  307. }
  308. //扣钱
  309. let currentDiamond:number=this.gold;
  310. currentDiamond-=price;
  311. if(currentDiamond<0){
  312. currentDiamond=0;
  313. }
  314. this.diamond=currentDiamond;
  315. }
  316. //发货
  317. this.AddWeapon(weaponCell.cellId,weaponId);
  318. return true;
  319. }
  320. /**
  321. * 通过武器槽ID查找
  322. * @param id
  323. */
  324. public FindCell(id:number):WeaponCell{
  325. for (let index = 0; index < this.__weaponCells.length; index++) {
  326. const element = this.__weaponCells[index];
  327. if(element.cellId==id){
  328. return element;
  329. }
  330. }
  331. return null;
  332. }
  333. /**
  334. * 武器格子列表
  335. */
  336. public get weaponCells():WeaponCell[]{
  337. return this.__weaponCells;
  338. }
  339. protected OnReadByLocal(data:any):void{
  340. let currentTime:number=director.getCurrentTime();
  341. //当前武器格
  342. this.__currentWeaponCell=new WeaponCell();
  343. for (const key in data.currentWeaponCell) {
  344. if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key)&&Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) {
  345. const element = data.currentWeaponCell[key];
  346. this.__currentWeaponCell[key]=element;
  347. }
  348. }
  349. this.__currentWeaponCell.lastOutputTime=currentTime;
  350. //武器格子
  351. let weaponCells:any[]=data.weaponCells;
  352. this.__weaponCells=[];
  353. let weaponCell:WeaponCell;
  354. if(weaponCells!=null){
  355. weaponCells.forEach(element => {
  356. weaponCell=new WeaponCell();
  357. for (const key in element) {
  358. if (Object.prototype.hasOwnProperty.call(element, key)&&Object.prototype.hasOwnProperty.call(weaponCell, key)) {
  359. const item = element[key];
  360. weaponCell[key]=item;
  361. }
  362. }
  363. weaponCell.lastOutputTime=currentTime;
  364. this.__weaponCells.push(weaponCell);
  365. });
  366. }
  367. }
  368. protected OnSaveToLocal(data:any):void{
  369. data.weaponCells=this.__weaponCells;
  370. data.currentWeaponCell=this.__currentWeaponCell;
  371. }
  372. private static instance:GameModel;
  373. public static get single():GameModel{
  374. if(this.instance==null){
  375. this.instance=new GameModel();
  376. }
  377. return this.instance;
  378. }
  379. /**
  380. * 当前关卡
  381. */
  382. get currentLevel():number{
  383. return this.GetProperty(GamePropertys.currentLevel);
  384. }
  385. set currentLevel(value:number){
  386. this.SetProperty(GamePropertys.currentLevel,value);
  387. }
  388. /**
  389. * 当前栅栏ID
  390. */
  391. get currentFenceId():number{
  392. return this.GetProperty(GamePropertys.currentFenceId);
  393. }
  394. set currentFenceId(value:number){
  395. this.SetProperty(GamePropertys.currentFenceId,value);
  396. }
  397. /**
  398. * 金币
  399. */
  400. get gold():number{
  401. return this.GetProperty(GamePropertys.gold);
  402. }
  403. set gold(value:number){
  404. this.SetProperty(GamePropertys.gold,value);
  405. }
  406. /**
  407. * 积分
  408. */
  409. get integral():number{
  410. return this.GetProperty(GamePropertys.integral);
  411. }
  412. set integral(value:number){
  413. this.SetProperty(GamePropertys.integral,value);
  414. }
  415. /**
  416. * 钻石
  417. */
  418. get diamond():number{
  419. return this.GetProperty(GamePropertys.diamond);
  420. }
  421. set diamond(value:number){
  422. this.SetProperty(GamePropertys.diamond,value);
  423. }
  424. /**
  425. * 击杀数量
  426. */
  427. get killCount():number{
  428. return this.GetProperty(GamePropertys.killCount);
  429. }
  430. set killCount(value:number){
  431. this.SetProperty(GamePropertys.killCount,value);
  432. }
  433. /**
  434. * 怒气值
  435. */
  436. get angerCount():number{
  437. return this.GetProperty(GamePropertys.angerCount);
  438. }
  439. set angerCount(value:number){
  440. this.SetProperty(GamePropertys.angerCount,value);
  441. }
  442. /**
  443. * 当前最大合成等级
  444. */
  445. get synthesisMaxWeaponId():number{
  446. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  447. }
  448. set synthesisMaxWeaponId(value:number){
  449. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  450. }
  451. /**
  452. * 上一次领取金币的时间
  453. */
  454. get lastGetGoldTime():number{
  455. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  456. }
  457. set lastGetGoldTime(value:number){
  458. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  459. }
  460. }