Files
ft/Client/Assets/Scripts/EventMowForTreasure/UIPanel/EventMowForTreasure_ToolPropCell.cs
2026-06-29 21:18:33 +08:00

155 lines
5.2 KiB
C#

using asap.core;
using cfg;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace EventMowForTreasure
{
public class EventMowForTreasure_ToolPropCell:MonoBehaviour,IDragHandler,IEndDragHandler,IPointerClickHandler
{
private Button _addButton;
private TMP_Text _numText;
private Image _image;
private EventMowForTreasureTool _toolConfig;
private EventMowForTreasureManager _manager;
private EventMowForTreasureManager Manager =>_manager??= GContext.container.Resolve<EventMowForTreasureManager>();
private EventMowForTreasureAct _act;
private EventMowForTreasureToolCell _toolCell;
#region ui
private void Awake()
{
_addButton = GetComponentInChildren<Button>();
_addButton.onClick.AddListener(OnClickAddButton);
_numText = GetComponentInChildren<TMP_Text>();
_image =gameObject.FindChildGameObject("collect_icon").GetComponent<Image>();
if(_act==null)
_act=(EventMowForTreasureAct)GContext.container.ResolveAct(Manager.GetCurrentStageAct());
InitUI();
}
private void InitUI()
{
_numText.text = string.Empty;
}
public void SetData(EventMowForTreasureTool toolConfig)
{
this._toolConfig = toolConfig;
ShowUI();
}
private void ShowUI()
{
var isNull=_toolConfig==null;
gameObject.SetActive(!isNull);
if(isNull) return;
Manager.TryGetToolAmount(_toolConfig.ID, out var amount);
_numText.text = $"{amount}";
_ =GContext.container.Resolve<IUIService>().SetImageSprite(_image,_toolConfig.Icon);
}
#endregion
private void OnClickAddButton()
{
if (_toolConfig == null) return;
if (Manager.IsDefaultTool(_toolConfig.ID))
{
Manager.ShowFestPackPanel();
return;
}
Manager.ShowExchangeToolPanel(_toolConfig.ID);
}
private EventMowForTreasureObstacleCell _obstacleCell;
public void OnDrag(PointerEventData eventData)
{
if(_toolConfig==null) return;
var toolCell = GetToolCell();
if(toolCell==null) return;
if(_act==null) return;
_act.RevertChangedObstacle();
var halfWidth = Screen.width / 2;
float offset;
if (eventData.position.x > halfWidth)
offset = -(eventData.position.x - halfWidth) /halfWidth;
else
offset = (halfWidth-eventData.position.x) / halfWidth;
RectTransformUtility.ScreenPointToWorldPointInRectangle((RectTransform)this.transform,
eventData.position,
Camera.main,
out var worldPos);
toolCell.SetRotation(offset);
toolCell.transform.position = worldPos;
toolCell.gameObject.SetActive(true);
toolCell.PlayShowTimeLine();
SendRaycastHit(eventData);
GContext.Publish(new EventMowForTreasureGuideRefresh());
}
private void SendRaycastHit(PointerEventData eventData)
{
var ray= RectTransformUtility.ScreenPointToRay(Camera.main, eventData.position);
if(Physics.Raycast(ray, out RaycastHit hit, 100f))
{
var cell=hit.transform.gameObject.GetComponent<EventMowForTreasureObstacleCell>();
if(cell==null)
return;
_obstacleCell = cell;
_act.ShowSelectedObstacleByTool(_toolConfig,cell);
#if UNITY_EDITOR
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 1f);
#endif
return;
}
_obstacleCell = null;
}
public void OnEndDrag(PointerEventData eventData)
{
var cell = GetToolCell();
cell?.gameObject.SetActive(false);
cell?.StopTimeLine();
if(_toolConfig==null) return;
if(_act==null) return;
_act.RevertChangedObstacle();
if(_obstacleCell==null) return;
if(Manager.IsDefaultTool(_toolConfig.ID))
{
if(_obstacleCell.GetData().IsBroke) return;
_obstacleCell.OnMouseDown();
return;
}
_act.BreakObstacle(_toolConfig,_obstacleCell);
GContext.Publish(new EventMowForTreasureGuideRefresh());
}
private EventMowForTreasureToolCell GetToolCell()
{
if (_toolCell != null)
return _toolCell;
if(_toolConfig==null) return null;
if(_act==null) return null;
if(!Manager.TryGetToolAmount(_toolConfig.ID, out var amount))
return null;
return amount<=0 ? null : _act.GetToolCell(_toolConfig.ID);
}
public void OnPointerClick(PointerEventData eventData)
{
if(eventData.dragging)return;
OnClickAddButton();
}
}
}