using Axibug;
using Axibug.Event;
using Axibug.Resources;
using Axibug.Runtime;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
namespace Game
{
public class InputComponent : GameComponent
{
public Vector2 InputV2 { get { return _InputV2; } }
private Vector2 _clickPoint; //鼠标点击ui位置
private Vector2 _InputV2;
private void Start()
{
//事件注册
//AppEntry.Event.Subscribe(A123EventArgs.EventId, OnA123EventArgs);
}
public void ReSetInput()
{
_InputV2 = Vector2.zero;
_clickPoint = Vector2.zero;
}
///
/// 检测是否点击UI
///
///
///
private GameObject GetPointerOverGameObject(Vector2 mousePosition, out bool hasUI)
{
hasUI = false;
//创建一个点击事件
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = mousePosition;
List raycastResults = new List();
//向点击位置发射一条射线,检测是否点击UI
EventSystem.current.RaycastAll(eventData, raycastResults);
if (raycastResults.Count == 0)
return null;
for(int i=0; i
/// 点击
///
void Update_Touch()
{
//双指操作
if (Update_DoubleTouch())
{
//如果有双指操作
return;
}
//按下
Vector2 clickPos = GetClickScreenPos();
if (clickPos.Equals(Vector2.zero))
{
return;
}
//点击UI,退出
GameObject obj = GetPointerOverGameObject(clickPos, out bool hasUI);
//显示点击特效
if (GamePlayEntry.UI.IsOpen()) GamePlayEntry.UI.Hide();
GamePlayEntry.UI.OpenUI();
if (hasUI)
{
return;
}
if (ProcessRaycast(clickPos, out E_NODE_TYPE withNode, out int CharIndex))
return;
if (obj == null)
return;
//点击地面
if (obj.CompareTag(Tags.Terrain) || obj.CompareTag(Tags.DROP))
{
//TODO一些阻止移动的内容
/*
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(
TerrainParent.GetComponent(),
clickPos,
GamePlayEntry.MainPlayer.TerrainCam,
out _clickPoint))
return;*/
if (withNode == E_NODE_TYPE.N_NPC)
{
//TODO 选中NPC
}
//TODO 移动
}
}
void Update_Input()
{
////AD方向控制
//_InputV2.x = Input.GetAxis("Horizontal");
////WS方向控制
//_InputV2.y = Input.GetAxis("Vertical");
//AD方向控制
_InputV2.x = Input.GetAxisRaw("Horizontal");
//WS方向控制
_InputV2.y = Input.GetAxisRaw("Vertical");
//AxibugLog.Debug("InputV2 =>" + InputV2);
}
#region 双指手势
protected readonly Transform m_zoom = null;
private float max = 300f;
private float min = -300;
protected static float current = 0;
private float last = -1;
public bool Update_DoubleTouch()
{
if (false)//有其他和双指互斥的操作则跳出,可补充
return false;
if (Input.touchCount == 2)
{
float dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);//两指之间的距离
if (-1 == last) last = dis;
float result = dis - last;//与上一帧比较变化
if (result + current < min)//区间限制:最小
result = min - current;
else if (result + current > max)//区间限制:最大
result = max - current;
result *= 0.1f;//系数
//TODO 可根据Result结果处理
current += result;//累计当前
last = dis;//记录为上一帧的值
return true;
}
else
{
last = -1;//不触发逻辑时
return false;
}
}
#endregion
}
}