fix: 切割锯子接入EventTrigger
This commit is contained in:
@@ -34,26 +34,26 @@ namespace AibisDream
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isCuttingMode && !isGearActive && gearFollower != null)
|
||||
{
|
||||
// 检测点击
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Ray ray = _mainCamera.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
|
||||
|
||||
if (hit.collider != null && hit.collider.gameObject.GetComponent<GearFollower>() != null)
|
||||
{
|
||||
gearFollower=hit.collider.gameObject.GetComponent<GearFollower>();
|
||||
ActivateGear();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isCuttingMode && isGearActive && gearFollower != null)
|
||||
if (isCuttingMode && isGearActive && gearFollower != null)
|
||||
{
|
||||
gearFollower.GearUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryActivateGear(GearFollower source)
|
||||
{
|
||||
if (!isCuttingMode || source == null || source != gearFollower)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isGearActive)
|
||||
{
|
||||
ActivateGear();
|
||||
}
|
||||
|
||||
return isGearActive;
|
||||
}
|
||||
public void ClearCutLine()
|
||||
{
|
||||
if (gearFollower != null)
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.VFX;
|
||||
using UnityEngine.EventSystems;
|
||||
using DG.Tweening;
|
||||
using AibisDream.FixSystem;
|
||||
using AibisDream; // 添加OpenSystem的命名空间
|
||||
using AibisDream.Framework;
|
||||
using System.Collections;
|
||||
using AibisDream.Kit;
|
||||
|
||||
public class GearFollower : MonoBehaviour
|
||||
[RequireComponent(typeof(EventTriggerEx))]
|
||||
public class GearFollower : MonoBehaviour, IInteraction
|
||||
{
|
||||
[Header("参数")]
|
||||
public SpriteRenderer targetSpriteRenderer;
|
||||
@@ -95,6 +99,73 @@ public class GearFollower : MonoBehaviour
|
||||
private Vector3 targetPosition = Vector3.zero;
|
||||
private bool isInTractionMode = false;
|
||||
private bool isDrillSfxPlaying = false; // 标记音效是否已播放
|
||||
private EventTriggerEx eventTrigger;
|
||||
private bool isPointerHeld;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
eventTrigger = GetComponent<EventTriggerEx>();
|
||||
EnsureEventTriggerEntries();
|
||||
eventTrigger.Register(EventTriggerType.PointerDown, OnPointerDown);
|
||||
eventTrigger.Register(EventTriggerType.PointerUp, OnPointerUp);
|
||||
}
|
||||
|
||||
private void EnsureEventTriggerEntries()
|
||||
{
|
||||
if (eventTrigger == null) return;
|
||||
|
||||
var needed = new[] { EventTriggerType.PointerDown, EventTriggerType.PointerUp };
|
||||
foreach (var triggerType in needed)
|
||||
{
|
||||
if (eventTrigger.triggers.Any(entry => entry.eventID == triggerType)) continue;
|
||||
eventTrigger.triggers.Add(new EventTrigger.Entry { eventID = triggerType });
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerDown(BaseEventData eventData)
|
||||
{
|
||||
if (eventData is not PointerEventData { button: PointerEventData.InputButton.Left }) return;
|
||||
|
||||
var eventSystem = EventSystemEx.Instance;
|
||||
if (!IsAvailable || eventSystem == null || eventSystem.isLocked)
|
||||
{
|
||||
eventSystem?.UnRegister(this);
|
||||
return;
|
||||
}
|
||||
|
||||
var cuttingManager = FixSystemCenter.SystemDic.Get<CuttingManager>();
|
||||
if (cuttingManager == null || !cuttingManager.TryActivateGear(this))
|
||||
{
|
||||
eventSystem.UnRegister(this);
|
||||
return;
|
||||
}
|
||||
|
||||
isPointerHeld = true;
|
||||
}
|
||||
|
||||
private void OnPointerUp(BaseEventData eventData)
|
||||
{
|
||||
if (eventData is not PointerEventData { button: PointerEventData.InputButton.Left }) return;
|
||||
|
||||
isPointerHeld = false;
|
||||
EndCutting();
|
||||
}
|
||||
|
||||
private bool IsInteractionLocked()
|
||||
{
|
||||
return EventSystemEx.Instance == null || EventSystemEx.Instance.isLocked;
|
||||
}
|
||||
|
||||
private void CancelPointerInteraction()
|
||||
{
|
||||
isPointerHeld = false;
|
||||
if (isCutting)
|
||||
{
|
||||
EndCutting();
|
||||
}
|
||||
|
||||
EventSystemEx.Instance?.UnRegister(this);
|
||||
}
|
||||
|
||||
public void Init(SpriteRenderer targetSpriteRenderer)
|
||||
{
|
||||
@@ -146,13 +217,19 @@ public class GearFollower : MonoBehaviour
|
||||
|
||||
public void GearUpdate()
|
||||
{
|
||||
if (IsInteractionLocked())
|
||||
{
|
||||
CancelPointerInteraction();
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取鼠标在屏幕上的位置
|
||||
Vector3 mouseScreenPos = Input.mousePosition;
|
||||
// 将屏幕坐标转换为世界坐标
|
||||
Vector3 mouseWorldPos = mainCamera.ScreenToWorldPoint(new Vector3(mouseScreenPos.x, mouseScreenPos.y, -mainCamera.transform.position.z));
|
||||
mouseWorldPos.z = 0; // 确保z坐标为0
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
if (isPointerHeld)
|
||||
{
|
||||
transform.Rotate(Vector3.forward, -360f * Time.deltaTime);
|
||||
}
|
||||
@@ -391,12 +468,12 @@ public class GearFollower : MonoBehaviour
|
||||
EndCutting();
|
||||
return;
|
||||
}
|
||||
if (!Input.GetMouseButton(0))
|
||||
if (!isPointerHeld)
|
||||
{
|
||||
EndCutting();
|
||||
return;
|
||||
}
|
||||
if (!isCutting && Input.GetMouseButton(0))
|
||||
if (!isCutting && isPointerHeld)
|
||||
{
|
||||
StartCutting();
|
||||
}
|
||||
@@ -835,6 +912,13 @@ public class GearFollower : MonoBehaviour
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (eventTrigger != null)
|
||||
{
|
||||
eventTrigger.UnRegister(EventTriggerType.PointerDown, OnPointerDown);
|
||||
eventTrigger.UnRegister(EventTriggerType.PointerUp, OnPointerUp);
|
||||
}
|
||||
EventSystemEx.Instance?.UnRegister(this);
|
||||
|
||||
// 清理DOTween序列
|
||||
if (cutCompleteSequence != null)
|
||||
{
|
||||
@@ -909,6 +993,10 @@ public class GearFollower : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsActive => !isCutComplete;
|
||||
public bool IsAvailable => !isCutComplete;
|
||||
public GameObject GetGameObject() => gameObject;
|
||||
|
||||
// 停止音效的辅助方法
|
||||
private void StopDrillSfxIfPlaying()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user