import { _decorator, Component, Node, Prefab, director } from 'cc'; import { EventDispatcher } from '../../engines/events/EventDispatcher'; import { DataModel } from '../../engines/models/DataModel'; import { DataModelEventType } from '../../engines/models/DataModelEventType'; import { GamePropertys } from './GamePropertys'; import { WeaponCell } from './weapons/WeaponCell'; const { ccclass, property } = _decorator; export class GameModel extends DataModel{ /** * 武器格子列表 */ private __weaponCells:WeaponCell[]=[]; /** * 当前武器格子 */ private __currentWeaponCell:WeaponCell; constructor(){ super(); } /** * 设置当前武器ID */ set currentWeaponId(value:number){ this.__currentWeaponCell.weaponId=value; this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.currentWeaponId); } get currentWeaponId():number{ return this.__currentWeaponCell.weaponId; } /** * 添加一个武器到武器格子 * @param cellId * @param weaponID */ public AddWeapon(cellId:number,weaponID:number):void{ for (let index = 0; index < this.__weaponCells.length; index++) { const element = this.__weaponCells[index]; if(element.cellId==cellId){ element.weaponId=weaponID; this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell); return; } } } /** * 删除武器 * @param cellId */ public RemoveWeapon(cellId:number):void{ for (let index = 0; index < this.__weaponCells.length; index++) { const element = this.__weaponCells[index]; if(element.cellId==cellId){ element.weaponId=-1; this.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell); return; } } } /** * 卸下武器 */ public UnequipWeapon(cellId:number):void{ let id:number=this.__currentWeaponCell.weaponId; this.currentWeaponId=-1; this.AddWeapon(cellId,id); } /** * 装配武器 * @param weaponId */ public EquipWeapon(sourceCellId:number,weaponId:number):void{ let cell:WeaponCell=this.FindCell(sourceCellId); if(cell==null){ throw new Error("找不到武器槽:"+sourceCellId); } this.currentWeaponId=weaponId; } /** * 通过武器槽ID查找 * @param id */ public FindCell(id:number):WeaponCell{ for (let index = 0; index < this.__weaponCells.length; index++) { const element = this.__weaponCells[index]; if(element.cellId==id){ return element; } } return null; } /** * 武器格子列表 */ public get weaponCells():WeaponCell[]{ return this.weaponCells; } /** * 设置默认属性 */ SetDefaultPropertys():void{ //默认武器 this.__currentWeaponCell=new WeaponCell(); this.__currentWeaponCell.cellId=-1; this.__currentWeaponCell.lastOutputTime=director.getCurrentTime(); this.__currentWeaponCell.weaponId=1; this.currentLevel=1; this.currentFenceId=1; this.gold=0; this.diamond=0; //12个默认格子 let weaponCell:WeaponCell; for (let index = 0; index < 12; index++) { weaponCell=new WeaponCell(); weaponCell.cellId=index; weaponCell.weaponId=-1; weaponCell.lastOutputTime=0; this.__weaponCells.push(weaponCell); } //保存到本地 this.SaveToLoacl(); } protected OnReadByLocal(data:any):void{ //当前武器格 this.__currentWeaponCell=new WeaponCell(); for (const key in data.currentWeaponCell) { if (Object.prototype.hasOwnProperty.call(data.currentWeaponCell, key)&&Object.prototype.hasOwnProperty.call(this.__currentWeaponCell, key)) { const element = data.currentWeaponCell[key]; this.__currentWeaponCell[key]=element; } } //武器格子 let weaponCells:any[]=data.weaponCells; this.__weaponCells=[]; let weaponCell:WeaponCell; if(weaponCells!=null){ weaponCells.forEach(element => { weaponCell=new WeaponCell(); for (const key in element) { if (Object.prototype.hasOwnProperty.call(element, key)&&Object.prototype.hasOwnProperty.call(weaponCell, key)) { const item = element[key]; weaponCell[key]=item; this.__weaponCells.push(weaponCell); } } }); } } protected OnSaveToLocal(data:any):void{ data.weaponCells=this.__weaponCells; data.currentWeaponCell=this.__currentWeaponCell; } private static instance:GameModel; public static get single():GameModel{ if(this.instance==null){ this.instance=new GameModel(); } return this.instance; } /** * 当前关卡 */ get currentLevel():number{ return this.GetProperty(GamePropertys.currentLevel); } set currentLevel(value:number){ this.SetProperty(GamePropertys.currentLevel,value); } /** * 当前栅栏ID */ get currentFenceId():number{ return this.GetProperty(GamePropertys.currentFenceId); } set currentFenceId(value:number){ this.SetProperty(GamePropertys.currentFenceId,value); } /** * 最大关卡数 */ get maxLevel():number{ return this.GetProperty(GamePropertys.maxLevel); } set maxLevel(value:number){ this.SetProperty(GamePropertys.maxLevel,value); } /** * 金币 */ get gold():number{ return this.GetProperty(GamePropertys.gold); } set gold(value:number){ this.SetProperty(GamePropertys.gold,value); } /** * 积分 */ get integral():number{ return this.GetProperty(GamePropertys.integral); } set integral(value:number){ this.SetProperty(GamePropertys.integral,value); } /** * 钻石 */ get diamond():number{ return this.GetProperty(GamePropertys.diamond); } set diamond(value:number){ this.SetProperty(GamePropertys.diamond,value); } /** * 击杀数量 */ get killCount():number{ return this.GetProperty(GamePropertys.killCount); } set killCount(value:number){ this.SetProperty(GamePropertys.killCount,value); } /** * 怒气值 */ get angerCount():number{ return this.GetProperty(GamePropertys.angerCount); } set angerCount(value:number){ this.SetProperty(GamePropertys.angerCount,value); } }