feat 客户端
This commit is contained in:
42
Client/Point/Assets/Scripts/PlayerControl.cs
Normal file
42
Client/Point/Assets/Scripts/PlayerControl.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
|
||||
// 玩家控制,只有当前玩家能控制自己角色
|
||||
public class PlayerControl : MonoBehaviour
|
||||
{
|
||||
private const float SendRate = 1f; // 1秒至少发送一次
|
||||
private float _lastSendTime;
|
||||
private Vector2 _lastSentDirection = Vector2.zero;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var input = new Vector2(
|
||||
Input.GetAxisRaw("Horizontal"),
|
||||
Input.GetAxisRaw("Vertical")
|
||||
);
|
||||
var currentDir = input == Vector2.zero ? Vector2.zero : input.normalized;
|
||||
|
||||
if (Time.time >= _lastSendTime + 1f / SendRate)
|
||||
{
|
||||
SendMoveInput(currentDir);
|
||||
}
|
||||
else if (currentDir != _lastSentDirection)
|
||||
{
|
||||
SendMoveInput(currentDir);
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMoveInput(Vector2 direction)
|
||||
{
|
||||
Debug.Log($"SendMoveInput {direction} {transform.GetComponent<PlayerInfo>().uid}");
|
||||
_lastSentDirection = direction;
|
||||
_lastSendTime = Time.time;
|
||||
SocketManager.Instance.SendMessage(MessageID.Action, new C2S_Action
|
||||
{
|
||||
// Sequence = SocketManager.Instance.Sequence,
|
||||
// Timestamp = (long)(Time.time * 1000),
|
||||
Action = ActionID.Move,
|
||||
DirX = (int)(direction.x * 100),
|
||||
DirY = (int)(direction.y * 100)
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user