Files
test/Assets/Scripts/CECHostPlayer.cs
T
2025-10-09 17:04:07 +07:00

574 lines
21 KiB
C#

using BrewMonster.Network;
using CSNetwork;
using CSNetwork.GPDataType;
using CSNetwork.Protocols;
using CSNetwork.Protocols.RPCData;
using PerfectWorld.Scripts.Managers;
using PerfectWorld.Scripts.Task;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Scene = UnityEngine.SceneManagement.Scene;
public class CECHostPlayer : EC_Player
{
[SerializeField] private TextMeshPro txtName;
[SerializeField] private CharacterController controller;
[SerializeField] private Joystick joystick;
[SerializeField] private Button btnJump;
[SerializeField] private Button btnRun;
[SerializeField] private Transform parentModel;
PlayerStateMachine _playerStateMachine;
PlayerMoveState _moveState;
PlayerIdleState _idleState;
CECHostMove m_MoveCtrl;
float playerSpeed = 5.0f;
float jumpHeight = 1.5f;
float gravityValue = -9.81f;
StateAnim stateAnim = StateAnim.Idle;
Vector3 playerVelocity;
bool isGrounded = false;
bool isRun = false;
Vector3 m_vLastSevPos;
// ====== Ground cast config ======
[Header("Ground Cast")]
[Tooltip("Khoảng thêm ngoài skinWidth để SphereCast xuống (m ngắn)")]
[SerializeField] private float extraGroundDistance = 0.05f;
[Tooltip("Bớt bán kính một chút để tránh tự va vào capsule (epsilon)")]
[SerializeField] private float radiusEpsilon = 0.005f;
[Tooltip("Layer mặt đất")]
[SerializeField] private LayerMask groundMask;
[Tooltip("Layer mặt đất")]
[SerializeField] private float slopeToleranceDeg = 2f;
// cache tùy chọn (không bắt buộc)
float ccRadius, ccSkin;
RaycastHit lastGroundHit;
private void Awake()
{
base.Awake();
_moveState = new PlayerMoveState(this);
_idleState = new PlayerIdleState(this);
_playerStateMachine = new PlayerStateMachine();
m_MoveCtrl = new CECHostMove(this);
// Cache: không bắt buộc, nhưng gọn tay và ít gọi property lặp.
if (controller != null)
{
ccRadius = controller.radius;
ccSkin = controller.skinWidth;
}
}
public void SetModelHostPlayer()
{
m_pPlayerModel = NPCManager.Instance.GetModelPlayer();
Scene scene = SceneManager.GetSceneByName("WorldRender");
SceneManager.MoveGameObjectToScene(m_pPlayerModel, scene);
m_pPlayerModel.transform.SetParent(parentModel);
m_pPlayerModel.transform.localPosition = Vector3.zero;
m_pPlayerModel.SetActive(true);
}
private void Start()
{
_playerStateMachine.InitState(_idleState);
// btnJump.onClick.AddListener(HandleJump);
}
private void Update()
{
//Debug.Log($"(ulong)Time.deltaTime * 1000 {(ulong)(Time.deltaTime * 1000)}");
m_MoveCtrl.Tick((ulong)(Time.deltaTime * 1000));
// Nếu có thay đổi runtime, có thể lấy lại mỗi vài giây/Start nếu bạn thích:
// ccRadius = controller.radius; ccSkin = controller.skinWidth;
_playerStateMachine.UpdateState();
}
public void StopMovement()
{
m_MoveCtrl.SendStopMoveCmd(transform.position, 5f, (int)GPMoveMode.GP_MOVE_WALK);
}
public void HandleMovement()
{
// 1) Kiểm tra grounded bằng SphereCast ngắn dựa trên radius + skinWidth
isGrounded = GroundCheck(out lastGroundHit);
// 2) Input tạm thời: giữ nguyên như bạn
if (UnityEngine.Input.GetKeyDown(KeyCode.LeftShift)) SetStatusRun(true);
if (UnityEngine.Input.GetKeyUp(KeyCode.LeftShift)) SetStatusRun(false);
if (UnityEngine.Input.GetKeyDown(KeyCode.Space)) HandleJump();
// 3) Trọng lực / sticky
if (isGrounded && playerVelocity.y < 0f)
{
// Đè nhẹ để bám đất (tránh nhấp-nháy)
playerVelocity.y = -2f;
}
else
{
playerVelocity.y += gravityValue * Time.deltaTime;
}
// 4) Chuyển động phẳng
float x = joystick.Horizontal;
float z = joystick.Vertical;
Vector3 move = new Vector3(x, 0, z);
move = Vector3.ClampMagnitude(move, 1f);
if (move != Vector3.zero)
{
transform.forward = move;
m_MoveCtrl.GroundMove(Time.deltaTime);
m_MoveCtrl.SendMoveCmd(transform.position, controller.velocity, (int)GPMoveMode.GP_MOVE_RUN);
}
else
{
}
Vector3 finalMove = (move * playerSpeed) + (playerVelocity.y * Vector3.up);
controller.Move(finalMove * Time.deltaTime);
}
private void JoystickRelease(JoystickRealeaseEvent joystickRealeaseEvent)
{
_playerStateMachine.ChangeState(_idleState);
}
private bool GroundCheck(out RaycastHit hit)
{
float radius = controller.radius;
float skin = controller.skinWidth;
float height = controller.height;
// Tâm capsule theo world
Vector3 cWorld = transform.TransformPoint(controller.center);
float hemi = Mathf.Max(0f, (height * 0.5f) - radius);
// Hai điểm top/bottom của capsule nhân vật (đang đứng)
Vector3 pTop = cWorld + Vector3.up * hemi;
Vector3 pBottom = cWorld - Vector3.up * hemi;
// Ta tạo một "đoạn capsule ngắn" gần đáy để sweep xuống
// Nhấc đoạn bắt đầu lên 1 chút để không bắt đầu trong trạng thái giao nhau
Vector3 startBottom = pBottom + Vector3.up * (skin + 0.01f);
Vector3 startTop = startBottom + Vector3.up * (radius * 2f - 0.02f); // chiều cao đoạn ngắn ~2*radius
float castRadius = Mathf.Max(0f, radius - radiusEpsilon);
float castDistance = skin + extraGroundDistance; // quãng sweep ngắn
bool hitSomething = Physics.CapsuleCast(
startTop, startBottom, castRadius,
Vector3.down, out hit, castDistance,
groundMask, QueryTriggerInteraction.Ignore
);
if (!hitSomething) return false;
// Lọc theo slope limit
float maxSlope = controller.slopeLimit + slopeToleranceDeg;
float slope = Vector3.Angle(hit.normal, Vector3.up);
if (slope > maxSlope) return false;
return true;
}
private void HandleJump()
{
if (isGrounded)
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravityValue);
}
}
public void ProcessMessage(in ECMSG Msg)
{
Debug.LogWarning("HoangDev : ProcessMessageProcessMessageProcessMessage");
var msg = (int)Msg.dwMsg;
switch (msg)
{
case int value when value == EC_MsgDef.MSG_HST_CORRECTPOS: OnMsgHstCorrectPos(Msg); break;
case int value when value == EC_MsgDef.MSG_HST_GOTO: OnMsgHstGoto(Msg); break;
case int value when value == EC_MsgDef.MSG_HST_IVTRINFO:
{
OnMsgHstIvtrInfo(Msg);
break;
}
case int value when value == EC_MsgDef.MSG_HST_OWNITEMINFO:
{
OnMsgHstOwnItemInfo(Msg);
break;
}
case int value when value == EC_MsgDef.MSG_HST_TASKDATA:
{
OnMsgHstTaskData(Msg);
Debug.Log("[Dat]- OnMsgHstTaskData");
break;
}
case int value when value == EC_MsgDef.MSG_HST_ITEMOPERATION:
OnMsgHstItemOperation(Msg);
break;
case int value when value == EC_MsgDef.MSG_HST_PICKUPITEM:
OnMsgHstPickupItem(Msg);
break;
}
}
public void OnMsgHstPickupItem(in ECMSG Msg)
{
var data = Msg.dwParam1 as byte[];
int cmd = Convert.ToInt32(Msg.dwParam2);
switch (cmd)
{
case CommandID.PICKUP_ITEM:
{
// Parse the pickup item data from the server response
if (data != null && data.Length >= 16)
{
int tid = BitConverter.ToInt32(data, 0);
int expire_date = BitConverter.ToInt32(data, 4);
uint iAmount = BitConverter.ToUInt32(data, 8);
uint iSlotAmount = BitConverter.ToUInt32(data, 12);
byte byPackage = data[16];
byte bySlot = data[17];
Debug.Log($"[Inventory] PICKUP_ITEM: tid={tid}, expire_date={expire_date}, iAmount={iAmount}, iSlotAmount={iSlotAmount}, byPackage={byPackage}, bySlot={bySlot}");
// Notify pickupItem script about successful pickup
pickupItem pickupScript = pickupItem.Instance;
if (pickupScript != null)
{
pickupScript.OnPickupSuccess(tid);
}
// Create new inventory item data
var newItem = new InventoryItemData
{
Package = byPackage,
Slot = bySlot,
TemplateId = tid,
ExpireDate = expire_date,
State = 0,
Count = (int)iAmount,
Crc = 0,
Content = null
};
// Add item to inventory
EC_Inventory.SetItem(byPackage, bySlot, newItem);
Debug.Log($"[Inventory] Successfully added item {tid} to package {byPackage}, slot {bySlot} with count {iAmount}");
// Trigger UI refresh if an EC_InventoryUI is present in scene
var ui = GameObject.FindFirstObjectByType<EC_InventoryUI>();
if (ui != null)
{
ui.RefreshAll();
}
}
else
{
Debug.LogWarning("[Inventory] PICKUP_ITEM: Invalid data length");
}
break;
}
}
}
public void OnMsgHstItemOperation(ECMSG Msg)
{
var data = Msg.dwParam1 as byte[];
int cmd = Convert.ToInt32(Msg.dwParam2);
switch (cmd)
{
case CommandID.PLAYER_DROP_ITEM:
{
// Parse the drop item data from the server response
if (data != null && data.Length >= 6)
{
byte byPackage = data[0];
byte bySlot = data[1];
int count = BitConverter.ToInt32(data, 2);
int tid = BitConverter.ToInt32(data, 6);
byte reason = data[10];
Debug.Log($"[Inventory] PLAYER_DROP_ITEM: package={byPackage}, slot={bySlot}, count={count}, tid={tid}, reason={reason}");
// Update the inventory by removing the item
bool success = EC_Inventory.RemoveItem(byPackage, bySlot, count);
if (success)
{
Debug.Log($"[Inventory] Successfully removed {count} items from package {byPackage}, slot {bySlot}");
// Trigger UI refresh if an EC_InventoryUI is present in scene
var ui = GameObject.FindFirstObjectByType<EC_InventoryUI>();
if (ui != null)
{
ui.RefreshAll();
}
}
else
{
Debug.LogWarning($"[Inventory] Failed to remove items from package {byPackage}, slot {bySlot}");
}
}
else
{
Debug.LogWarning("[Inventory] PLAYER_DROP_ITEM: Invalid data length");
}
break;
}
case CommandID.EQUIP_ITEM:
{
byte index_inv = data[0];
byte index_equip = data[1];
// Update client-side data: move item between PACK_INVENTORY and PACK_EQUIPMENT
var invItem = EC_Inventory.GetItem(EC_Inventory.PACK_INVENTORY, index_inv, true);
var equipItem = EC_Inventory.GetItem(EC_Inventory.PACK_EQUIPMENT, index_equip, true);
if (invItem != null)
{
invItem.Package = EC_Inventory.PACK_EQUIPMENT;
invItem.Slot = index_equip;
EC_Inventory.SetItem(EC_Inventory.PACK_EQUIPMENT, index_equip, invItem);
}
if (equipItem != null)
{
equipItem.Package = EC_Inventory.PACK_INVENTORY;
equipItem.Slot = index_inv;
EC_Inventory.SetItem(EC_Inventory.PACK_INVENTORY, index_inv, equipItem);
}
// Trigger UI refresh if an EC_InventoryUI is present in scene
var ui = GameObject.FindObjectOfType<EC_InventoryUI>();
if (ui != null)
{
ui.RefreshAll();
}
break;
}
}
}
public void OnMsgHstOwnItemInfo(ECMSG Msg)
{
int cmd = Convert.ToInt32(Msg.dwParam2);
switch (cmd)
{
case CommandID.OWN_ITEM_INFO:
{
Debug.Log("[Inventory] OWN_ITEM_INFO received");
var data = Msg.dwParam1 as byte[];
int hostId = Convert.ToInt32(Msg.dwParam3);
PerfectWorld.Scripts.Managers.EC_Inventory.LogInventoryPacket("OWN_ITEM_INFO", data, hostId);
break;
}
}
}
public void OnMsgHstIvtrInfo(ECMSG Msg)
{
var data = Msg.dwParam1 as byte[];
int cmd = Convert.ToInt32(Msg.dwParam2);
int hostId = Convert.ToInt32(Msg.dwParam3);
switch (cmd)
{
case CommandID.OWN_IVTR_DATA:
{
Debug.Log("[Inventory] OWN_IVTR_DATA received");
PerfectWorld.Scripts.Managers.EC_Inventory.LogInventoryPacket("OWN_IVTR_DATA", data, hostId);
break;
}
case CommandID.OWN_IVTR_DETAIL_DATA:
{
Debug.Log("[Inventory] OWN_IVTR_DETAIL_DATA received");
PerfectWorld.Scripts.Managers.EC_Inventory.LogInventoryPacket("OWN_IVTR_DETAIL_DATA", data, hostId);
// Parse and store
if (data != null && data.Length >= 6)
{
byte byPackage = data[0];
byte ivtrSize = data[1];
if (PerfectWorld.Scripts.Managers.EC_IvtrItem.TryParseInventoryDetail(data, out var pkg, out var size, out var items))
{
PerfectWorld.Scripts.Managers.EC_Inventory.UpdatePack(pkg, size, items);
}
}
break;
}
}
}
public void OnMsgHstCorrectPos(in ECMSG Msg)
{
Debug.LogWarning("HoangDev : OnMsgHstCorrectPos");
byte[] buf = (byte[])Msg.dwParam1; // chỗ bạn lưu pDataBuf
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
cmd_host_correct_pos pCmd = (cmd_host_correct_pos)Marshal.PtrToStructure(
handle.AddrOfPinnedObject(), typeof(cmd_host_correct_pos));
handle.Free();
Debug.LogWarning("HoangDev :pCmd.pos " + pCmd.pos);
SetPos(pCmd.pos);
}
public void OnMsgHstGoto(in ECMSG Msg)
{
Debug.Log("HoangDev :OnMsgHstGoto");
cmd_notify_hostpos pCmd = (cmd_notify_hostpos)Msg.dwParam1;
}
private void SetPos(Vector3 pos)
{
transform.position = pos;
}
public void SetStatusRun(bool value)
{
if (!isGrounded)
{
Debug.LogError("Player not in ground");
return;
}
isRun = value;
}
public void InitCharacter(cmd_self_info_1 role)
{
string roleName = "(Error decoding name)";
//if (role.name != null && role.name.ByteArray != null)
//{
// roleName = Encoding.UTF8.GetString(role.name.ByteArray, 0, role.name.Length);
//}
SetPlayerInfor(new INFO(role.cid, role.crc_e, role.crc_c));
Vector3 pos = new Vector3(role.pos.x, role.pos.y, role.pos.z);
if (txtName != null) txtName.text = roleName;
transform.position = pos;
SetModelHostPlayer();
Debug.LogError("Pos Character = " + pos);
joystick = FindAnyObjectByType<Joystick>();
EventBus.Subscribe<JoystickRealeaseEvent>(JoystickRelease);
EventBus.Subscribe<JoystickPressEvent>(JoystickStartDrag);
if (TryGetComponent<PlayerVisual>(out var visual))
{
visual.InitHostPlayerEventDoneHandler();
}
}
private void JoystickStartDrag(JoystickPressEvent joystickPressEvent)
{
_playerStateMachine.ChangeState(_moveState);
}
private void OnDestroy()
{
EventBus.Unsubscribe<JoystickRealeaseEvent>(JoystickRelease);
EventBus.Unsubscribe<JoystickPressEvent>(JoystickStartDrag);
}
public void InitCharacter(info_player_1 role)
{
string roleName = "(Error decoding name)";
//if (role.name != null && role.name.ByteArray != null)
//{
// roleName = Encoding.UTF8.GetString(role.name.ByteArray, 0, role.name.Length);
//}
Vector3 pos = new Vector3(role.pos.x, role.pos.y, role.pos.z);
if (txtName != null) txtName.text = roleName;
transform.position = pos;
SetModelHostPlayer();
Debug.LogError("Pos Character = " + pos);
}
#region Task
public void OnMsgHstTaskData(ECMSG Msg)
{
int cmd = Convert.ToInt32(Msg.dwParam2);
if (cmd == CommandID.TASK_DATA)
{
Debug.Log("[Dat]- OnMsgHstTaskData- TASK_DATA");
//a_LogOutput(1, "[Dat]- EC_HostMsg- OnMsgHstTaskData- TASK_DATA");
//cmd_task_data* pCmd = (cmd_task_data*)Msg.dwParam1;
//ASSERT(pCmd);
//int iActiveListSize = (int)pCmd->active_list_size;
//BYTE* pData = (BYTE*)pCmd + sizeof(size_t);
//void* pActiveListbuf = pData;
//pData += iActiveListSize;
//int iFinishedListSize = *(int*)pData;
//pData += sizeof(int);
//void* pFinishedListBuf = pData;
//pData += iFinishedListSize;
//int iFinishTimeListSize = *(int*)pData;
//pData += sizeof(int);
//void* pFinishTimeListBuf = pData;
//pData += iFinishTimeListSize;
//int iFinishedCountListSize = *(int*)pData;
//pData += sizeof(int);
//void* pFinishedCountListBuf = pData;
//pData += iFinishedCountListSize;
//int iStorageTasksListSize = *(int*)pData;
//pData += sizeof(int);
//void* pStorageTaskListBuf = pData;
//pData += iStorageTasksListSize;
//A3DRELEASE(m_pTaskInterface);
return;
var m_pTaskInterface = new CECTaskInterface(this);
if (!m_pTaskInterface.Init(null, 0, null, 0,
null, 0, null, 0, null, 0))
{
//a_LogOutput(1, "CECHostPlayer::OnMsgHstTaskData, failed to initialize task interface");
return;
}
m_pTaskInterface.CheckPQEnterWorldInit();
//// check if player has equipped goblin
//if (m_pEquipPack->GetItem(EQUIPIVTR_GOBLIN) != NULL)
//{
// CECIvtrGoblin* pIvtrGoblin = (CECIvtrGoblin*)m_pEquipPack->GetItem(EQUIPIVTR_GOBLIN);
// m_pGoblin = new CECHostGoblin();
// m_pGoblin->Init(pIvtrGoblin->GetTemplateID(), pIvtrGoblin, this);
//}
//// Note: this command now is also used as the end flag of responding
//// for GET_ALL_DATA request
//g_pGame->GetGameSession()->LoadConfigData();
//// ¸ù¾Ý×°±¸°ü¹ü¸üÐÂ×°±¸¼¼Äܵ½¼¼ÄÜÁбí
//if (UpdateEquipSkills())
// UpdateEquipSkillCoolDown();
}
else if (cmd == CommandID.TASK_VAR_DATA)
{
//cmd_task_var_data* pCmd = (cmd_task_var_data*)Msg.dwParam1;
//ASSERT(pCmd);
//if (m_pTaskInterface)
// OnServerNotify(m_pTaskInterface, pCmd->data, pCmd->size);
//else
// ASSERT(m_pTaskInterface);
}
}
#endregion
}
public enum StateAnim
{
Idle = 1,
Walk = 2,
Run = 3,
Jump = 4
}