Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Entity/Outline.cs

144 lines
4.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Managers;
using Prefab;
using UnityEngine;
using UnityEngine.Events;
namespace Entity
{
public class Outline : MonoBehaviour
{
public RightMenuPrefab rightMenuPrefab;
public GameObject body;
public SpriteRenderer outlineRenderer;
public CapsuleCollider2D outlineCollider;
public ProgressBarPrefab progressBarPrefab;
public Entity entity;
private bool _select = false;
public static Vector3 minimum=new(0.5f,0.5f,0.5f);
public virtual void Init()
{
var size = GetSize();
outlineRenderer.size = size;
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
outlineCollider.size = size;
if (progressBarPrefab)
{
progressBarPrefab.transform.localPosition += new Vector3(0f, size.y * 2 / 3, 0f);
progressBarPrefab.transform.localScale = new Vector3(size.x, 1f / 10f, 1);
}
}
public void Show()
{
outlineRenderer.enabled = true;
}
public void Hide()
{
outlineRenderer.enabled = false;
}
/// <summary>
/// 获取指定对象及其所有子对象组成的图像的大小。
/// </summary>
/// <returns>
/// 返回一个 Vector3 对象,表示对象在世界空间中的总大小(宽度、高度、深度)。
/// 如果没有找到任何渲染器,则返回 (-1, -1, -1) 表示无效大小。
/// </returns>
public Vector3 GetSize()
{
// 获取所有子对象的 Renderer 组件
var renderers = body.GetComponentsInChildren<Renderer>();
// 如果没有找到任何 Renderer返回一个默认值 (-1, -1, -1)
if (renderers.Length == 0)
{
return minimum;
}
// 初始化 totalBounds 为第一个 Renderer 的 bounds
var totalBounds = renderers[0].bounds;
// 遍历剩余的 Renderer将它们的 bounds 合并到 totalBounds 中
for (var i = 1; i < renderers.Length; i++)
{
totalBounds.Encapsulate(renderers[i].bounds);
}
// 获取合并后的包围盒的大小
var size = totalBounds.size;
// 确保每个维度的大小都不小于 0.5
size.x = Mathf.Max(size.x, 0.5f);
size.y = Mathf.Max(size.y, 0.5f);
size.z = Mathf.Max(size.z, 0.5f);
return size;
}
private void OnMouseEnter()
{
Show();
}
private void OnMouseExit()
{
Hide();
}
private void OnMouseOver()
{
if (!entity.canSelect)
return;
// 检测是否按下的是鼠标右键
if (Input.GetMouseButtonDown(1)) // 鼠标右键对应的是按钮索引 1
{
RightMenuManager.GenerateRightMenu(GetMenu(), Input.mousePosition);
}
}
private List<(string name, UnityAction callback)> GetMenu()
{
var result = new List<(string name, UnityAction callback)>();
if (entity.PlayerControlled)
result.Add(("结束操控", EndControl));
else
result.Add(("手动操控", StartControl));
if (CameraControl.CameraControl.Instance.focusedEntity == entity)
{
result.Add(("取消跟随", ()=>CameraControl.CameraControl.Instance.focusedEntity=null));
}
else
{
result.Add(("视角跟随", ()=>CameraControl.CameraControl.Instance.focusedEntity=entity));
}
result.Add(("杀死", () => entity.Kill()));
result.Add(("变成笨蛋", BecomeDefault));
return result;
}
private void BecomeDefault()
{
entity.Kill();
Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position);
}
private void StartControl()
{
entity.PlayerControlled = true;
CameraControl.CameraControl.Instance.focusedEntity=entity;
}
private void EndControl()
{
entity.PlayerControlled = false;
CameraControl.CameraControl.Instance.focusedEntity=null;
}
}
}