123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- 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);
- }
- }
|