AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/InputComponent.cs
2022-12-30 13:34:14 +08:00

216 lines
6.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
private Vector2 _clickPoint; //鼠标点击ui位置
private int RoleID;
private void Start()
{
//事件注册
//AppEntry.Event.Subscribe(A123EventArgs.EventId, OnA123EventArgs);
}
/// <summary>
/// 检测是否点击UI
/// </summary>
/// <param name="mousePosition"></param>
/// <returns></returns>
private GameObject GetPointerOverGameObject(Vector2 mousePosition, out bool hasUI)
{
hasUI = false;
//创建一个点击事件
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
//向点击位置发射一条射线检测是否点击UI
EventSystem.current.RaycastAll(eventData, raycastResults);
if (raycastResults.Count == 0)
return null;
for(int i=0; i<raycastResults.Count; i++)
{
if (raycastResults[i].gameObject.layer == LayerMask.NameToLayer("UI"))
{
hasUI = true;
continue;
}
return raycastResults[i].gameObject;
}
return null;
}
private Vector2 GetClickScreenPos()
{
//移动端
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount == 0)
return Vector2.zero;
// 手指刚触摸到屏幕的时候 手指在屏幕上移动
if (Input.GetTouch(0).phase != TouchPhase.Began
&& Input.GetTouch(0).phase != TouchPhase.Moved)
return Vector2.zero;
return Input.GetTouch(0).position;
}
//其它平台
else
{
if (!Input.GetMouseButtonDown(0))
return Vector2.zero;
return Input.mousePosition;
}
}
//点击角色
//private bool ProcessRaycast(Vector2 point, out GameObject obj,out E_NODE_TYPE withNode,out int CharIndex)
private bool ProcessRaycast(Vector2 point, out E_NODE_TYPE withNode,out int RoleID)
{
withNode = E_NODE_TYPE.N_FREE;
RoleID = 0;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(point);
if (!Physics.Raycast(ray, out hit))
return false;
GameObject obj = hit.collider.gameObject;
//点击怪物, 选中只作选中
if (obj.CompareTag(Tags.Monster))
{
//TODO 点击到怪物
return true;
}
else if(obj.CompareTag(Tags.NPC))
{
//TODO 点击到NPC
}
else if (obj.CompareTag(Tags.Players))
{
//TODO 点击到玩家
return true;
}
return false;
}
// Update is called once per frame
void Update()
{
//双指操作
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<TouchButtonEffectUI>()) GamePlayEntry.UI.Hide<TouchButtonEffectUI>();
GamePlayEntry.UI.OpenUI<TouchButtonEffectUI>();
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<RectTransform>(),
clickPos,
GamePlayEntry.MainPlayer.TerrainCam,
out _clickPoint))
return;*/
if (withNode == E_NODE_TYPE.N_NPC)
{
//TODO 选中NPC
}
//TODO 移动
}
}
#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
}
}