This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Client/Point/Assets/Scripts/PlayerControl.cs

45 lines
1.4 KiB
C#

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)
{
if (currentDir != Vector2.zero)
{
SendMoveInput(currentDir);
}
}
else if (currentDir != _lastSentDirection)
{
SendMoveInput(currentDir);
}
}
private void SendMoveInput(Vector2 direction)
{
Debug.Log($"SendMoveInput {direction} {transform.GetComponent<PlayerInfo>().usn}");
_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)
});
}
}