Browse Source

keyboard control move

master
JiLingXiaoBai 1 month ago
parent
commit
187c0c03b7
  1. 58
      client/client/Assets/Scripts/CoreGame/AFramSync/Render/View/JoystickRender.cs

58
client/client/Assets/Scripts/CoreGame/AFramSync/Render/View/JoystickRender.cs

@ -18,13 +18,13 @@ namespace CoreGame.Render
[HideInInspector] public Vector2 input = Vector2.zero; [HideInInspector] public Vector2 input = Vector2.zero;
private RectTransform m_BaseRect; private RectTransform m_BaseRect;
private Canvas m_Canvas; private Canvas m_Canvas;
private Vector2 m_StartPos = Vector2.zero; // private Vector2 m_StartPos = Vector2.zero;
private Vector2 m_StartPosInWorld = Vector2.zero; // private Vector2 m_StartPosInWorld = Vector2.zero;
private bool m_IsPointDown; private bool m_IsPointDown;
private Camera m_UICamera; private Camera m_UICamera;
[SerializeField] private Vector2 m_BackGroundOriginPos = new Vector2(0, -535); [SerializeField] private Vector2 m_BackGroundOriginPos = new Vector2(0, -535);
private const float MinDist = 1000f; private const float MinDist = 1f;
private Coroutine m_JoystickCanvasCoroutine; private Coroutine m_JoystickCanvasCoroutine;
private WaitForSeconds m_WaitForSeconds = new WaitForSeconds(1.0f); private WaitForSeconds m_WaitForSeconds = new WaitForSeconds(1.0f);
private bool m_IsForcePointerUp = false; private bool m_IsForcePointerUp = false;
@ -42,18 +42,18 @@ namespace CoreGame.Render
if (m_Canvas == null) if (m_Canvas == null)
Debug.LogError("The Joystick is not placed inside a canvas"); Debug.LogError("The Joystick is not placed inside a canvas");
Vector2 center = new(0.5f, 0.5f); // Vector2 center = new(0.5f, 0.5f);
background.pivot = center; // background.pivot = center;
//
handle.anchorMin = center; // handle.anchorMin = center;
handle.anchorMax = center; // handle.anchorMax = center;
handle.pivot = center; // handle.pivot = center;
handle.anchoredPosition = Vector2.zero; // handle.anchoredPosition = Vector2.zero;
m_IsPointDown = false; m_IsPointDown = false;
input = Vector2.zero; input = Vector2.zero;
background.anchoredPosition = m_BackGroundOriginPos; // background.anchoredPosition = m_BackGroundOriginPos;
// if (BattleModule.Instance.worldType == WorldType.Community) // if (BattleModule.Instance.worldType == WorldType.Community)
// { // {
// m_JoystickCanvasCoroutine = StartCoroutine(CanvasGroupAlphaUpdate(0.5f)); // m_JoystickCanvasCoroutine = StartCoroutine(CanvasGroupAlphaUpdate(0.5f));
@ -64,9 +64,9 @@ namespace CoreGame.Render
public void OnPointerDown(PointerEventData eventData) public void OnPointerDown(PointerEventData eventData)
{ {
m_IsForcePointerUp = false; m_IsForcePointerUp = false;
background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position); // background.anchoredPosition = ScreenPointToAnchoredPosition(eventData.position);
m_StartPos = eventData.position; // m_StartPos = eventData.position;
m_StartPosInWorld = m_UICamera.ScreenToWorldPoint(eventData.position); // m_StartPosInWorld = m_UICamera.ScreenToWorldPoint(eventData.position);
// if (BattleModule.Instance.worldType == WorldType.Community) // if (BattleModule.Instance.worldType == WorldType.Community)
// { // {
// if (m_JoystickCanvasCoroutine != null) // if (m_JoystickCanvasCoroutine != null)
@ -97,8 +97,8 @@ namespace CoreGame.Render
return; return;
if (m_IsPointDown == false) if (m_IsPointDown == false)
{ {
float dist = (eventData.position - m_StartPos).sqrMagnitude; // float dist = (eventData.position - m_StartPos).sqrMagnitude;
float dist = eventData.delta.sqrMagnitude;
if (dist > MinDist) if (dist > MinDist)
{ {
SetPointer(true); SetPointer(true);
@ -108,15 +108,15 @@ namespace CoreGame.Render
return; return;
} }
Vector2 position = background.anchoredPosition; // Vector2 position = background.anchoredPosition;
Vector2 radius = background.sizeDelta / 2; // Vector2 radius = background.sizeDelta / 2;
Vector2 worldPoint = m_UICamera.ScreenToWorldPoint(eventData.position); // Vector2 worldPoint = m_UICamera.ScreenToWorldPoint(eventData.position);
var posVector2 = ScreenPointToAnchoredPosition(eventData.position); // var posVector2 = ScreenPointToAnchoredPosition(eventData.position);
// input = (posVector2 - position) / (radius * m_Canvas.scaleFactor); // input = (posVector2 - position) / (radius * m_Canvas.scaleFactor);
input = eventData.delta; input = eventData.delta;
m_StartPosInWorld = worldPoint; // m_StartPosInWorld = worldPoint;
// HandleInput(input.magnitude, input.normalized); // HandleInput(input.magnitude, input.normalized);
handle.anchoredPosition = input * radius * handleRange; // handle.anchoredPosition = input * radius * handleRange;
} }
public void OnPointerUp(PointerEventData eventData) public void OnPointerUp(PointerEventData eventData)
@ -124,8 +124,8 @@ namespace CoreGame.Render
if (m_IsForcePointerUp) if (m_IsForcePointerUp)
return; return;
input = Vector2.zero; input = Vector2.zero;
background.anchoredPosition = m_BackGroundOriginPos; // background.anchoredPosition = m_BackGroundOriginPos;
handle.anchoredPosition = Vector2.zero; // handle.anchoredPosition = Vector2.zero;
SetPointer(false); SetPointer(false);
// if (BattleModule.Instance.worldType == WorldType.Community) // if (BattleModule.Instance.worldType == WorldType.Community)
// { // {
@ -171,6 +171,7 @@ namespace CoreGame.Render
private static readonly List<string> s_InputRemove = new List<string>(); private static readonly List<string> s_InputRemove = new List<string>();
private static float m_LruSpan = 1.5f; private static float m_LruSpan = 1.5f;
private static float m_KeyBoardSpeed = 10f;
private void Update() private void Update()
{ {
@ -180,19 +181,19 @@ namespace CoreGame.Render
Vector2 direction = Vector2.zero; Vector2 direction = Vector2.zero;
if (Input.GetKey(KeyCode.W) ) if (Input.GetKey(KeyCode.W) )
{ {
direction.y += 200; direction.y += m_KeyBoardSpeed;
} }
if (Input.GetKey(KeyCode.A) ) if (Input.GetKey(KeyCode.A) )
{ {
direction.x += -200; direction.x += -m_KeyBoardSpeed;
} }
if (Input.GetKey(KeyCode.S) ) if (Input.GetKey(KeyCode.S) )
{ {
direction.y += -200; direction.y += -m_KeyBoardSpeed;
} }
if (Input.GetKey(KeyCode.D)) if (Input.GetKey(KeyCode.D))
{ {
direction.x += 200; direction.x += m_KeyBoardSpeed;
} }
// if (CoreUIBridge.s_InputKey.Count > 0) // if (CoreUIBridge.s_InputKey.Count > 0)
@ -221,6 +222,7 @@ namespace CoreGame.Render
m_IsInputCtrl = true; m_IsInputCtrl = true;
OnPointerDown(pointerEventData); OnPointerDown(pointerEventData);
pointerEventData.position = pos + direction; pointerEventData.position = pos + direction;
pointerEventData.delta = direction;
// 模拟拖动 // 模拟拖动
OnDrag(pointerEventData); OnDrag(pointerEventData);
m_IsKeyBoardControl = true; m_IsKeyBoardControl = true;

Loading…
Cancel
Save