using Axibug.Resources; using Axibug.Runtime; using Codice.CM.WorkspaceServer.DataStore; using Game.Config; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; namespace Game { public class RoleMgrComponent : GameComponent { private Queue temp_RemoveUser = new Queue();//需要移除的玩家列表 private Queue temp_RemoveRequest = new Queue();//请求数据列表 private Dictionary dictAllRole = new Dictionary(); private Dictionary> dictRoleList = new Dictionary>(); //key:职业+性别 比如:ss_w private Dictionary> userPool = new Dictionary>(); private float temp_CheckTime = 0; private float temp_RequestTime = 0; private long mRoleIdSeed = 0; private void Start() { //AppEntry.Event.Subscribe(UserMoveEventArgs.EventId, OnUserMoveEvent); //AppEntry.Event.Subscribe(UserPlayerSkillEventArgs.EventId, OnUserPlayerSkill); //AppEntry.Event.Subscribe(RoleDeadEventArgs.EventId, OnRoleDead); //AppEntry.Event.Subscribe(RoleReviveEventArgs.EventId, OnRoleRevive); //AppEntry.Event.Subscribe(RoleLevelupEventArgs.EventId, OnRoleLevelUp); //AppEntry.Event.Subscribe(AddRoleBufferEvent.EventId, AddBuffer); //AppEntry.Event.Subscribe(RemoveRoleBufferEvent.EventId, RemoveBuffer); } long GetNewRoleId() { return ++mRoleIdSeed; } #region 角色管理 public void AddRole(RoleBase role) { if (!dictAllRole.ContainsKey(role.RoleID)) { dictAllRole[role.RoleID] = role; Dictionary roleList; if (!dictRoleList.ContainsKey(role.RoleType)) roleList = dictRoleList[role.RoleType] = new Dictionary(); else roleList = dictRoleList[role.RoleType]; roleList[role.RoleID] = role; } } public void RemoveRole(RoleBase role) { if (dictAllRole.ContainsKey(role.RoleID)) { dictAllRole.Remove(role.RoleID); Dictionary roleList; if (!dictRoleList.ContainsKey(role.RoleType)) roleList = dictRoleList[role.RoleType] = new Dictionary(); else roleList = dictRoleList[role.RoleType]; roleList.Remove(role.RoleID); } } #endregion public MainRole CreateMainRole() { //数据 S_ROLE_SELF data = new S_ROLE_SELF(); data.innate.roleid = GetNewRoleId(); data.innate.job = 0;//TODO data.innate.nick = "";//昵称 data.innate.sex = 0;//性别 //经验 data.exp.exp = 0; data.exp.level = 1; //状态等 data.status.state = CharacterState.Living; data.status.pos = GamePlayEntry.Map.SpawnPos; data.status.mapid = 0;//TODO //生命魔法等 data.life.maxMP = 100; data.life.curMP = 100; data.life.maxMP = 100; data.life.curMP = 100; //货币 data.econ.gold = 0; string ModelName = "Warrior";//模型名称 //实例化角色 GameObject playergo = CloneRole(ModelName, GamePlayEntry.MainPlayer.transform); playergo.transform.localPosition = Vector3.zero; GamePlayEntry.MainPlayer.transform.position = data.status.pos; SetRigibody(playergo.transform); //挂组件 MainRole role = playergo.gameObject.AddComponent(); role.Init(data); AddRole(role); return role; } private GameObject CloneRole(string RoleName,Transform parent) { string rootPath = "Assets/GameAssets"; string MapName = $"Assets/GameAssets/Prefabs/Role/{RoleName}/{RoleName}.prefab"; string tmp = MapName.Remove(0, rootPath.Length + 1); int idx = tmp.LastIndexOf('/'); string bundleName = tmp.Substring(0, idx); UnityEngine.Object asset = null; if (AppEntry.Base.EditorResourceMode) { #if UNITY_EDITOR asset = AssetDatabase.LoadAssetAtPath(MapName); #endif } else { int id = MapName.GetHashCode(); asset = PrefabManager.LoadPrefab(bundleName.ToLower(), MapName, id, parent); } if (asset == null) { Debug.LogError($"asset加载失败,path={MapName}"); return null; } GameObject go = Instantiate(asset, parent) as GameObject; if (go == null) { Debug.LogError("LoadPrefabByEditor2. go == null. asset:" + asset); } return go; } void SetRigibody(Transform trans) { Rigidbody mRigidbody = trans.gameObject.AddComponent(); mRigidbody.isKinematic = false; mRigidbody.useGravity = true; mRigidbody.constraints = RigidbodyConstraints.FreezeRotation; } } }