175 lines
5.7 KiB
C#
175 lines
5.7 KiB
C#
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<RoleBase> temp_RemoveUser = new Queue<RoleBase>();//需要移除的玩家列表
|
||
private Queue<Int32> temp_RemoveRequest = new Queue<Int32>();//请求数据列表
|
||
private Dictionary<long, RoleBase> dictAllRole = new Dictionary<long, RoleBase>();
|
||
private Dictionary<E_NODE_TYPE, Dictionary<long, RoleBase>> dictRoleList = new Dictionary<E_NODE_TYPE, Dictionary<long, RoleBase>>();
|
||
|
||
//key:职业+性别 比如:ss_w
|
||
private Dictionary<string, Queue<GameObject>> userPool = new Dictionary<string, Queue<GameObject>>();
|
||
|
||
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<long, RoleBase> roleList;
|
||
if (!dictRoleList.ContainsKey(role.RoleType))
|
||
roleList = dictRoleList[role.RoleType] = new Dictionary<long, RoleBase>();
|
||
else
|
||
roleList = dictRoleList[role.RoleType];
|
||
roleList[role.RoleID] = role;
|
||
}
|
||
}
|
||
public void RemoveRole(RoleBase role)
|
||
{
|
||
if (dictAllRole.ContainsKey(role.RoleID))
|
||
{
|
||
dictAllRole.Remove(role.RoleID);
|
||
|
||
Dictionary<long, RoleBase> roleList;
|
||
if (!dictRoleList.ContainsKey(role.RoleType))
|
||
roleList = dictRoleList[role.RoleType] = new Dictionary<long, RoleBase>();
|
||
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;
|
||
|
||
//组件
|
||
Rigidbody mRigidbody = this.gameObject.AddComponent<Rigidbody>();
|
||
mRigidbody.isKinematic = false;
|
||
mRigidbody.useGravity = true;
|
||
|
||
MainRole role = playergo.AddComponent<MainRole>();
|
||
role.Init(data);
|
||
|
||
//挂组件
|
||
SetRigibody(GamePlayEntry.MainPlayer.transform);
|
||
|
||
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<GameObject>(MapName);
|
||
#endif
|
||
}
|
||
else
|
||
{
|
||
int id = MapName.GetHashCode();
|
||
asset = PrefabManager.LoadPrefab<UnityEngine.Object>(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<Rigidbody>();
|
||
mRigidbody.isKinematic = false;
|
||
mRigidbody.useGravity = true;
|
||
}
|
||
|
||
}
|
||
}
|
||
|