using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; /* 游戏道具管理者 */ public class PropMgr { static PropMgr _ins = null; public static PropMgr ins { get { if (_ins == null) { _ins = new PropMgr(); } return _ins; } } Hashtable propConfigs = new Hashtable(); List propConfigIds = new List(); PropMgr() { for (int i = 1; i <= 5; i++) { PropScaleAim propScaleAim = new PropScaleAim(10, i); CachePropConfig(propScaleAim.id, propScaleAim); } for (int i = 1; i <= 5; i++) { PropScaleShoot propScaleShoot = new PropScaleShoot(20, i); CachePropConfig(propScaleShoot.id, propScaleShoot); } } void CachePropConfig(int id, PropConfig propConfig) { propConfigs.Add(id, propConfig); propConfigIds.Add(id); } public PropConfig GetPropConfig(int id) { return (PropConfig) propConfigs[id]; } public List ListForShop() { List list = new List(); foreach (int id in propConfigIds) { PropConfig propConfig = (PropConfig) propConfigs[id]; if (propConfig.inShop) { list.Add(propConfig); } } return list; } public List ListForBag() { List list = LoginMgr.myUserInfo.bagList; if (CommonConfig.SpecialVersion1) list = GetDebugLocalBagList(); foreach (var propInfo in list) { propInfo.config = (PropConfig) propConfigs[propInfo.id]; } return list; } public List ListForEquipped() { List list = new List(); List myProps = LoginMgr.myUserInfo.bagList; if (CommonConfig.SpecialVersion1) myProps = GetDebugLocalBagList(); foreach (var propInfo in myProps) { if (propInfo.inuse) { propInfo.config = (PropConfig) propConfigs[propInfo.id]; list.Add(propInfo); } } return list; } //商店购买接口 public bool isSoldOut(PropConfig propConfig) { if (propConfig.type == 1 || propConfig.type == 2) { List myProps = LoginMgr.myUserInfo.bagList; if (CommonConfig.SpecialVersion1) myProps = GetDebugLocalBagList(); foreach (var myProp in myProps) { if (myProp.id == propConfig.id && myProp.count > 0) return true; } } return false; } public bool buyProp(PropConfig propConfig) { if (LoginMgr.myUserInfo.diamond >= propConfig.diamond) { LoginMgr.myUserInfo.diamond -= propConfig.diamond; PropInfo propInfo = new PropInfo(); propInfo.id = propConfig.id; propInfo.count = 1; LoginMgr.myUserInfo.bagList.Add(propInfo); LoginMgr.myUserInfo.Save(); return true; } return false; } public bool useProp(PropInfo propInfo) { if (!propInfo.inuse) { if (propInfo.config.type == 1 || propInfo.config.type == 2) { List equippeds = ListForEquipped(); foreach (var equipped in equippeds) { if (equipped.config.type == propInfo.config.type) { equipped.inuse = false; } } } } propInfo.inuse = !propInfo.inuse; if (CommonConfig.SpecialVersion1) { SaveDebugLocalBagList(); return propInfo.inuse; } LoginMgr.myUserInfo.Save(); return propInfo.inuse; } private List _debugLocalBagList; private int _debugLocalBagList_uid = -1; private List GetDebugLocalBagList() { if (_debugLocalBagList_uid != LoginMgr.myUserInfo.id) { _debugLocalBagList_uid = LoginMgr.myUserInfo.id; _debugLocalBagList = null; } if (_debugLocalBagList == null) { try { string str = PlayerPrefs.GetString("sv1_baglist," + LoginMgr.myUserInfo.id, null); _debugLocalBagList = JsonConvert.DeserializeObject>(str); } catch (System.Exception) {} if (_debugLocalBagList == null) { _debugLocalBagList = new List(); for (int i = 2; i <= 5; i++) { PropInfo propInfo = new PropInfo(); propInfo.id = 10 + i; propInfo.count = 1; _debugLocalBagList.Add(propInfo); if (propInfo.id == 12) { propInfo.inuse = true; } } for (int i = 2; i <= 5; i++) { PropInfo propInfo = new PropInfo(); propInfo.id = 20 + i; propInfo.count = 1; _debugLocalBagList.Add(propInfo); if (propInfo.id == 25) { propInfo.inuse = true; } } } } return _debugLocalBagList; } private void SaveDebugLocalBagList() { if (_debugLocalBagList != null) { string str = JsonConvert.SerializeObject(_debugLocalBagList); PlayerPrefs.SetString("sv1_baglist," + LoginMgr.myUserInfo.id, str); } } } public class PropInfo { public int id = 0; public int count = 1; [JsonIgnore] public PropConfig config = null; public bool inuse = false; } public class PropConfig { public int id = 0; public int type = 0; public int iconID = 0; public string[] name = {null}; public string[] detail = {null}; public int difficulty = -1; public int diamond = 0; public bool inShop = true; } public class PropScaleAim : PropConfig { public int scaleValue = 0; public PropScaleAim(int baseID, int scaleValue) { this.scaleValue = scaleValue; if (this.scaleValue == 1) { this.inShop = false; } id = baseID + scaleValue; type = 1; iconID = 1000; name = new string[]{"101000", scaleValue.ToString()}; detail = new string[]{"111000", scaleValue.ToString()}; diamond = scaleValue * 20; } } public class PropScaleShoot : PropConfig { public int scaleValue = 0; public PropScaleShoot(int baseID, int scaleValue) { this.scaleValue = scaleValue; if (this.scaleValue == 1) { this.inShop = false; } id = baseID + scaleValue; type = 2; iconID = 1001; name = new string[]{"101001", scaleValue.ToString()}; detail = new string[]{"111001", scaleValue.ToString()}; diamond = scaleValue * 20; } }