GameModel.ts 15 KB

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