using Managers; using Prefab; using System.Collections.Generic; using Base; 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; public static Vector3 minimum = new(0.5f, 0.5f, 0.5f); public bool CanShow => Setting.Instance.CurrentSettings.developerMode && entity.canSelect; public virtual void Init() { outlineRenderer.size = GetBodyTextureSize(); var size = GetColliderSize(); outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical; outlineCollider.size = size; outlineCollider.offset = GetOffset(); 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; } /// /// 获取指定对象及其所有子对象组成的图像的大小。 /// /// /// 返回一个 Vector3 对象,表示对象在世界空间中的总大小(宽度、高度、深度)。 /// 如果没有找到任何渲染器,则返回 (-1, -1, -1) 表示无效大小。 /// public Vector2 GetColliderSize() { return !string.IsNullOrEmpty(entity.entityDef.colliderSize) ? Utils.StringUtils.StringToVector2(entity.entityDef.colliderSize) : GetBodyTextureSize(); } public Vector2 GetBodyTextureSize() { // 获取所有子对象的 Renderer 组件 var renderers = body.GetComponentsInChildren(); // 如果没有找到任何 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; } public Vector2 GetOffset() { return string.IsNullOrEmpty(entity.entityDef.colliderPosition) ? Vector2.zero : Utils.StringUtils.StringToVector2(entity.entityDef.colliderPosition); } protected virtual void OnMouseEnter() { if (!CanShow) return; Show(); } protected virtual void OnMouseExit() { Hide(); } protected virtual void OnMouseOver() { if (!Program.Instance.CanOpenRightMenu || !CanShow) return; // 检测是否按下的是鼠标右键 if (Input.GetMouseButtonDown(1)) { RightMenuManager.GenerateRightMenu(GetMenu(), Input.mousePosition); } } protected virtual List<(string name, UnityAction callback)> GetMenu() { var result = new List<(string name, UnityAction callback)>(); if (entity.PlayerControlled) result.Add(("结束操控", EndControl)); else result.Add(("手动操控", StartControl)); result.Add(("杀死", () => entity.Kill())); result.Add(("变成笨蛋", BecomeDefault)); return result; } protected void BecomeDefault() { entity.Kill(); EntityManager.Instance.GenerateDefaultEntity(Program.Instance.FocusedDimensionId, entity.Position); } protected void StartControl() { entity.PlayerControlled = true; } protected void EndControl() { entity.PlayerControlled = false; } } }