GameModel.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { _decorator, Component, Node, Prefab, director } 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 { GamePropertys } from './GamePropertys';
  6. import { WeaponCell } from './weapons/WeaponCell';
  7. const { ccclass, property } = _decorator;
  8. export class GameModel extends DataModel{
  9. /**
  10. * 武器格子列表
  11. */
  12. private __weaponCells:WeaponCell[]=[];
  13. /**
  14. * 当前武器格子
  15. */
  16. private __currentWeaponCell:WeaponCell;
  17. constructor(){
  18. super();
  19. }
  20. /**
  21. * 设置当前武器ID
  22. */
  23. set currentWeaponId(value:number){
  24. this.__currentWeaponCell.weaponId=value;
  25. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.currentWeaponId);
  26. }
  27. get currentWeaponId():number{
  28. return this.__currentWeaponCell.weaponId;
  29. }
  30. /**
  31. * 添加一个武器到武器格子
  32. * @param cellId
  33. * @param weaponID
  34. */
  35. public AddWeapon(cellId:number,weaponID:number):void{
  36. for (let index = 0; index < this.__weaponCells.length; index++) {
  37. const element = this.__weaponCells[index];
  38. if(element.cellId==cellId){
  39. element.weaponId=weaponID;
  40. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  41. return;
  42. }
  43. }
  44. }
  45. /**
  46. * 删除武器
  47. * @param cellId
  48. */
  49. public RemoveWeapon(cellId:number):void{
  50. for (let index = 0; index < this.__weaponCells.length; index++) {
  51. const element = this.__weaponCells[index];
  52. if(element.cellId==cellId){
  53. element.weaponId=-1;
  54. this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
  55. return;
  56. }
  57. }
  58. }
  59. /**
  60. * 卸下武器
  61. */
  62. public UnequipWeapon(cellId:number):void{
  63. let id:number=this.__currentWeaponCell.weaponId;
  64. this.currentWeaponId=-1;
  65. this.AddWeapon(cellId,id);
  66. }
  67. /**
  68. * 装配武器
  69. * @param weaponId
  70. */
  71. public EquipWeapon(sourceCellId:number,weaponId:number):void{
  72. let cell:WeaponCell=this.FindCell(sourceCellId);
  73. if(cell==null){
  74. throw new Error("找不到武器槽:"+sourceCellId);
  75. }
  76. this.currentWeaponId=weaponId;
  77. }
  78. /**
  79. * 通过武器槽ID查找
  80. * @param id
  81. */
  82. public FindCell(id:number):WeaponCell{
  83. for (let index = 0; index < this.__weaponCells.length; index++) {
  84. const element = this.__weaponCells[index];
  85. if(element.cellId==id){
  86. return element;
  87. }
  88. }
  89. return null;
  90. }
  91. /**
  92. * 武器格子列表
  93. */
  94. public get weaponCells():WeaponCell[]{
  95. return this.weaponCells;
  96. }
  97. /**
  98. * 设置默认属性
  99. */
  100. SetDefaultPropertys():void{
  101. //默认武器
  102. this.__currentWeaponCell=new WeaponCell();
  103. this.__currentWeaponCell.cellId=-1;
  104. this.__currentWeaponCell.lastOutputTime=director.getCurrentTime();
  105. this.__currentWeaponCell.weaponId=1;
  106. this.currentLevel=1;
  107. this.currentFenceId=1;
  108. this.gold=0;
  109. this.diamond=0;
  110. //12个默认格子
  111. let weaponCell:WeaponCell;
  112. for (let index = 0; index < 12; index++) {
  113. weaponCell=new WeaponCell();
  114. weaponCell.cellId=index;
  115. weaponCell.weaponId=-1;
  116. weaponCell.lastOutputTime=0;
  117. this.__weaponCells.push(weaponCell);
  118. }
  119. //保存到本地
  120. this.SaveToLoacl();
  121. }
  122. protected OnReadByLocal(data:any):void{
  123. //当前武器格
  124. this.__currentWeaponCell=new WeaponCell();
  125. for (const key in data.currentWeaponCell) {
  126. if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key)&&Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) {
  127. const element = data.currentWeaponCell[key];
  128. this.__currentWeaponCell[key]=element;
  129. }
  130. }
  131. //武器格子
  132. let weaponCells:any[]=data.weaponCells;
  133. this.__weaponCells=[];
  134. let weaponCell:WeaponCell;
  135. if(weaponCells!=null){
  136. weaponCells.forEach(element => {
  137. weaponCell=new WeaponCell();
  138. for (const key in element) {
  139. if (Object.prototype.hasOwnProperty.call(element, key)&&Object.prototype.hasOwnProperty.call(weaponCell, key)) {
  140. const item = element[key];
  141. weaponCell[key]=item;
  142. this.__weaponCells.push(weaponCell);
  143. }
  144. }
  145. });
  146. }
  147. }
  148. protected OnSaveToLocal(data:any):void{
  149. data.weaponCells=this.__weaponCells;
  150. data.currentWeaponCell=this.__currentWeaponCell;
  151. }
  152. private static instance:GameModel;
  153. public static get single():GameModel{
  154. if(this.instance==null){
  155. this.instance=new GameModel();
  156. }
  157. return this.instance;
  158. }
  159. /**
  160. * 当前关卡
  161. */
  162. get currentLevel():number{
  163. return this.GetProperty(GamePropertys.currentLevel);
  164. }
  165. set currentLevel(value:number){
  166. this.SetProperty(GamePropertys.currentLevel,value);
  167. }
  168. /**
  169. * 当前栅栏ID
  170. */
  171. get currentFenceId():number{
  172. return this.GetProperty(GamePropertys.currentFenceId);
  173. }
  174. set currentFenceId(value:number){
  175. this.SetProperty(GamePropertys.currentFenceId,value);
  176. }
  177. /**
  178. * 最大关卡数
  179. */
  180. get maxLevel():number{
  181. return this.GetProperty(GamePropertys.maxLevel);
  182. }
  183. set maxLevel(value:number){
  184. this.SetProperty(GamePropertys.maxLevel,value);
  185. }
  186. /**
  187. * 金币
  188. */
  189. get gold():number{
  190. return this.GetProperty(GamePropertys.gold);
  191. }
  192. set gold(value:number){
  193. this.SetProperty(GamePropertys.gold,value);
  194. }
  195. /**
  196. * 积分
  197. */
  198. get integral():number{
  199. return this.GetProperty(GamePropertys.integral);
  200. }
  201. set integral(value:number){
  202. this.SetProperty(GamePropertys.integral,value);
  203. }
  204. /**
  205. * 钻石
  206. */
  207. get diamond():number{
  208. return this.GetProperty(GamePropertys.diamond);
  209. }
  210. set diamond(value:number){
  211. this.SetProperty(GamePropertys.diamond,value);
  212. }
  213. /**
  214. * 击杀数量
  215. */
  216. get killCount():number{
  217. return this.GetProperty(GamePropertys.killCount);
  218. }
  219. set killCount(value:number){
  220. this.SetProperty(GamePropertys.killCount,value);
  221. }
  222. /**
  223. * 怒气值
  224. */
  225. get angerCount():number{
  226. return this.GetProperty(GamePropertys.angerCount);
  227. }
  228. set angerCount(value:number){
  229. this.SetProperty(GamePropertys.angerCount,value);
  230. }
  231. }