using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; /* 设备管理者(弓和箭的种类) */ public class DeviceMgr { static DeviceMgr _ins = null; public static DeviceMgr ins { get { if (_ins == null) { _ins = new DeviceMgr(); } return _ins; } } Hashtable deviceConfigs = new Hashtable(); DeviceMgr() { CacheDeviceConfig(); CacheDeviceConfig(); CacheDeviceConfig(); //用户数据初始化 List deviceInfos = LoginMgr.myUserInfo.deviceList; if (deviceInfos.Count == 0) { // 加入初始设备 // int[] ids = {1000, 1001, 2000}; int[] ids = {1000, 2000}; foreach (var id in ids) { DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.id = id; if (id == 1000 || id == 2000) deviceInfo.inuse = true; deviceInfos.Add(deviceInfo); } LoginMgr.myUserInfo.Save(); } } void CacheDeviceConfig() where T : DeviceConfig { DeviceConfig deviceConfig = System.Activator.CreateInstance(); deviceConfigs.Add(deviceConfig.id, deviceConfig); } void CacheDeviceConfig(int id, DeviceConfig deviceConfig) { deviceConfigs.Add(id, deviceConfig); } public PropConfig GetDeviceConfig(int id) { return (PropConfig) deviceConfigs[id]; } public List GetMyDeviceList() { List list = LoginMgr.myUserInfo.deviceList; foreach (var deviceInfo in list) { deviceInfo.config = (DeviceConfig) deviceConfigs[deviceInfo.id]; } return list; } public void UseDevice(DeviceInfo deviceInfo) { List myDevices = GetMyDeviceList(); if (deviceInfo.config.type == 1 || deviceInfo.config.type == 2) { foreach (var myDevice in myDevices) { if (myDevice.config.type == deviceInfo.config.type) { myDevice.inuse = false; } } deviceInfo.inuse = true; LoginMgr.myUserInfo.Save(); } } public (DeviceInfo, DeviceInfo) GetCurrentBowArrowInfo() { List myDevices = GetMyDeviceList(); DeviceInfo bow = null; DeviceInfo arrow = null; foreach (var myDevice in myDevices) { if (myDevice.config.type == 1 && myDevice.inuse) { bow = myDevice; } else if (myDevice.config.type == 2 && myDevice.inuse) { arrow = myDevice; } } return (bow, arrow); } public int GetCurrentBowPound() { List myDevices = GetMyDeviceList(); foreach (var myDevice in myDevices) { if (myDevice.config.type == 1 && myDevice.inuse) { return ((DeviceBowConfig) myDevice.config).pound; } } return 0; } } public class DeviceInfo { public int id = 0; public bool inuse = false; [JsonIgnore] public DeviceConfig config = null; } public class DeviceConfig { public int id = 0; public int name = 0; public int detail = 0; public int difficulty = 0; public int model = 0; public int type = 0; } public class DeviceBowConfig : DeviceConfig { public int pound = 0; } public class DeviceBow1000 : DeviceBowConfig { public DeviceBow1000() { id = 1000; name = 201000; detail = 211000; difficulty = 3; model = 1; type = 1; pound = 18; } } public class DeviceBow1001 : DeviceBowConfig { public DeviceBow1001() { id = 1001; name = 201001; detail = 211001; difficulty = 5; model = 1; type = 1; pound = 25; } } public class DeviceArrow2000 : DeviceConfig { public DeviceArrow2000() { id = 2000; name = 201002; detail = 211002; difficulty = 3; model = 2; type = 2; } }