AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/RoleMgrComponent.cs
2023-01-09 23:13:17 +08:00

284 lines
8.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 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 int 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);
}
int GetNewRoleId()
{
return ++mRoleIdSeed;
}
#region
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;
}
}
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);
}
}
public RoleBase FindRole(int RoleID)
{
if (dictAllRole.ContainsKey(RoleID))
return dictAllRole[RoleID];
return null;
}
public RoleBase FindRole(E_NODE_TYPE type , int RoleID)
{
if (dictRoleList.ContainsKey(type) && dictRoleList[type].ContainsKey(RoleID))
return dictRoleList[type][RoleID];
return null;
}
#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.maxHP = 100;
data.life.curHP = 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.position = data.status.pos;
SetRigibody(playergo.transform);
//挂组件
MainRole role = playergo.gameObject.AddComponent<MainRole>();
role.Init(data);
//攻击盒子
GameObject AttackBoxGo = CloneAttackBox(role.BridgeTransfrom);
AttackBoxGo.transform.localPosition = Vector3.zero;
AttackBoxGo.transform.localEulerAngles = Vector3.zero;
//攻击盒子动画机
role.SetAttackBoxAnime(AttackBoxGo.GetComponent<Animator>());
AddRole(role);
return role;
}
public MonsterRole CreateMonster(Vector3 SpawnPos)
{
//数据
S_ROLE_MONSTER data = new S_ROLE_MONSTER();
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 = SpawnPos;
data.status.mapid = 0;//TODO
//生命魔法等
data.life.maxHP = 100;
data.life.curHP = 100;
data.life.maxMP = 100;
data.life.curMP = 100;
string ModelName = "SlimeGreen";//模型名称
//实例化角色
GameObject playergo = CloneRole(ModelName, GamePlayEntry.RoleMgr.transform);
playergo.transform.position = data.status.pos;
SetRigibody(playergo.transform);
//挂组件
MonsterRole role = playergo.gameObject.AddComponent<MonsterRole>();
role.Init(data);
//攻击盒子
GameObject AttackBoxGo = CloneAttackBox(role.BridgeTransfrom);
AttackBoxGo.transform.localPosition = Vector3.zero;
AttackBoxGo.transform.localEulerAngles = Vector3.zero;
//攻击盒子动画机
role.SetAttackBoxAnime(AttackBoxGo.GetComponent<Animator>());
AddRole(role);
return role;
}
public void DestroyRole(RoleBase role)
{
role.enabled = false;
RemoveRole(role);
Destroy(role.gameObject);
}
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;
}
private GameObject CloneAttackBox(Transform parent)
{
string rootPath = "Assets/GameAssets";
string MapName = $"Assets/GameAssets/Prefabs/AttackBox/NormalAttack.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;
mRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
}
}