Update movement of host - discard this only

This commit is contained in:
HungDK
2026-01-10 12:11:31 +07:00
parent ac574933b7
commit e6d29176df
+90 -144
View File
@@ -16,7 +16,6 @@ using ModelRenderer.Scripts.GameData;
using PerfectWorld.Scripts;
using PerfectWorld.Scripts.Managers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
@@ -204,8 +203,9 @@ namespace BrewMonster
{
if (m_pNavigatePlayer == null)
{
// TODO: Implement proper creation of navigate player
// m_pNavigatePlayer = CreateNavigatePlayer();
// Create navigate player on-demand (C++: host owns a navigate/clone player + controller)
// 按需创建导航玩家(C++:宿主玩家持有导航/克隆玩家 + 控制器)
m_pNavigatePlayer = new CECHostNavigatePlayer(this);
}
return m_pNavigatePlayer;
}
@@ -2243,14 +2243,14 @@ namespace BrewMonster
public void OnMsgHstCorrectPos(in ECMSG Msg)
{
Debug.LogError("HoangDev : OnMsgHstCorrectPos");
//Debug.LogError("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();
//cmd_host_correct_pos pCmd = GPDataTypeHelper.FromBytes<cmd_host_correct_pos>((byte[])Msg.dwParam1);
Debug.LogError("HoangDev :pCmd.pos " + pCmd.pos);
//Debug.LogError("HoangDev :pCmd.pos " + pCmd.pos);
SetPos(pCmd.pos);
m_vVelocity.Clear();
m_CDRInfo.vAbsVelocity.Clear();
@@ -2472,7 +2472,7 @@ namespace BrewMonster
m_dwResFlags = (uint)PlayerResourcesReadyFlag.RESFG_ALL;
joystick = FindAnyObjectByType<Joystick>();
EventBus.Subscribe<JoystickRealeaseEvent>(JoystickRelease);
EventBus.Subscribe<JoystickPressEvent>(OnClickJoystick);
EventBus.Subscribe<JoystickPressEvent>(OnMsgHstPushMove);
if (TryGetComponent<PlayerVisual>(out var visual))
{
visual.InitPlayerEventDoneHandler();
@@ -2563,10 +2563,79 @@ namespace BrewMonster
// TODO: Move this to right flow later , it's just for test now
}
private void OnMsgHstPushMove(JoystickPressEvent joystickPressEvent)
{
//_playerStateMachine.ChangeState(_moveState);
/* if (m_pWorkMan.IsSitting())
{
g_pGame.GetGameSession().c2s_CmdStandUp();
return;
}*/
m_dwMoveRelDir = 0;
if (!CanDo(ActionCanDo.CANDO_MOVETO)) return;
if(joystick.Vertical > 0)
{
if(joystick.Horizontal > 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_FORWARD | MOVE_DIR.MD_RIGHT);
}
else if(joystick.Horizontal < 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_FORWARD | MOVE_DIR.MD_LEFT);
}
else
{
m_dwMoveRelDir |= (uint)MOVE_DIR.MD_FORWARD;
}
}
else if (joystick.Vertical < 0)
{
if (joystick.Horizontal > 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_BACK | MOVE_DIR.MD_RIGHT);
}
else if (joystick.Horizontal < 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_BACK | MOVE_DIR.MD_LEFT);
}
else
{
m_dwMoveRelDir |= (uint)MOVE_DIR.MD_BACK;
}
}
else
{
if (joystick.Horizontal > 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_RIGHT);
}
else if (joystick.Horizontal < 0)
{
m_dwMoveRelDir |= (uint)(MOVE_DIR.MD_LEFT);
}
}
bool bPushMove = true;
/* if (Msg.dwParam1 == 8 || Msg.dwParam1 == 9)
{
if (m_iMoveEnv != (int)MoveEnvironment.MOVEENV_AIR && m_iMoveEnv != (int)MoveEnvironment.MOVEENV_WATER)
bPushMove = false;
}*/
if (bPushMove /*&& !IsAboutToDie()*/ && CanDo(ActionCanDo.CANDO_MOVETO))
{
if (m_pWorkMan.CanStartWork(Host_work_ID.WORK_MOVETOPOS))
{
CECHPWorkMove pNewWork = (CECHPWorkMove)m_pWorkMan.CreateWork(Host_work_ID.WORK_MOVETOPOS);
pNewWork.SetDestination(CECHPWorkMove.DestTypes.DEST_PUSH, g_vOrigin);
m_pWorkMan.StartWork_p1(pNewWork);
}
}
}
private void OnDestroy()
{
EventBus.Unsubscribe<JoystickRealeaseEvent>(JoystickRelease);
EventBus.Unsubscribe<JoystickPressEvent>(OnClickJoystick);
EventBus.Unsubscribe<JoystickPressEvent>(OnMsgHstPushMove);
}
//TODO: Remove this function. Since it has been deprecated.
@@ -4455,14 +4524,6 @@ namespace BrewMonster
vPushDir = Vector3.zero;
if (joystick.Horizontal == 0 && joystick.Vertical == 0)
{
if (isPressMoveUp)
{
vPushDir = Vector3.up;
}
else if (isPressMoveDown)
{
vPushDir = Vector3.down;
}
return false;
}
@@ -4471,22 +4532,10 @@ namespace BrewMonster
}
else if (m_iMoveEnv == Move_environment.MOVEENV_WATER)
{
float angle = Vector2.Angle(new Vector2(joystick.Horizontal, joystick.Vertical), Vector2.up);
angle *= joystick.Horizontal < 0 ? 1 : -1;
Vector2 v2Cam = new Vector2(mainCam.transform.forward.x, mainCam.transform.forward.z);
v2Cam = Quaternion.Euler(0, 0, angle) * v2Cam;
v2Cam.Normalize();
vPushDir.x = v2Cam.x;
vPushDir.y = ((transform.position + Vector3.up * m_CDRInfo.vExtent.y) - mainCam.transform.position).normalized.y;
if (isPressMoveUp)
{
vPushDir.y = Math.Abs(vPushDir.y) * Time.deltaTime;
}
else if (isPressMoveDown)
{
vPushDir.y = -Math.Abs(vPushDir.y) * Time.deltaTime;
}
vPushDir.z = v2Cam.y;
vPushDir.x = joystick.Horizontal;
vPushDir.y = (transform.position - mainCam.transform.position).normalized.y;
vPushDir.z = joystick.Vertical;
vPushDir.Normalize();
}
else
{
@@ -4506,28 +4555,12 @@ namespace BrewMonster
// Is under water
bool CanTakeOffWater()
{
// TO DO: fix later
return true;
//A3DVECTOR3 vPos = GetPos();
//if (vPos.y < EC_Game.GetGameRun().GetWorld().GetWaterHeight(vPos) - m_MoveConst.fShoreDepth)
// return false;
//else
// return true;
A3DVECTOR3 vPos = GetPos();
float h0 = 0f;
int countHits0 = Physics.RaycastNonAlloc(EC_Utility.ToVector3(vPos) + Vector3.up * 500f, Vector3.down, hits, 1000f, 1 << 8);
if (countHits0 > 0)
{
h0 = hits[0].point.y;
}
if(vPos.y < h0 - m_MoveConst.fShoreDepth)
{
return false;
}
else
{
return true;
}
}
bool IsUsingItem()
@@ -4551,27 +4584,6 @@ namespace BrewMonster
transform.rotation = Quaternion.LookRotation(dir);
}
public void SetRotationHPWithTime(Vector3 dir, float time)
{
StartCoroutine(RotateToDir(transform, dir, time));
}
IEnumerator RotateToDir(Transform target, Vector3 dir, float duration)
{
Quaternion startRot = target.rotation;
Quaternion endRot = Quaternion.LookRotation(dir);
float t = 0f;
while (t < duration)
{
t += Time.deltaTime;
target.rotation = Quaternion.Slerp(startRot, endRot, t / duration);
yield return null;
}
target.rotation = endRot;
}
void SetJumpInWater(bool b)
{
m_bJumpInWater = b;
@@ -5317,7 +5329,7 @@ namespace BrewMonster
}
// Is under water
public bool IsUnderWater()
bool IsUnderWater()
{
return m_iMoveEnv == Move_environment.MOVEENV_WATER ? true : false;
}
@@ -5407,7 +5419,7 @@ namespace BrewMonster
VertRayTrace(EC_Utility.ToVector3(vTestPos), ref vGndPos, ref m_GndInfo.vGndNormal, 1000f);
m_GndInfo.fGndHei = vGndPos.y;
if (Physics.RaycastNonAlloc(EC_Utility.ToVector3(vTestPos) + Vector3.up * 500f, Vector3.down, hits, 1000f, 1 << 8) > 0)
if(Physics.RaycastNonAlloc(EC_Utility.ToVector3(vTestPos), Vector3.down, hits, 1000f, 1<<8) > 0)
{
m_GndInfo.fWaterHei = hits[0].point.y;
}
@@ -5509,13 +5521,13 @@ namespace BrewMonster
LayerMask layerMaskTerrain = 1 << 6;
LayerMask layerMaskBush = 1 << 7;
if (Physics.RaycastNonAlloc(vPos, (Vector3.down), hits, layerMaskTerrain) > 0)
if (Physics.RaycastNonAlloc(vPos, (vPos + Vector3.down), hits, layerMaskTerrain) > 0)
{
vTerrainPos = hits[0].point;
vTerrainNormal = hits[0].normal;
}
if (Physics.RaycastNonAlloc(vPos, (Vector3.down), hits, DeltaY, layerMaskBush) > 0)
if (Physics.RaycastNonAlloc(vPos, (vPos + Vector3.down), hits, DeltaY, layerMaskTerrain) > 0)
{
if (vBuildingPos.y > vTerrainPos.y)
{
@@ -5543,85 +5555,19 @@ namespace BrewMonster
vHitPos.y -= DeltaY;
LayerMask layerMask = 1 << 6 | 1 << 7;
hits = new RaycastHit[5];
int count = Physics.BoxCastNonAlloc(vCenter, vExt, (Vector3.down).normalized,
hits, transform.rotation, DeltaY, layerMask);
if (count == 0)
if (Physics.BoxCastNonAlloc(vCenter, vExt, (vCenter + Vector3.down).normalized,
hits, transform.rotation, DeltaY, layerMask) == 0)
{
vHitPos = vCenter;
vHitNormal = EC_Utility.ToA3DVECTOR3(Vector3.up);
}
else
{
if (Math.Abs(hits[0].distance - 0f) <= float.Epsilon)
{
count = Physics.BoxCastNonAlloc(vCenter, new Vector3(vExt.x, 0.05f, vExt.z), (Vector3.down).normalized,
hits, transform.rotation, DeltaY, layerMask);
if(count == 0)
{
vHitPos = vCenter;
vHitNormal = EC_Utility.ToA3DVECTOR3(Vector3.up);
}
else
{
vHitPos = hits[0].point;
vHitPos.y += vExt.y;
vHitNormal = EC_Utility.ToA3DVECTOR3(hits[0].normal);
}
}
{
vHitPos = hits[0].point;
vHitNormal = EC_Utility.ToA3DVECTOR3(hits[0].normal);
}
}
void DebugBoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction,
Quaternion orientation, float distance, Color color)
{
Vector3 end = center + direction.normalized * distance;
// Vẽ box ở vị trí bắt đầu
DebugDrawBox(center, halfExtents, orientation, color);
// Vẽ box ở vị trí kết thúc
DebugDrawBox(end, halfExtents, orientation, color);
// Vẽ đường nối giữa 2 box
Debug.DrawLine(center, end, color);
}
// Vẽ 1 box từ center + halfExtents + rotation
void DebugDrawBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, Color color)
{
var matrix = Matrix4x4.TRS(center, orientation, Vector3.one);
Vector3[] points = {
matrix.MultiplyPoint3x4(new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3( halfExtents.x, -halfExtents.y, -halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3( halfExtents.x, -halfExtents.y, halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3(-halfExtents.x, -halfExtents.y, halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3( halfExtents.x, halfExtents.y, -halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3( halfExtents.x, halfExtents.y, halfExtents.z)),
matrix.MultiplyPoint3x4(new Vector3(-halfExtents.x, halfExtents.y, halfExtents.z))
};
// đáy
Debug.DrawLine(points[0], points[1], color);
Debug.DrawLine(points[1], points[2], color);
Debug.DrawLine(points[2], points[3], color);
Debug.DrawLine(points[3], points[0], color);
// đỉnh
Debug.DrawLine(points[4], points[5], color);
Debug.DrawLine(points[5], points[6], color);
Debug.DrawLine(points[6], points[7], color);
Debug.DrawLine(points[7], points[4], color);
// cạnh
Debug.DrawLine(points[0], points[4], color);
Debug.DrawLine(points[1], points[5], color);
Debug.DrawLine(points[2], points[6], color);
Debug.DrawLine(points[3], points[7], color);
}
void OnMsgHstExtProp(ECMSG Msg)
{
cmd_own_ext_prop pCmd = GPDataTypeHelper.FromBytes<cmd_own_ext_prop>((byte[])Msg.dwParam1);