MayHeCome/Assets/仪式/C#/BuildRemains.cs
2024-12-18 17:55:34 +08:00

243 lines
8.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class BuildRemains : MonoBehaviour, IPointerDownHandler
{
private Vector3 offset;
private float Zpos = -9f; // 控制物体的 z 轴位置
private delegate void Style();
private Style style;
//public float rayDistance = 2f; // 射线的最大距离
public Vector3 boxSize = new Vector3(1f, 1f, 1f); // 盒子的尺寸
public float detectionDistance = 2f; // 盒子检测的距离
public List<float> distances;
private int CurrentScreen;//当前所在场景index
private float BuffValue;
[Header("效果持续时间-1为永久")]
public float UseTimes;
private Action BuffOpen;
private Action BuffClose;
private bool BuffOk;//buff已经添加上了
Buildings MyBuilds;
private void OnEnable()
{
style = Move;
if (SceneTransitionManager.Instance.CurrentTrackingPointIndex < 3)
{
Zpos = distances[SceneTransitionManager.Instance.CurrentTrackingPointIndex];
CurrentScreen = SceneTransitionManager.Instance.CurrentTrackingPointIndex;
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
switch (CurrentScreen)
{
case 0:
spriteRenderer.sortingLayerName = "Slime";
spriteRenderer.sortingOrder = 1200;
break;
case 1:
spriteRenderer.sortingLayerName = "伐木场";
spriteRenderer.sortingOrder = 30;
break;
case 2:
spriteRenderer.sortingLayerName = "日神";
spriteRenderer.sortingOrder = 5;
break;
default:
break;
}
}
}
private void Update()
{
style?.Invoke();
}
/// <summary>
/// 确定遗物类别
/// </summary>
public void ChangeRemainskind(string remiansname)
{
BuffOk = false;
if (remiansname.StartsWith("天外乐章"))
{
// 判断是哪个版本
if (remiansname == "天外乐章I")
{
BuffValue = 2;
Debug.Log("这是 天外乐章I");
}
else if (remiansname == "天外乐章II")
{
BuffValue = 3;
Debug.Log("这是 天外乐章II");
}
else if (remiansname == "天外乐章III")
{
BuffValue = 5;
Debug.Log("这是 天外乐章III");
}
BuffOpen = () => { if (BuffOk) return; GlobalBuffSystem.Instance.GlobalMultiplier *= BuffValue; BuffOk = true; };
BuffClose = () => { if (!BuffOk) return; GlobalBuffSystem.Instance.GlobalMultiplier /= BuffValue; BuffOk = false; };
UseTimes = 10;
}
else if (remiansname.StartsWith("鼓舞铭文拓本"))
{
if (remiansname == "鼓舞铭文拓本I")
{
BuffValue = 3;
Debug.Log("这是 鼓舞铭文拓本I");
}
else if (remiansname == "鼓舞铭文拓本II")
{
BuffValue = 5;
Debug.Log("这是 鼓舞铭文拓本II");
}
else if (remiansname == "鼓舞铭文拓本III")
{
BuffValue = 10;
Debug.Log("这是 鼓舞铭文拓本III");
}
BuffOpen = GuWuRemainsOpen;
BuffClose = GuWuRemainsClose;
UseTimes = -1;
}
else if (remiansname.StartsWith("信仰铭文"))
{
if (remiansname == "信仰铭文I")
{
BuffValue = 3;
Debug.Log("这是 信仰铭文I");
}
else if (remiansname == "信仰铭文II")
{
BuffValue = 5;
Debug.Log("这是 信仰铭文II");
}
else if (remiansname == "信仰铭文III")
{
BuffValue = 10;
Debug.Log("这是 信仰铭文III");
}
BuffOpen = FaithRemainsOpen;
BuffClose = FaithRewadsClose;
UseTimes = -1;
}
}
private void Move()
{
// 获取当前鼠标的位置并转换为世界坐标,保持物体的 Zpos
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z));
// 更新物体位置为鼠标位置加上偏移量,确保 Z 轴不变
transform.position = new Vector3(mousePosition.x + offset.x, mousePosition.y + offset.y, Zpos);
if (Input.GetMouseButtonUp(0) || SceneTransitionManager.Instance.CurrentTrackingPointIndex != CurrentScreen)
{
style = null;
OnclickEvent();
}
}
private void OnclickEvent()
{
ClickEvent();
}
private void ClickEvent()
{
// 盒子的中心点位于物体自身的位置,并在物体的前方一定距离处
Vector3 boxCenter = transform.position + transform.forward * detectionDistance;
// 绘制可视化线条用于调试(代替 Gizmos 绘制)
Debug.DrawLine(transform.position, boxCenter, Color.green, 2.0f); // 画一条线指向检测位置
// 获取在盒子范围内的所有碰撞体
Collider[] hitColliders = Physics.OverlapBox(boxCenter, boxSize / 2, transform.rotation);
// 遍历所有碰撞体
foreach (Collider hitCollider in hitColliders)
{
Debug.Log("Hit object: " + hitCollider.gameObject.name);
GameObject obj = hitCollider.gameObject;
// 检查物体是否实现了 IPointerDownHandler 接口
if (obj.transform.parent.TryGetComponent(out ContactArea contactArea))
{
Debug.Log("检测到建筑类");
if (UseTimes != -1) style = CutTimes;
MyBuilds = contactArea.buildings;
UsingRemains();
break;
}
}
}
private void CutTimes()
{
UseTimes -= Time.deltaTime;
if (UseTimes < 0 && UseTimes != -1)
{
DisUsingRemains();
Destroy(gameObject);
}
}
public void UsingRemains() //使用遗物效果
{
BuffOpen?.Invoke();
}
public void DisUsingRemains() //取消使用遗物效果
{
BuffClose?.Invoke();
}
private void GuWuRemainsOpen()
{
if (BuffOk) return;
MyBuilds.LocalBuff_Auto += BuffValue;
Debug.LogWarning("鼓舞" + BuffValue + "---" + MyBuilds.LocalBuff_Auto);
BuffOk = true;
}
private void GuWuRemainsClose()
{
if (!BuffOk) return;
MyBuilds.LocalBuff_Auto -= BuffValue;
BuffOk = false;
}
private void FaithRemainsOpen()
{
if (BuffOk) return;
MyBuilds.SpecalBuffAdd_efficiency_Auto += BuffValue;
BuffOk = true;
}
private void FaithRewadsClose()
{
if (!BuffOk) return;
MyBuilds.SpecalBuffAdd_efficiency_Auto -= BuffValue;
BuffOk = false;
}
// 绘制盒子用于调试
private void OnDrawGizmos()
{
// 在编辑器中显示盒子的检测区域
Vector3 boxCenter = transform.position + transform.forward * detectionDistance;
Gizmos.color = Color.red;
Gizmos.DrawWireCube(boxCenter, boxSize);
}
public void OnPointerDown(PointerEventData eventData)
{
DisUsingRemains();
style = Move;
// 获取鼠标点击位置并将其转换为世界坐标,保持物体的 Zpos
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z));
// 计算物体位置与鼠标位置的偏移量
offset = transform.position - mousePosition;
}
}