131 lines
3.6 KiB
C#
131 lines
3.6 KiB
C#
using System;
|
|
using AibisDream.Framework;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class PipelineProduct : MonoBehaviour, IInteraction
|
|
{
|
|
private static readonly int AddColorFade = Shader.PropertyToID("_AddColorFade");
|
|
|
|
private EventTriggerEx _trigger;
|
|
private SpriteRenderer _renderer;
|
|
private Material _material;
|
|
private PipelineSystem _pipelineSystem;
|
|
|
|
private float _speed;
|
|
private Transform[] _wayPoints;
|
|
private int _curIndex;
|
|
private bool _isActive;
|
|
|
|
public CheckData CheckData { get; private set; }
|
|
|
|
public event Action<PipelineProduct, bool> OnRecycled;
|
|
|
|
private void Awake()
|
|
{
|
|
_trigger = GetComponent<EventTriggerEx>();
|
|
_renderer = GetComponent<SpriteRenderer>();
|
|
_material = _renderer.material;
|
|
|
|
_trigger.Register(EventTriggerType.PointerEnter, OnPointerEnter);
|
|
_trigger.Register(EventTriggerType.PointerExit, OnPointerExit);
|
|
_trigger.Register(EventTriggerType.PointerDown, OnPointerDown);
|
|
}
|
|
|
|
public void Init(Transform[] wayPoints, CheckData checkData, PipelineSystem pipelineSystem)
|
|
{
|
|
gameObject.SetActive(true);
|
|
_isActive = true;
|
|
|
|
_pipelineSystem = pipelineSystem;
|
|
|
|
transform.position = wayPoints[0].position;
|
|
_wayPoints = wayPoints;
|
|
|
|
CheckData = checkData;
|
|
_speed = checkData.speed;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Move();
|
|
}
|
|
|
|
private void Move()
|
|
{
|
|
if (!_isActive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 判断是否到达当前目标点
|
|
if (Vector3.Distance(transform.position, _wayPoints[_curIndex].position) < 0.05f)
|
|
{
|
|
_curIndex++;
|
|
if (_curIndex >= _wayPoints.Length)
|
|
{
|
|
// 自我销毁并计数
|
|
OnRecycled?.Invoke(this, false);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 移动到当前目标点
|
|
transform.position = Vector3.MoveTowards(transform.position, _wayPoints[_curIndex].position,
|
|
Time.deltaTime * _speed);
|
|
}
|
|
|
|
public void CheckPass(bool matchRes)
|
|
{
|
|
OnRecycled?.Invoke(this, matchRes);
|
|
}
|
|
|
|
private void OnPointerEnter(BaseEventData eventData)
|
|
{
|
|
// TODO 增加悬浮效果
|
|
transform.localScale = Vector3.one * 1.1f;
|
|
}
|
|
|
|
private void OnPointerExit(BaseEventData eventData)
|
|
{
|
|
// TODO 消除悬浮效果
|
|
transform.localScale = Vector3.one;
|
|
}
|
|
|
|
private void OnPointerDown(BaseEventData eventData)
|
|
{
|
|
// TODO 增加点击效果
|
|
_pipelineSystem.Selected(this);
|
|
_material.SetFloat(AddColorFade, 1);
|
|
}
|
|
|
|
public void Deselect()
|
|
{
|
|
_material.SetFloat(AddColorFade, 0);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_material.SetFloat(AddColorFade, 0);
|
|
transform.localScale = Vector3.one;
|
|
gameObject.SetActive(false);
|
|
_isActive = false;
|
|
_curIndex = 0;
|
|
OnRecycled = null;
|
|
}
|
|
|
|
#region 交互接口
|
|
|
|
public bool IsActive => true;
|
|
public bool IsAvailable => gameObject.activeInHierarchy;
|
|
|
|
public GameObject GetGameObject()
|
|
{
|
|
return gameObject;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |