Files
test/Assets/PerfectWorld/Scripts/Managers/EC_ManPlayer.cs
T
2025-09-13 09:59:38 +07:00

198 lines
7.4 KiB
C#

using BrewMonster;
using BrewMonster.Network;
using CSNetwork;
using CSNetwork.GPDataType;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace PerfectWorld.Scripts.Managers
{
namespace BrewMonster.Managers
{
[Serializable]
public class EC_ManPlayer : IMsgHandler
{
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");
if (GameController.Instance == null) return true;
GameController.Instance.GetHostPlayer().ProcessMessage(Msg);
}
else if (Msg.iSubID < 0)
{
switch ((int)Msg.dwMsg)
{
case int value when value == EC_MsgDef.MSG_PM_PLAYERINFO:
{
Debug.Log(" EC_MsgDef.MSG_PM_PLAYERINFO");
OnMsgPlayerInfo(Msg);
break;
}
case int value when value == EC_MsgDef.MSG_PM_PLAYERMOVE:
{
OnMsgPlayerMove(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);
switch (Convert.ToInt32(Msg.dwParam2))
{
case CommandID.PLAYER_INFO_1:
case CommandID.PLAYER_ENTER_WORLD:
case CommandID.PLAYER_ENTER_SLICE:
{
if (cid != iHostID)
{
GameController.Instance.Log("ElsePlayer has join");
}
break;
}
case CommandID.SELF_INFO_1:
Debug.Log("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)
{
Debug.Log("HostPlayerInfo1");
bool isDoneWorldRender = false;
bool isDoneNPCRender = false;
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();
});
return true;
}
public EC_ElsePlayer ElsePlayerEnter()
{
return null;
}
private EC_ElsePlayer SeekOutElsePlayer(int cid)
{
return null;
}
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()
{
}
}
}
}