249 lines
7.4 KiB
C#
249 lines
7.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using System.Collections;
|
|
using AibisDream;
|
|
using AibisDream.FixSystem;
|
|
using AibisDream.Kit;
|
|
|
|
public class HeatMapController : MonoBehaviour
|
|
{
|
|
public Material heatMaterial; // 需要传递数据的材质
|
|
|
|
public Transform[] Pos;
|
|
|
|
public Button coolingButton;
|
|
|
|
private BodyModuleSystem bodyModuleSystem;
|
|
|
|
public GameObject heatmapGameobject;
|
|
|
|
public RectTransform imageToMove; // 需要移动的Image
|
|
public Transform imageToRotate; // 需要旋转的Image
|
|
public Vector3 inPos; // Heatmap进入位置
|
|
public Vector3 outPos; // Heatmap退出位置
|
|
|
|
|
|
private Vector3 imageInitialPos; // Image的初始位置
|
|
|
|
[HideInInspector] private Vector4[] points = new Vector4[7]; // 存储6个点的数组,(x, y, z) = center, w = temperature
|
|
|
|
[HideInInspector] private Vector4[] properties = new Vector4[7]; // 存储6个盒子大小的数组, (x,y) = boxsize
|
|
|
|
//public Slider slider;
|
|
private float tempNum = 0;
|
|
|
|
private float uftempture = 1;
|
|
private float backtemture = 1;
|
|
|
|
private float modulesTempture = 1;
|
|
|
|
//public float uf=1;
|
|
//public float back=1;
|
|
|
|
//public float module=0.7f;
|
|
//public Texture2D hotspotShapeTexture; // 引用热点形状纹理
|
|
|
|
private float tempture = 1;
|
|
|
|
//public GameObject heatMap;
|
|
public float Tempture
|
|
{
|
|
get => tempture;
|
|
set
|
|
{
|
|
if (tempture != value)
|
|
{
|
|
tempture = value;
|
|
SetDataToMaterial(); // 调用方法更新材质数据
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowHeatMap()
|
|
{
|
|
StartCoroutine(ShowHeatMapCoroutine());
|
|
}
|
|
|
|
private IEnumerator ShowHeatMapCoroutine()
|
|
{
|
|
// 初始化Heatmap位置
|
|
|
|
heatmapGameobject.transform.localPosition = outPos;
|
|
heatmapGameobject.SetActive(true);
|
|
heatmapGameobject.transform.DOLocalMove(inPos, 1f);
|
|
yield return new WaitForSeconds(1f); // 等待动画完成
|
|
heatmapGameobject.GetComponent<DraggableUI>().UnlockDrag();
|
|
coolingButton.interactable = false;
|
|
}
|
|
|
|
public void HideHeatMap()
|
|
{
|
|
var draggableUI = heatmapGameobject.GetComponent<DraggableUI>();
|
|
heatmapGameobject.transform.DOLocalMove(outPos, 1f).OnComplete(() => { draggableUI.UnlockDrag(); heatmapGameobject.SetActive(false);});
|
|
|
|
}
|
|
|
|
public void StartCooling()
|
|
{
|
|
coolingButton.interactable = false;
|
|
StartCoroutine(ReduceValueCoroutine());
|
|
}
|
|
|
|
IEnumerator ReduceValueCoroutine()
|
|
{
|
|
var draggableUI = heatmapGameobject.GetComponent<DraggableUI>();
|
|
draggableUI.LockDrag();
|
|
|
|
// 存储初始位置
|
|
imageInitialPos = imageToMove.localPosition;
|
|
|
|
// 移动和旋转动画
|
|
imageToMove.DOLocalMoveX(imageInitialPos.x + 3.5f, 1f);
|
|
var rotationTween = imageToRotate.DORotate(new Vector3(0, 0, 360), 0.2f, RotateMode.FastBeyond360)
|
|
.SetLoops(-1, LoopType.Restart)
|
|
.SetEase(Ease.Linear);
|
|
|
|
if (Tempture < 2)
|
|
{
|
|
Debug.Log("降温成功");
|
|
float duration = 3.5f;
|
|
SetTemptureSmooth(0.5f, duration);
|
|
AudioManager.Instance.PlaySfx("event:/FollowInput/cooler_cooling");
|
|
yield return new WaitForSeconds(duration);
|
|
rotationTween.Kill();
|
|
imageToRotate.localRotation = Quaternion.identity;
|
|
// 恢复初始状态
|
|
imageToMove.DOLocalMove(imageInitialPos, 1f);
|
|
yield return new WaitForSeconds(1.5f);
|
|
DialogController.Instance.StartDialogNode("降温成功");
|
|
bodyModuleSystem.OnCooling();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("降温失败");
|
|
yield return new WaitForSeconds(3f);
|
|
DialogController.Instance.StartDialogNode("降温失败");
|
|
}
|
|
|
|
// 停止旋转
|
|
rotationTween.Kill();
|
|
imageToRotate.localRotation = Quaternion.identity;
|
|
|
|
// 恢复初始状态
|
|
imageToMove.DOLocalMove(imageInitialPos, 1f);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
bodyModuleSystem = FindObjectOfType<BodyModuleSystem>();
|
|
// // 初始调用,设置初始位置
|
|
//tempture=1;
|
|
// // 初始化数据,这里是示例,你可以根据实际需求设置
|
|
SetDataToMaterial();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 热点尺寸:优先 SpriteRenderer,其次 UI Image,最后用 RectTransform 尺寸。
|
|
/// </summary>
|
|
private static bool TryGetHotspotSize(Transform t, out Vector2 size)
|
|
{
|
|
size = default;
|
|
if (t == null) return false;
|
|
|
|
var sr = t.GetComponent<SpriteRenderer>();
|
|
if (sr != null && sr.sprite != null)
|
|
{
|
|
var b = sr.sprite.bounds.size;
|
|
size = new Vector2(b.x, b.y);
|
|
return true;
|
|
}
|
|
|
|
var image = t.GetComponent<Image>();
|
|
if (image != null && image.sprite != null)
|
|
{
|
|
var b = image.sprite.bounds.size;
|
|
size = new Vector2(b.x, b.y);
|
|
return true;
|
|
}
|
|
|
|
if (t is RectTransform rt)
|
|
{
|
|
size = new Vector2(rt.rect.width, rt.rect.height);
|
|
return size.x > 0f || size.y > 0f;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void SetDataToMaterial()
|
|
{
|
|
if (tempture >= 0 && tempture <= 1)
|
|
{
|
|
uftempture = Mathf.Lerp(2.0f, 2.8f, tempture); // 线性插值
|
|
modulesTempture = Mathf.Lerp(1.2f, 1.6f, tempture);
|
|
backtemture = Mathf.Lerp(1.5f, 2.25f, tempture);
|
|
}
|
|
else if (tempture >= 1 && tempture <= 2)
|
|
{
|
|
uftempture = Mathf.Lerp(2.8f, 3.2f, tempture - 1);
|
|
modulesTempture = Mathf.Lerp(1.8f, 2.3f, tempture - 1);
|
|
backtemture = Mathf.Lerp(2.25f, 3.4f, tempture - 1);
|
|
}
|
|
// else if (tempture >= 2)
|
|
// {
|
|
// uftempture =3.2f;
|
|
// modulesTempture = 2.6f;
|
|
// backtemture = 3.8f;
|
|
// }
|
|
|
|
|
|
if (Pos == null || heatMaterial == null)
|
|
return;
|
|
|
|
for (int i = 0; i < 7; i++)
|
|
{
|
|
if (Pos[i] == null)
|
|
{
|
|
points[i] = Vector4.zero;
|
|
properties[i] = Vector4.zero;
|
|
continue;
|
|
}
|
|
|
|
Transform currentTransform = Pos[i].GetComponent<Transform>();
|
|
|
|
if (i == 0)
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, uftempture, 0);
|
|
}
|
|
else if (i == 6)
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, backtemture, 0);
|
|
}
|
|
else
|
|
{
|
|
points[i] = new Vector4(Pos[i].position.x, Pos[i].position.y, modulesTempture,
|
|
0); // (x, y, z, temperature)
|
|
}
|
|
|
|
if (!TryGetHotspotSize(currentTransform, out var boxSize))
|
|
boxSize = Vector2.one;
|
|
|
|
properties[i] = new Vector4(boxSize.x, boxSize.y, 0.0f, 0.0f);
|
|
}
|
|
|
|
// 将数据传递给 Shader
|
|
heatMaterial.SetVectorArray("_Points", points);
|
|
heatMaterial.SetVectorArray("_Properties", properties);
|
|
}
|
|
|
|
public void SetTemptureDirect(float value)
|
|
{
|
|
Tempture = value;
|
|
}
|
|
|
|
public void SetTemptureSmooth(float targetValue, float duration)
|
|
{
|
|
DOTween.To(() => tempture, x => Tempture = x, targetValue, duration);
|
|
}
|
|
} |