264 lines
10 KiB
C#
264 lines
10 KiB
C#
using BrewMonster;
|
|
using BrewMonster.Network;
|
|
using CSNetwork;
|
|
using CSNetwork.GPDataType;
|
|
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
|
|
{
|
|
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)
|
|
{
|
|
if (Msg.iSubID == 0)
|
|
{
|
|
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void OnMsgPlayerInfo(ECMSG Msg)
|
|
{
|
|
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);
|
|
ElsePlayerEnter(info_Player_1, commandID);
|
|
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;
|
|
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(info_player_1 info, int iCmd)
|
|
{
|
|
// 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(Info, iAppearFlag, true);
|
|
return pPlayer;
|
|
}
|
|
|
|
// Create a new player
|
|
if (!(pPlayer = CreateElsePlayer(info, iAppearFlag)))
|
|
{
|
|
return null;
|
|
}
|
|
lock (m_csPlayerTab)
|
|
{
|
|
if (m_PlayerTab.ContainsKey(info.cid))
|
|
{
|
|
m_PlayerTab[info.cid] = pPlayer;
|
|
}
|
|
}
|
|
return pPlayer;
|
|
}
|
|
|
|
private EC_ElsePlayer CreateElsePlayer(info_player_1 info, int iAppearFlag)
|
|
{
|
|
var ob = GameController.Instance.InitCharacter(info);
|
|
EC_ElsePlayer elsePlayer = ob.AddComponent<EC_ElsePlayer>();
|
|
return elsePlayer;
|
|
}
|
|
|
|
private 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;
|
|
}
|
|
|
|
//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()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
} |