GameModel.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. let weaponId:number=this.CurrentQuickBuyWeaponId;
  200. //已购买的次数
  201. let buyCount:number=this.GetBuyCount(weaponId);
  202. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  203. //价格
  204. let price:number=weaponConfig.consumeGold;
  205. //系数
  206. let coefficient:number=weaponConfig.coefficient;
  207. if(buyCount<=0){
  208. buyCount=1;
  209. }
  210. return Math.floor(price*Math.pow(coefficient,buyCount));
  211. }
  212. /**
  213. * 已购买次数
  214. * @param weaponId
  215. */
  216. public GetBuyCount(weaponId:number):number{
  217. let buyCount:number=0;
  218. if(this.__buyHistory.has(weaponId)){
  219. buyCount=this.__buyHistory.get(weaponId);
  220. }
  221. return buyCount;
  222. }
  223. /**
  224. * 购买武器
  225. */
  226. public QuickBuy():void{
  227. let price:number=this.GetQuickBuyPrice();
  228. if(this.gold<price){
  229. NoticeManager.ShowPrompt("金币不足,不能购买武器!");
  230. return;
  231. }
  232. //找到空槽位
  233. let weaponCell:WeaponCell=this.FindWeaponEmptyCell();
  234. if(weaponCell==null){
  235. NoticeManager.ShowPrompt("没有空槽位了!");
  236. return;
  237. }
  238. //扣钱
  239. let currentGold:number=this.gold;
  240. currentGold-=price;
  241. if(currentGold<0){
  242. currentGold=0;
  243. }
  244. this.gold=currentGold;
  245. //记录购买次数
  246. let weaponId:number=this.CurrentQuickBuyWeaponId;
  247. let weaponConfig:any=GameConfigManager.GetWeaponConfig(weaponId);
  248. let buyCount:number=this.GetBuyCount(weaponId);
  249. buyCount++;
  250. if(buyCount>weaponConfig.frequencyLimit){
  251. buyCount=weaponConfig.frequencyLimit;
  252. }
  253. this.__buyHistory.set(weaponId,buyCount);
  254. //发货
  255. this.AddWeapon(weaponCell.cellId,this.CurrentQuickBuyWeaponId);
  256. }
  257. /**
  258. * 通过武器槽ID查找
  259. * @param id
  260. */
  261. public FindCell(id:number):WeaponCell{
  262. for (let index = 0; index < this.__weaponCells.length; index++) {
  263. const element = this.__weaponCells[index];
  264. if(element.cellId==id){
  265. return element;
  266. }
  267. }
  268. return null;
  269. }
  270. /**
  271. * 武器格子列表
  272. */
  273. public get weaponCells():WeaponCell[]{
  274. return this.__weaponCells;
  275. }
  276. /**
  277. * 设置默认属性
  278. */
  279. SetDefaultPropertys():void{
  280. //默认武器
  281. this.__currentWeaponCell=new WeaponCell();
  282. this.__currentWeaponCell.cellId=-1;
  283. this.__currentWeaponCell.lastOutputTime=director.getCurrentTime();
  284. this.__currentWeaponCell.weaponId=10101;
  285. //默认关卡
  286. this.currentLevel=1;
  287. //默认栅栏
  288. this.currentFenceId=30001;
  289. //默认最大已合成枪ID
  290. this.synthesisMaxWeaponId=10101;
  291. this.gold=0;
  292. this.diamond=0;
  293. //12个默认格子
  294. let weaponCell:WeaponCell;
  295. for (let index = 0; index < 12; index++) {
  296. weaponCell=new WeaponCell();
  297. weaponCell.cellId=index;
  298. weaponCell.weaponId=10101;
  299. weaponCell.lastOutputTime=0;
  300. this.__weaponCells.push(weaponCell);
  301. }
  302. //保存到本地
  303. this.SaveToLoacl();
  304. }
  305. protected OnReadByLocal(data:any):void{
  306. //当前武器格
  307. this.__currentWeaponCell=new WeaponCell();
  308. for (const key in data.currentWeaponCell) {
  309. if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key)&&Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) {
  310. const element = data.currentWeaponCell[key];
  311. this.__currentWeaponCell[key]=element;
  312. }
  313. }
  314. //武器格子
  315. let weaponCells:any[]=data.weaponCells;
  316. this.__weaponCells=[];
  317. let weaponCell:WeaponCell;
  318. if(weaponCells!=null){
  319. weaponCells.forEach(element => {
  320. weaponCell=new WeaponCell();
  321. for (const key in element) {
  322. if (Object.prototype.hasOwnProperty.call(element, key)&&Object.prototype.hasOwnProperty.call(weaponCell, key)) {
  323. const item = element[key];
  324. weaponCell[key]=item;
  325. this.__weaponCells.push(weaponCell);
  326. }
  327. }
  328. });
  329. }
  330. }
  331. protected OnSaveToLocal(data:any):void{
  332. data.weaponCells=this.__weaponCells;
  333. data.currentWeaponCell=this.__currentWeaponCell;
  334. }
  335. private static instance:GameModel;
  336. public static get single():GameModel{
  337. if(this.instance==null){
  338. this.instance=new GameModel();
  339. }
  340. return this.instance;
  341. }
  342. /**
  343. * 当前关卡
  344. */
  345. get currentLevel():number{
  346. return this.GetProperty(GamePropertys.currentLevel);
  347. }
  348. set currentLevel(value:number){
  349. this.SetProperty(GamePropertys.currentLevel,value);
  350. }
  351. /**
  352. * 当前栅栏ID
  353. */
  354. get currentFenceId():number{
  355. return this.GetProperty(GamePropertys.currentFenceId);
  356. }
  357. set currentFenceId(value:number){
  358. this.SetProperty(GamePropertys.currentFenceId,value);
  359. }
  360. /**
  361. * 金币
  362. */
  363. get gold():number{
  364. return this.GetProperty(GamePropertys.gold);
  365. }
  366. set gold(value:number){
  367. this.SetProperty(GamePropertys.gold,value);
  368. }
  369. /**
  370. * 积分
  371. */
  372. get integral():number{
  373. return this.GetProperty(GamePropertys.integral);
  374. }
  375. set integral(value:number){
  376. this.SetProperty(GamePropertys.integral,value);
  377. }
  378. /**
  379. * 钻石
  380. */
  381. get diamond():number{
  382. return this.GetProperty(GamePropertys.diamond);
  383. }
  384. set diamond(value:number){
  385. this.SetProperty(GamePropertys.diamond,value);
  386. }
  387. /**
  388. * 击杀数量
  389. */
  390. get killCount():number{
  391. return this.GetProperty(GamePropertys.killCount);
  392. }
  393. set killCount(value:number){
  394. this.SetProperty(GamePropertys.killCount,value);
  395. }
  396. /**
  397. * 怒气值
  398. */
  399. get angerCount():number{
  400. return this.GetProperty(GamePropertys.angerCount);
  401. }
  402. set angerCount(value:number){
  403. this.SetProperty(GamePropertys.angerCount,value);
  404. }
  405. /**
  406. * 当前最大合成等级
  407. */
  408. get synthesisMaxWeaponId():number{
  409. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  410. }
  411. set synthesisMaxWeaponId(value:number){
  412. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  413. }
  414. /**
  415. * 上一次领取金币的时间
  416. */
  417. get lastGetGoldTime():number{
  418. return this.GetProperty(GamePropertys.synthesisMaxWeaponId);
  419. }
  420. set lastGetGoldTime(value:number){
  421. this.SetProperty(GamePropertys.synthesisMaxWeaponId,value);
  422. }
  423. }