Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generate Minimap UI
- using FIMSpace.Generating;
- using FIMSpace.Generating.PathFind;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class PHUI_PlanToMinimapUI : MonoBehaviour
- {
- public static PHUI_PlanToMinimapUI Instance;
- public GameObject RoomSquarePrefab;
- public GameObject RoomDoorsPrefab;
- public GameObject PlayerMarkerPrefab;
- [Space(5)]
- public RectTransform MapElementsContainer;
- public ScrollRect ScrollRect;
- [Space(5)]
- public float sat = 0.15f;
- public float val = 0.75f;
- public PlanHelper CurrentPlan { get; private set; }
- public List<GameObject> Generated { get; private set; }
- private float drawScale = 32;
- private void Awake()
- {
- PHUI_PlanToMinimapUI.Instance = this;
- }
- private void OnDrawGizmos()
- {
- PHUI_PlanToMinimapUI.Instance = this;
- }
- private RectTransform playerRect;
- private RectTransform debugRect;
- private Bounds mapBound;
- public Transform debugPos;
- private void Update()
- {
- if (CurrentPlan != null)
- {
- if (playerRect == null)
- {
- GameObject playerUI = Instantiate(PlayerMarkerPrefab);
- playerRect = playerUI.GetComponent<RectTransform>();
- playerRect.SetParent(MapElementsContainer);
- playerRect.anchoredPosition = Vector2Int.zero;
- playerRect.localScale = PlayerMarkerPrefab.transform.localScale;
- }
- if (debugPos)
- {
- if (!debugRect)
- {
- GameObject playerUI = Instantiate(PlayerMarkerPrefab);
- debugRect = playerUI.GetComponent<RectTransform>();
- debugRect.SetParent(MapElementsContainer);
- debugRect.anchoredPosition = Vector2Int.zero;
- debugRect.localScale = PlayerMarkerPrefab.transform.localScale;
- }
- debugRect.anchoredPosition = new Vector2(debugPos.position.x, debugPos.position.z) * .5f * drawScale;
- debugRect.localRotation = Quaternion.Euler(0, 0, -debugPos.eulerAngles.y + 90);
- }
- playerRect.anchoredPosition = new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.z) * .5f * drawScale;
- //playerRect.position = MapElementsContainer.parent.position;
- playerRect.localRotation = Quaternion.Euler(0f, 0f, -Camera.main.transform.eulerAngles.y + 90f);
- Vector2 pWPos = new Vector2(-Camera.main.transform.position.x, -Camera.main.transform.position.z);
- if (mapBound.size == Vector3.zero) mapBound = CurrentPlan.MeasureBounding();
- Vector2 zooms = new Vector2(MapElementsContainer.localScale.x, MapElementsContainer.localScale.z);
- float s = 0.75f;
- Vector3 centerOff = mapBound.center * -4f;
- if (CurrentPlan.PlanPreset.RootChunkSetup.FieldSetup) centerOff = mapBound.center * -CurrentPlan.PlanPreset.RootChunkSetup.FieldSetup.CellSize * 2f;
- if (pWPos.x < (centerOff.x - mapBound.size.x * s) * zooms.x) pWPos.x = (centerOff.x - mapBound.size.x * s) * zooms.x;
- if (pWPos.x > (centerOff.x + mapBound.size.x * s) * zooms.x) pWPos.x = (centerOff.x + mapBound.size.x * s) * zooms.x;
- if (pWPos.y < (centerOff.z - mapBound.size.z * s) * zooms.y) pWPos.y = (centerOff.z - mapBound.size.z * s) * zooms.y;
- if (pWPos.y > (centerOff.z + mapBound.size.z * s) * zooms.y) pWPos.y = (centerOff.z + mapBound.size.z * s) * zooms.y;
- MapElementsContainer.anchoredPosition = (Vector2.Scale(pWPos, zooms)) * .5f * drawScale;
- }
- }
- public float baseDrawScale = 50f;
- public void GenerateMap(PlanHelper plan)
- {
- if (RoomSquarePrefab == null) return;
- if (RoomDoorsPrefab == null) return;
- CurrentPlan = plan;
- drawScale = baseDrawScale;
- ClearGenerated();
- for (int i = 0; i < plan.InteriorRects.Count; i++)
- {
- AddRoom(plan.InteriorRects[i]);
- }
- for (int i = 0; i < plan.ConnectionRects.Count; i++)
- {
- AddDoor(plan.ConnectionRects[i]);
- }
- }
- public GameObject AddRoom(PlanHelper.HelperRect room)
- {
- GameObject r = Instantiate(RoomSquarePrefab);
- RectTransform rect = r.GetComponent<RectTransform>();
- if (room.TypeID == -1)
- {
- r.name = "UI_Room_Corridor";
- }
- else
- {
- TextMeshProUGUI roomName = r.GetComponentInChildren<TextMeshProUGUI>();
- if (roomName)
- {
- roomName.text = CurrentPlan.PlanPreset.Settings[room.TypeID].GetName();
- if (room.Bound.size.z > room.Bound.size.x) roomName.rectTransform.rotation = Quaternion.Euler(0, 0, 90);
- }
- r.name = "UI_Room_" + CurrentPlan.PlanPreset.Settings[room.TypeID].GetName() + " [] BPos: " + room.Bound.center + " Scale: " + room.Bound.size + " Ext: " + room.Bound.extents;
- }
- rect.SetParent(MapElementsContainer);
- rect.localScale = Vector3.one;
- rect.sizeDelta = new Vector2(room.Bound.size.x, room.Bound.size.z) * drawScale;
- if (room.TypeID == -1)
- {
- if (room.Bound.size.z > room.Bound.size.x) rect.sizeDelta = new Vector2(room.Bound.size.x, room.Bound.size.z + 0.27f) * drawScale;
- else rect.sizeDelta = new Vector2(room.Bound.size.x + 0.27f, room.Bound.size.z) * drawScale;
- }
- rect.anchoredPosition = new Vector2(room.Bound.center.x, room.Bound.center.z) * drawScale;
- Image im = r.GetComponent<Image>();
- if (im)
- {
- float br = 0.7f;
- if (room.TypeID == -1)
- im.color = new Color(br, br, br, 1f);
- else
- {
- float step = 1f / (float)CurrentPlan.PlanPreset.Settings.Count;
- im.color = Color.HSVToRGB(room.TypeID * step, sat, val);
- }
- }
- Generated.Add(r);
- return r;
- }
- public GameObject AddDoor(PlanHelper.ConnectionRect connection)
- {
- GameObject r = Instantiate(RoomDoorsPrefab);
- RectTransform rect = r.GetComponent<RectTransform>();
- //Image doors = r.transform.GetChild(0).GetComponent<Image>();
- rect.SetParent(MapElementsContainer);
- rect.localScale = Vector3.one;
- rect.sizeDelta = new Vector2(drawScale * 0.9f, drawScale * 0.2f);
- //doors.rectTransform.sizeDelta = new Vector2(drawScale * 0.85f, drawScale * 0.1f);
- if (connection.direction == EAlignDir.Down)
- {
- rect.rotation = Quaternion.Euler(0, 0, 0);
- //rect.sizeDelta = new Vector2(drawScale * 0.9f, drawScale * 0.2f);
- }
- else if (connection.direction == EAlignDir.Up)
- {
- rect.rotation = Quaternion.Euler(0, 0, 180);
- //rect.sizeDelta = new Vector2(drawScale * 0.2f, drawScale * 0.9f);
- }
- else if (connection.direction == EAlignDir.Right)
- {
- rect.rotation = Quaternion.Euler(0, 0, 90);
- }
- else if (connection.direction == EAlignDir.Left)
- {
- rect.rotation = Quaternion.Euler(0, 0, -90);
- }
- r.name = "PR_UI_Connection_[" + connection.Connection1.TypeID + ":" + connection.Connection2.TypeID + "]";
- rect.anchoredPosition = new Vector2(connection.Bound.center.x, connection.Bound.center.z) * drawScale;
- Image im = r.GetComponent<Image>();
- if (im) im.color = Color.black;
- Generated.Add(r);
- return r;
- }
- public void ClearGenerated()
- {
- if (Generated == null) Generated = new List<GameObject>();
- for (int i = 0; i < Generated.Count; i++)
- {
- if (Generated[i] == null) continue;
- FGenerators.DestroyObject(Generated[i]);
- }
- Generated.Clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment