Created
February 11, 2022 17:50
-
-
Save juliolitwin/747dd82619e15267400e672b8468e8a0 to your computer and use it in GitHub Desktop.
Ragnarok Minimap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Assets.Scripts.MapEditor; | |
using Poring.Shared.Extensions; | |
using TMPro; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class MinimapUI : MonoBehaviour | |
{ | |
private const int DEFAULT_ZOOM_INDEX = 0; | |
private string m_strMap; | |
private Mask m_maskMinimap; | |
private Image m_imageMiniMap; | |
private Image m_imagePlayer; | |
private TextMeshProUGUI m_txtMapName; | |
private TextMeshProUGUI m_txtCoordinate; | |
private Button m_btnPlus; | |
private Button m_btnMinus; | |
private float m_flMaskWidth = 0.0f; | |
private float m_flMaskHeight = 0.0f; | |
[SerializeField] | |
private int m_nZoom = DEFAULT_ZOOM_INDEX; | |
[SerializeField] | |
private int m_nCenterX = 0; | |
[SerializeField] | |
private int m_nCenterY = 0; | |
[SerializeField] | |
private float m_fOffsetX = 0.0f; | |
[SerializeField] | |
private float m_fOffsetY = 0.0f; | |
private float[] m_fZoomValue = | |
{ | |
0.25f, | |
0.32f, | |
0.425f, | |
0.75f, | |
1.0f | |
}; | |
private float m_fMapPicRealWidht = 0.0f; | |
private float m_fMapPicRealHeight = 0.0f; | |
private int m_nMapWidth; | |
private int m_nMapHeight; | |
//--------------------------------------------------------------------------------------------------- | |
void Awake() | |
{ | |
InitializeUI(); | |
GameEventUI.Instance.EventUpdateCurrentMapMiniMap += OnEventUpdateMap; | |
GameEventUI.Instance.EventUpdateCoordinateMiniMap += OnEventUpdateCoordinateMiniMap; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
void LateUpdate() | |
{ | |
if (GameManager.Instance.SceneIngame == null) return; | |
UpdatePlayerArrow(); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
void OnDestroy() | |
{ | |
GameEventUI.Instance.EventUpdateCurrentMapMiniMap -= OnEventUpdateMap; | |
GameEventUI.Instance.EventUpdateCoordinateMiniMap -= OnEventUpdateCoordinateMiniMap; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
void InitializeUI() | |
{ | |
Transform trBase = transform.Find("Base"); | |
m_maskMinimap = trBase.Find("Mask").GetComponent<Mask>(); | |
m_txtCoordinate = trBase.Find("TxtCoordinates").GetComponent<TextMeshProUGUI>(); | |
m_txtMapName = trBase.Find("TxtMap").GetComponent<TextMeshProUGUI>(); | |
m_btnPlus = trBase.Find("BtnPlus").GetComponent<Button>(); | |
m_btnPlus.onClick.RemoveAllListeners(); | |
m_btnPlus.onClick.AddListener(OnClickMiniMapPlus); | |
m_btnMinus = trBase.Find("BtnMinus").GetComponent<Button>(); | |
m_btnMinus.onClick.RemoveAllListeners(); | |
m_btnMinus.onClick.AddListener(OnClickMiniMapMinus); | |
m_imageMiniMap = m_maskMinimap.transform.Find("ImageMinimap").GetComponent<Image>(); | |
m_imagePlayer = m_maskMinimap.transform.Find("ImagePlayer").GetComponent<Image>(); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void OnEventUpdateMap() | |
{ | |
var hero = GameManager.Instance.SceneIngame.Hero; | |
if (hero == null) | |
{ | |
UnityEngine.Debug.LogError($"Hero is null."); | |
return; | |
} | |
var position = hero.Position; | |
var currentMap = hero.Map; | |
var texture = IngameData.Instance.LoadAsset<Sprite>("UI/Minimaps/" + currentMap); | |
m_imageMiniMap.sprite = texture; | |
// Reset the room. | |
m_nZoom = DEFAULT_ZOOM_INDEX; | |
m_fMapPicRealWidht = m_imageMiniMap.rectTransform.rect.width; | |
m_fMapPicRealHeight = m_imageMiniMap.rectTransform.rect.height; | |
m_flMaskWidth = m_maskMinimap.rectTransform.sizeDelta.x; | |
m_flMaskHeight = m_maskMinimap.rectTransform.sizeDelta.y; | |
m_nMapWidth = RoWalkDataProvider.Instance.WalkData.Width; | |
m_nMapHeight = RoWalkDataProvider.Instance.WalkData.Height; | |
SetMapName(currentMap); | |
SetCoordinateMiniMap(position.x, position.y); | |
UpdateMiniMapOffset(true); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void OnEventUpdateCoordinateMiniMap(float x, float y) | |
{ | |
SetCoordinateMiniMap(x, y); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void UpdatePlayerArrow() | |
{ | |
// Get the hero. | |
var hero = GameManager.Instance.SceneIngame.Hero; | |
if (hero == null) | |
return; | |
// Get the current position from the hero. | |
var vtPosition = hero.Position; | |
int vtX2 = 0, vtY2 = 0;// mini map point | |
ConverMapXYToMiniMapXY((int)vtPosition.x, (int)vtPosition.y, ref vtX2, ref vtY2); | |
// Update the player position from the mark in the minimap. | |
m_imagePlayer.rectTransform.anchoredPosition3D = new Vector3(ConveRealPiexPointX(vtX2), ConveRealPiexPointY(vtY2), 0.0f); | |
// Update the mark rotation. | |
m_imagePlayer.rectTransform.localRotation = Quaternion.Euler(0.0f, 0.0f, DirectionExtensions.GetAngleFromFacing(hero.SpriteAnimator.Direction)); | |
int vtW = 0; | |
int vtH = 0; | |
ConverMapXYToMiniMapXY((int)vtPosition.x, (int)vtPosition.y, ref vtW, ref vtH); | |
SetCenterPoint(vtW, vtH); | |
UpdateMiniMapOffset(false); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void SetMapName(string strMapName) | |
{ | |
m_txtMapName.text = strMapName; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void SetCoordinateMiniMap(float x, float y) | |
{ | |
m_txtCoordinate.text = $"{(int)x} {(int)y}"; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void UpdateMiniMapOffset(bool bIsUpdateScale) | |
{ | |
var flScale = m_fZoomValue[m_nZoom]; | |
if (bIsUpdateScale) | |
m_imageMiniMap.rectTransform.localScale = new Vector2(flScale, flScale); | |
m_imageMiniMap.rectTransform.anchoredPosition3D = new Vector3(m_fOffsetX, m_fOffsetY, 0.0f); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void MiniMapZoom(bool bIsPlus) | |
{ | |
var hero = GameManager.Instance.SceneIngame.Hero; | |
int w1 = 0; | |
int h1 = 0; | |
ConverMapXYToMiniMapXY((int)hero.Position.x, (int)hero.Position.y, ref w1, ref h1); | |
if (bIsPlus) | |
ZoomOut(w1, h1); | |
else | |
ZoomIn(w1, h1); | |
UpdateMiniMapOffset(true); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void OnClickMiniMapPlus() | |
{ | |
MiniMapZoom(true); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void OnClickMiniMapMinus() | |
{ | |
MiniMapZoom(false); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void ZoomOut(int flX, int flY) | |
{ | |
m_nZoom++; | |
if (m_nZoom >= m_fZoomValue.Length) | |
m_nZoom = m_fZoomValue.Length - 1; | |
SetCenterPoint(flX, flY); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void ZoomIn(int flX, int flY) | |
{ | |
m_nZoom--; | |
if (m_nZoom < 0) | |
{ | |
m_nZoom = DEFAULT_ZOOM_INDEX; | |
SetCenterPoint(flX, flY); | |
return; | |
} | |
if (m_fMapPicRealWidht * m_fZoomValue[m_nZoom] < m_flMaskWidth || | |
m_fMapPicRealHeight * m_fZoomValue[m_nZoom] < m_flMaskHeight) | |
{ | |
ZoomOut(flX, flY); | |
} | |
SetCenterPoint(flX, flY); | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void SetCenterPoint(int nX, int nY) | |
{ | |
#if false | |
if (m_nCenterX == nX && m_nCenterY == nY) | |
return; | |
#endif | |
m_nCenterX = nX; | |
m_nCenterY = nY; | |
m_fOffsetX = (m_flMaskWidth / 2) - nX * m_fZoomValue[m_nZoom]; | |
m_fOffsetY = (m_flMaskHeight / 2) - nY * m_fZoomValue[m_nZoom]; | |
m_fOffsetX = Math.Min(m_fOffsetX, 0); | |
m_fOffsetY = Math.Min(m_fOffsetY, 0); | |
m_fOffsetX = Math.Max(m_fOffsetX, -m_fMapPicRealWidht * m_fZoomValue[m_nZoom] + m_flMaskWidth); | |
m_fOffsetY = Math.Max(m_fOffsetY, -m_fMapPicRealHeight * m_fZoomValue[m_nZoom] + m_flMaskWidth); // TODO: Check it. | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private void ConverMapXYToMiniMapXY(int nMapX, int nMapY, ref int nMiniMapX, ref int nMiniMapY) | |
{ | |
nMiniMapX = nMapX * (int)m_fMapPicRealWidht / m_nMapWidth; | |
nMiniMapY = nMapY * (int)m_fMapPicRealHeight / m_nMapHeight; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private float ConveRealPiexPointX(float flX) | |
{ | |
return flX * m_fZoomValue[m_nZoom] + m_fOffsetX; | |
} | |
//--------------------------------------------------------------------------------------------------- | |
private float ConveRealPiexPointY(float flY) | |
{ | |
return flY * m_fZoomValue[m_nZoom] + m_fOffsetY; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment