Files
test/Assets/PerfectWorld/Scripts/Managers/EC_ManPlayer.cs
T
Tungdv 0252370c2b Merge branch 'develop' into feature/elseplayer
# Conflicts:
#	Assets/NetworkLib/Debug/netstandard2.1/CSNetwork.dll
#	Assets/PerfectWorld/Scripts/Network/EC_ManMessageMono.cs
#	Assets/Scripts/CECHostPlayer.cs
#	Assets/Scripts/GameController.cs
2025-09-13 16:36:09 +07:00

298 lines
12 KiB
C#

using BrewMonster;
using BrewMonster.Network;
using CSNetwork;
using CSNetwork.GPDataType;
using CSNetwork.Protocols.RPCData;
using PerfectWorld.Scripts.Player;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace PerfectWorld.Scripts.Managers
{
namespace BrewMonster.Managers
{
[Serializable]
public class EC_ManPlayer : IMsgHandler
{
Dictionary<int, int> m_UkPlayerTab = new Dictionary<int, int>();
Dictionary<int, EC_ElsePlayer> m_PlayerTab = new Dictionary<int, EC_ElsePlayer>();
private readonly object m_csPlayerTab = new object();
public int HandlerId => (int)MANAGER_INDEX.MAN_PLAYER;
public bool ProcessMessage(ECMSG Msg)
{
Debug.LogWarning("HoangDev : EC_ManPlayerProcessMessage");
if (Msg.iSubID == 0)
{
Debug.LogWarning("HoangDev : EC_ManPlayerEC_ManPlayerEC_ManPlayer");
GameController.Instance.GetHostPlayer().ProcessMessage(Msg);
}
else if (Msg.iSubID < 0)
{
switch ((int)Msg.dwMsg)
{
case int value when value == EC_MsgDef.MSG_PM_PLAYERINFO:
{
OnMsgPlayerInfo(Msg);
break;
}
case int value when value == EC_MsgDef.MSG_PM_PLAYERMOVE:
{
OnMsgPlayerMove(Msg);
break;
}
case int value when value == EC_MsgDef.MSG_PM_PLAYERSTOPMOVE:
{
OnMsgPlayerStopMove(Msg);
break;
}
}
}
else
{
}
return true;
}
public void OnMsgPlayerInfo(ECMSG Msg)
{
Debug.LogWarning("OnMsgPlayerInfo ");
int iHostID = Convert.ToInt32(Msg.dwParam3);
int lenghtByte = Marshal.SizeOf<int>();
byte[] byteArray = new byte[lenghtByte];
byte[] data = (byte[])Msg.dwParam1;
for (int i = 0; i < lenghtByte; i++)
{
byteArray[i] = data[i];
}
int cid = BitConverter.ToInt32(byteArray);
int commandID = Convert.ToInt32(Msg.dwParam2);
switch (commandID)
{
case CommandID.PLAYER_INFO_1:
case CommandID.PLAYER_ENTER_WORLD:
case CommandID.PLAYER_ENTER_SLICE:
{
if (cid != iHostID)
{
info_player_1 info_Player_1 = GPDataTypeHelper.FromBytes<info_player_1>((byte[])Msg.dwParam1);
RoleInfo roleInfo = (RoleInfo)Msg.dwParam4;
ElsePlayerEnter(info_Player_1, commandID, roleInfo);
GameController.Instance.Log("ElsePlayer has join");
}
break;
}
case CommandID.SELF_INFO_1:
cmd_self_info_1 info = GPDataTypeHelper.FromBytes<cmd_self_info_1>((byte[])Msg.dwParam1);
HostPlayerInfo1(info);
break;
}
}
public void OnMsgPlayerMove(ECMSG Msg)
{
int iHostID = Convert.ToInt32(Msg.dwParam3);
cmd_object_move pCmd = GPDataTypeHelper.FromBytes<cmd_object_move>((byte[])Msg.dwParam1);
if (pCmd.use_time == 0)
return;
if (pCmd.id != iHostID)
{
EC_ElsePlayer pPlayer = SeekOutElsePlayer(pCmd.id);
if (pPlayer)
{
pPlayer.MoveTo(pCmd);
}
}
}
public bool HostPlayerInfo1(cmd_self_info_1 info)
{
bool isDoneWorldRender = false;
bool isDoneNPCRender = false;
Debug.Log("HostPlayerInfo1HostPlayerInfo1");
Action actLoadChar = () =>
{
if(!isDoneNPCRender || !isDoneWorldRender)
{
return;
}
GameController.Instance.InitCharacter(info);
};
string nameScene = "NPCRender";
/* UnityGameSession.Instance.LoadScene(nameScene, LoadSceneMode.Single, (value) =>
{
isDoneNPCRender = value;
actLoadChar?.Invoke();
});
nameScene = "WorldRender";
UnityGameSession.Instance.LoadScene(nameScene, LoadSceneMode.Additive, (value) =>
{
isDoneWorldRender = value;
actLoadChar?.Invoke();
});*/
UnityGameSession.Instance.LoadScene("HoangTest", LoadSceneMode.Single, (value) =>
{
isDoneWorldRender = value;
actLoadChar?.Invoke();
});
return true;
}
public EC_ElsePlayer ElsePlayerEnter(info_player_1 info, int iCmd, RoleInfo roleInfo)
{
// If this player's id is in unknown table, remove it because this player
// won't be unknown anymore
if (m_UkPlayerTab.TryGetValue(info.cid, out int value))
{
if (value != 0) // Pair.second != 0
{
m_UkPlayerTab.Remove(info.cid);
}
}
int iAppearFlag = (iCmd == (int)CommandID.PLAYER_ENTER_WORLD) ?
(int)PlayerAppearFlag.APPEAR_ENTERWORLD : (int)PlayerAppearFlag.APPEAR_RUNINTOVIEW;
// Has player been in active player table ?
EC_ElsePlayer pPlayer = GetElsePlayer(info.cid);
if (pPlayer)
{
// This player has existed in player table, call special initial function
// TODO: fix after pPlayer init
pPlayer.Init(roleInfo, info);
return pPlayer;
}
// Create a new player
if (!(pPlayer = CreateElsePlayer(roleInfo, info, iAppearFlag)))
{
return null;
}
lock (m_csPlayerTab)
{
if (m_PlayerTab.ContainsKey(info.cid))
{
m_PlayerTab[info.cid] = pPlayer;
}
else
{
m_PlayerTab.Add(info.cid ,pPlayer);
}
}
return pPlayer;
}
private EC_ElsePlayer CreateElsePlayer(RoleInfo roleInfo, info_player_1 info, int iAppearFlag)
{
var ob = GameController.Instance.InitCharacter(info);
EC_ElsePlayer elsePlayer = ob.AddComponent<EC_ElsePlayer>();
elsePlayer.GetComponent<CharacterCtrl>().enabled = false;
//init
elsePlayer.Init(roleInfo, info);
return elsePlayer;
}
public EC_ElsePlayer GetElsePlayer(int cid, long dwBornStamp = 0)
{
lock (m_csPlayerTab)
{
if (!m_PlayerTab.TryGetValue(cid, out var player))
return null;
if (dwBornStamp != 0)
{
//TO DO: fix after GetBornStamp() is create in code
//if (player.GetBornStamp() != dwBornStamp)
return null;
}
return player;
}
}
private EC_ElsePlayer SeekOutElsePlayer(int cid)
{
if (!m_PlayerTab.TryGetValue(cid, out var player))
{
// Couldn't find this else player, put it into unknown player table
m_UkPlayerTab[cid] = cid;
return null;
}
return player;
}
public bool OnMsgPlayerStopMove(ECMSG Msg)
{
cmd_object_stop_move pCmd = GPDataTypeHelper.FromBytes<cmd_object_stop_move>((byte[])Msg.dwParam1);
EC_ElsePlayer pPlayer = SeekOutElsePlayer(pCmd.id);
if (pPlayer)
pPlayer.StopMoveTo(pCmd);
return true;
}
//private cmd_object_move ConvertToStruct(byte[] bytes)
//{
// if (bytes.Length < Marshal.SizeOf<cmd_object_move>())
// {
// return default;
// }
// cmd_object_move result = new cmd_object_move();
// int preLenghtData = 0;
// int lenghtDataType = Marshal.SizeOf<int>();
// byte[] arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.id = BitConverter.ToInt32(arrByteData);
// preLenghtData += lenghtDataType;
// lenghtDataType = Marshal.SizeOf<float>();
// arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.dest_X = BitConverter.ToSingle(arrByteData);
// preLenghtData += lenghtDataType;
// lenghtDataType = Marshal.SizeOf<float>();
// arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.dest_Y = BitConverter.ToSingle(arrByteData);
// preLenghtData += lenghtDataType;
// lenghtDataType = Marshal.SizeOf<float>();
// arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.dest_Z = BitConverter.ToSingle(arrByteData);
// preLenghtData += lenghtDataType;
// lenghtDataType = Marshal.SizeOf<ushort>();
// arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.use_time = BitConverter.ToUInt16(arrByteData);
// preLenghtData += lenghtDataType;
// lenghtDataType = Marshal.SizeOf<short>();
// arrByteData = GetBytes(bytes, lenghtDataType, preLenghtData);
// result.sSpeed = BitConverter.ToInt16(arrByteData);
// preLenghtData += lenghtDataType;
// result.move_mode = bytes[preLenghtData + 1];
// return result;
//}
private byte[] GetBytes(byte[] bytes, int length, int index)
{
byte[] arrByteData = new byte[length];
for (int i = 0; i < length; i++)
{
arrByteData[i] = bytes[i + index];
}
return arrByteData;
}
public void Initialize()
{
}
}
}
}