88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using AZTECHWB.Core;
|
|
using AZTECHWB.Management;
|
|
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AZTECHWB.UI
|
|
{
|
|
public class MiniMap : UIPanel
|
|
{
|
|
Building building;
|
|
GameObject target;
|
|
|
|
private Image MiniMapScreen;
|
|
private RectTransform Image_MiniMapIcon;
|
|
|
|
public Vector2 worldPos;
|
|
|
|
public Vector2 worldMin;
|
|
public Vector2 worldMax;
|
|
public float offset;
|
|
|
|
public Action onClickCloseButton;
|
|
|
|
[SerializeField] Sprite[] miniMapImages;
|
|
|
|
public override async UniTask Init()
|
|
{
|
|
building = AZTECHSceneMain.Instance.building;
|
|
target = AZTECHAppMain.Instance.cameraController.cameraPivot.gameObject;
|
|
|
|
MiniMapScreen = GetElement<Image>(nameof(MiniMapScreen));
|
|
Image_MiniMapIcon = GetElement<RectTransform>(nameof(Image_MiniMapIcon));
|
|
|
|
await UniTask.CompletedTask;
|
|
}
|
|
public void SetAcitveMiniMap(bool isOn)
|
|
{
|
|
if (isOn)
|
|
{
|
|
Open();
|
|
}
|
|
else
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
var topMenuPanel = AZTECHSceneMain.Instance.GetManager<AZTECHUIManager>().GetCanvas<StaticCanvas>().GetPanel<LeftSidePanel>();
|
|
topMenuPanel.SetToggleButton("button_minimap_screen", false);
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateMiniMapBackground();
|
|
UpdateMiniMapIcon();
|
|
}
|
|
private void UpdateMiniMapBackground()
|
|
{
|
|
var sprite = building.isOnlyMachineFloorsEnabled ? miniMapImages[1] : miniMapImages[0];
|
|
MiniMapScreen.sprite = sprite;
|
|
}
|
|
|
|
void UpdateMiniMapIcon()
|
|
{
|
|
Vector2 worldPos = new Vector2(target.transform.position.x, target.transform.position.z);
|
|
Vector2 normalizedPos = (worldPos - worldMin) / (worldMax - worldMin);
|
|
|
|
RectTransform miniMapRect = MiniMapScreen.GetComponent<RectTransform>();
|
|
Vector2 miniMapSize = miniMapRect.rect.size;
|
|
Vector2 iconPos = new Vector2(
|
|
-((normalizedPos.x * miniMapSize.x) - (miniMapSize.x * 0.5f)),
|
|
-((normalizedPos.y * miniMapSize.y) - (miniMapSize.y * 0.5f))
|
|
);
|
|
|
|
Image_MiniMapIcon.anchoredPosition = iconPos;
|
|
|
|
float cameraRotateY = Camera.main.transform.eulerAngles.y;
|
|
Image_MiniMapIcon.localEulerAngles = new Vector3(0, 0, -cameraRotateY + offset);
|
|
}
|
|
}
|
|
}
|
|
|