Guest User

Untitled

a guest
Oct 17th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.09 KB | None | 0 0
  1. // Generate Minimap UI
  2. using FIMSpace.Generating;
  3. using FIMSpace.Generating.PathFind;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10.  
  11. public class PHUI_PlanToMinimapUI : MonoBehaviour
  12. {
  13.     public static PHUI_PlanToMinimapUI Instance;
  14.  
  15.     public GameObject RoomSquarePrefab;
  16.     public GameObject RoomDoorsPrefab;
  17.     public GameObject PlayerMarkerPrefab;
  18.  
  19.     [Space(5)]
  20.     public RectTransform MapElementsContainer;
  21.     public ScrollRect ScrollRect;
  22.  
  23.     [Space(5)]
  24.     public float sat = 0.15f;
  25.     public float val = 0.75f;
  26.  
  27.     public PlanHelper CurrentPlan { get; private set; }
  28.     public List<GameObject> Generated { get; private set; }
  29.  
  30.     private float drawScale = 32;
  31.  
  32.     private void Awake()
  33.     {
  34.         PHUI_PlanToMinimapUI.Instance = this;
  35.     }
  36.  
  37.     private void OnDrawGizmos()
  38.     {
  39.         PHUI_PlanToMinimapUI.Instance = this;
  40.     }
  41.  
  42.     private RectTransform playerRect;
  43.     private RectTransform debugRect;
  44.     private Bounds mapBound;
  45.     public Transform debugPos;
  46.     private void Update()
  47.     {
  48.         if (CurrentPlan != null)
  49.         {
  50.             if (playerRect == null)
  51.             {
  52.                 GameObject playerUI = Instantiate(PlayerMarkerPrefab);
  53.                 playerRect = playerUI.GetComponent<RectTransform>();
  54.                 playerRect.SetParent(MapElementsContainer);
  55.                 playerRect.anchoredPosition = Vector2Int.zero;
  56.                 playerRect.localScale = PlayerMarkerPrefab.transform.localScale;
  57.             }
  58.  
  59.             if (debugPos)
  60.             {
  61.                 if (!debugRect)
  62.                 {
  63.                     GameObject playerUI = Instantiate(PlayerMarkerPrefab);
  64.                     debugRect = playerUI.GetComponent<RectTransform>();
  65.                     debugRect.SetParent(MapElementsContainer);
  66.                     debugRect.anchoredPosition = Vector2Int.zero;
  67.                     debugRect.localScale = PlayerMarkerPrefab.transform.localScale;
  68.                 }
  69.  
  70.                 debugRect.anchoredPosition = new Vector2(debugPos.position.x, debugPos.position.z) * .5f * drawScale;
  71.                 debugRect.localRotation = Quaternion.Euler(0, 0, -debugPos.eulerAngles.y + 90);
  72.             }
  73.  
  74.             playerRect.anchoredPosition = new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.z) * .5f * drawScale;
  75.             //playerRect.position = MapElementsContainer.parent.position;
  76.             playerRect.localRotation = Quaternion.Euler(0f, 0f, -Camera.main.transform.eulerAngles.y + 90f);
  77.  
  78.             Vector2 pWPos = new Vector2(-Camera.main.transform.position.x, -Camera.main.transform.position.z);
  79.  
  80.             if (mapBound.size == Vector3.zero) mapBound = CurrentPlan.MeasureBounding();
  81.  
  82.             Vector2 zooms = new Vector2(MapElementsContainer.localScale.x, MapElementsContainer.localScale.z);
  83.             float s = 0.75f;
  84.             Vector3 centerOff = mapBound.center * -4f;
  85.             if (CurrentPlan.PlanPreset.RootChunkSetup.FieldSetup) centerOff = mapBound.center * -CurrentPlan.PlanPreset.RootChunkSetup.FieldSetup.CellSize * 2f;
  86.  
  87.             if (pWPos.x < (centerOff.x - mapBound.size.x * s) * zooms.x) pWPos.x = (centerOff.x - mapBound.size.x * s) * zooms.x;
  88.             if (pWPos.x > (centerOff.x + mapBound.size.x * s) * zooms.x) pWPos.x = (centerOff.x + mapBound.size.x * s) * zooms.x;
  89.  
  90.             if (pWPos.y < (centerOff.z - mapBound.size.z * s) * zooms.y) pWPos.y = (centerOff.z - mapBound.size.z * s) * zooms.y;
  91.             if (pWPos.y > (centerOff.z + mapBound.size.z * s) * zooms.y) pWPos.y = (centerOff.z + mapBound.size.z * s) * zooms.y;
  92.  
  93.             MapElementsContainer.anchoredPosition = (Vector2.Scale(pWPos, zooms)) * .5f * drawScale;
  94.         }
  95.     }
  96.  
  97.     public float baseDrawScale = 50f;
  98.     public void GenerateMap(PlanHelper plan)
  99.     {
  100.         if (RoomSquarePrefab == null) return;
  101.         if (RoomDoorsPrefab == null) return;
  102.  
  103.         CurrentPlan = plan;
  104.         drawScale = baseDrawScale;
  105.  
  106.         ClearGenerated();
  107.  
  108.         for (int i = 0; i < plan.InteriorRects.Count; i++)
  109.         {
  110.             AddRoom(plan.InteriorRects[i]);
  111.         }
  112.  
  113.         for (int i = 0; i < plan.ConnectionRects.Count; i++)
  114.         {
  115.             AddDoor(plan.ConnectionRects[i]);
  116.         }
  117.     }
  118.  
  119.     public GameObject AddRoom(PlanHelper.HelperRect room)
  120.     {
  121.         GameObject r = Instantiate(RoomSquarePrefab);
  122.         RectTransform rect = r.GetComponent<RectTransform>();
  123.  
  124.         if (room.TypeID == -1)
  125.         {
  126.             r.name = "UI_Room_Corridor";
  127.         }
  128.         else
  129.         {
  130.             TextMeshProUGUI roomName = r.GetComponentInChildren<TextMeshProUGUI>();
  131.             if (roomName)
  132.             {
  133.                 roomName.text = CurrentPlan.PlanPreset.Settings[room.TypeID].GetName();
  134.                 if (room.Bound.size.z > room.Bound.size.x) roomName.rectTransform.rotation = Quaternion.Euler(0, 0, 90);
  135.             }
  136.  
  137.             r.name = "UI_Room_" + CurrentPlan.PlanPreset.Settings[room.TypeID].GetName() + " [] BPos: " + room.Bound.center + " Scale: " + room.Bound.size + " Ext: " + room.Bound.extents;
  138.         }
  139.  
  140.         rect.SetParent(MapElementsContainer);
  141.         rect.localScale = Vector3.one;
  142.  
  143.         rect.sizeDelta = new Vector2(room.Bound.size.x, room.Bound.size.z) * drawScale;
  144.  
  145.         if (room.TypeID == -1)
  146.         {
  147.             if (room.Bound.size.z > room.Bound.size.x) rect.sizeDelta = new Vector2(room.Bound.size.x, room.Bound.size.z + 0.27f) * drawScale;
  148.             else rect.sizeDelta = new Vector2(room.Bound.size.x + 0.27f, room.Bound.size.z) * drawScale;
  149.         }
  150.  
  151.         rect.anchoredPosition = new Vector2(room.Bound.center.x, room.Bound.center.z) * drawScale;
  152.  
  153.         Image im = r.GetComponent<Image>();
  154.         if (im)
  155.         {
  156.             float br = 0.7f;
  157.             if (room.TypeID == -1)
  158.                 im.color = new Color(br, br, br, 1f);
  159.             else
  160.             {
  161.                 float step = 1f / (float)CurrentPlan.PlanPreset.Settings.Count;
  162.                 im.color = Color.HSVToRGB(room.TypeID * step, sat, val);
  163.             }
  164.         }
  165.  
  166.         Generated.Add(r);
  167.         return r;
  168.     }
  169.  
  170.     public GameObject AddDoor(PlanHelper.ConnectionRect connection)
  171.     {
  172.         GameObject r = Instantiate(RoomDoorsPrefab);
  173.         RectTransform rect = r.GetComponent<RectTransform>();
  174.         //Image doors = r.transform.GetChild(0).GetComponent<Image>();
  175.  
  176.         rect.SetParent(MapElementsContainer);
  177.         rect.localScale = Vector3.one;
  178.  
  179.         rect.sizeDelta = new Vector2(drawScale * 0.9f, drawScale * 0.2f);
  180.         //doors.rectTransform.sizeDelta = new Vector2(drawScale * 0.85f, drawScale * 0.1f);
  181.  
  182.         if (connection.direction == EAlignDir.Down)
  183.         {
  184.             rect.rotation = Quaternion.Euler(0, 0, 0);
  185.             //rect.sizeDelta = new Vector2(drawScale * 0.9f, drawScale * 0.2f);
  186.         }
  187.         else if (connection.direction == EAlignDir.Up)
  188.         {
  189.             rect.rotation = Quaternion.Euler(0, 0, 180);
  190.             //rect.sizeDelta = new Vector2(drawScale * 0.2f, drawScale * 0.9f);
  191.         }
  192.         else if (connection.direction == EAlignDir.Right)
  193.         {
  194.             rect.rotation = Quaternion.Euler(0, 0, 90);
  195.         }
  196.         else if (connection.direction == EAlignDir.Left)
  197.         {
  198.             rect.rotation = Quaternion.Euler(0, 0, -90);
  199.         }
  200.  
  201.         r.name = "PR_UI_Connection_[" + connection.Connection1.TypeID + ":" + connection.Connection2.TypeID + "]";
  202.  
  203.         rect.anchoredPosition = new Vector2(connection.Bound.center.x, connection.Bound.center.z) * drawScale;
  204.  
  205.         Image im = r.GetComponent<Image>();
  206.         if (im) im.color = Color.black;
  207.  
  208.         Generated.Add(r);
  209.  
  210.         return r;
  211.     }
  212.  
  213.     public void ClearGenerated()
  214.     {
  215.         if (Generated == null) Generated = new List<GameObject>();
  216.  
  217.         for (int i = 0; i < Generated.Count; i++)
  218.         {
  219.             if (Generated[i] == null) continue;
  220.             FGenerators.DestroyObject(Generated[i]);
  221.         }
  222.  
  223.         Generated.Clear();
  224.     }
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment