SanAndreasUnity/Assets/Scripts/Behaviours/MiniMap.cs

270 lines
9 KiB
C#
Raw Normal View History

2020-05-01 18:53:28 +00:00
using SanAndreasUnity.Importing.Conversion;
2020-05-31 17:07:22 +00:00
using SanAndreasUnity.Utilities;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace SanAndreasUnity.Behaviours
{
public class MiniMap : MonoBehaviour
{
public const int tileEdge = 12; // width/height of map in tiles
public const int tileCount = tileEdge * tileEdge; // number of tiles
public const int mapEdge = 6000; // width/height of map in world coordinates
public const int texSize = 128; // width/height of single tile in px
public const int mapSize = tileEdge * texSize; // width/height of whole map in px
public const int uiSize = 256, uiOffset = 10;
2020-05-01 18:23:02 +00:00
public static MiniMap Instance { get; private set; }
2020-05-31 17:07:22 +00:00
public Image northImage,
outlineImage,
2020-05-02 19:52:39 +00:00
maskImage;
2020-05-31 17:07:22 +00:00
2020-05-02 19:52:39 +00:00
public RawImage mapImage, playerImage;
2020-04-28 21:55:02 +00:00
2020-05-31 17:07:22 +00:00
public RectTransform mapTransform,
maskTransform,
2020-05-01 18:23:02 +00:00
mapContainer,
northPivot;
2020-05-31 17:07:22 +00:00
2020-04-28 23:08:38 +00:00
public Text zoneNameLabel;
private Canvas _canvas;
2020-05-01 18:23:02 +00:00
private const float maxVelocityForZooming = 300f;
public static readonly float[] availableZooms = new float[] { .5f, .75f, 1f, 1.2f, 1.4f, 1.6f, 2f, 2.5f, 3f, 5f };
2020-05-31 17:07:22 +00:00
2020-05-01 18:23:02 +00:00
public float zoom = 1.3f;
public float zoomDuration = 1;
private float curZoomPercentage;
public int zoomSelector = 2;
private Coroutine zoomCoroutine;
2020-05-31 17:07:22 +00:00
2020-05-01 16:21:22 +00:00
public Vector3 FocusPos { get; set; } = Vector3.zero;
public bool IsMinimapVisible => _canvas.enabled && this.gameObject.activeInHierarchy;
2020-05-01 16:35:43 +00:00
private float _timeWhenRetrievedZoneName = 0f;
2020-05-31 17:07:22 +00:00
2020-05-01 16:26:45 +00:00
private string _lastZoneName = "";
2020-05-31 17:07:22 +00:00
private string ZoneName
{
get
{
2020-05-01 16:35:43 +00:00
if (Time.time - _timeWhenRetrievedZoneName > 2f)
2020-05-31 17:07:22 +00:00
{
2020-05-01 16:35:43 +00:00
_timeWhenRetrievedZoneName = Time.time;
2020-05-01 16:26:45 +00:00
_lastZoneName = Zone.GetZoneName(this.FocusPos);
2020-05-31 17:07:22 +00:00
}
2020-05-01 16:26:45 +00:00
return _lastZoneName;
2020-05-31 17:07:22 +00:00
}
}
2020-05-01 18:25:06 +00:00
public Texture2D NorthBlip { get; private set; }
public Texture2D PlayerBlip { get; private set; }
public Texture2D WaypointTexture { get; private set; }
public Texture2D VehicleTexture { get; private set; }
2019-10-27 01:07:08 +00:00
public Texture2D GreenHouseTexture { get; private set; }
2020-05-01 18:25:06 +00:00
public Texture2D MapTexture { get; private set; }
2020-05-31 17:07:22 +00:00
2020-05-01 18:25:06 +00:00
public Texture2D BlackPixel { get; private set; }
public Texture2D SeaPixel { get; private set; }
2020-05-31 17:07:22 +00:00
2020-05-01 17:28:01 +00:00
public void Load()
2020-05-31 17:07:22 +00:00
{
2020-05-01 17:28:01 +00:00
LoadGameTextures();
2020-05-31 17:07:22 +00:00
2020-05-01 18:25:06 +00:00
BlackPixel = new Texture2D(1, 1);
BlackPixel.SetPixel(0, 0, new Color(0, 0, 0, .5f));
BlackPixel.Apply();
2020-05-31 17:07:22 +00:00
2020-05-01 18:25:06 +00:00
SeaPixel = new Texture2D(1, 1);
SeaPixel.SetPixel(0, 0, new Color(.45f, .54f, .678f));
SeaPixel.Apply();
2020-05-31 17:07:22 +00:00
}
2020-05-01 17:39:40 +00:00
private void LoadGameTextures()
{
2020-05-01 18:25:06 +00:00
MapTexture = new Texture2D(mapSize, mapSize, TextureFormat.ARGB32, false, true);
2020-05-01 17:39:40 +00:00
TextureLoadParams textureLoadParams = new TextureLoadParams() { makeNoLongerReadable = false };
for (int i = 0; i < tileCount; i++)
{
// Offset
int y = ((i / tileEdge) + 1) * texSize,
x = (i % tileEdge) * texSize;
string name = "radar" + ((i < 10) ? "0" : "") + i;
var texDict = TextureDictionary.Load(name);
Texture2D tex = texDict.GetDiffuse(name, textureLoadParams).Texture;
for (int ii = 0; ii < texSize; ++ii)
for (int jj = 0; jj < texSize; ++jj)
2020-05-01 18:25:06 +00:00
MapTexture.SetPixel(x + ii, texSize - (y + jj) - 1, tex.GetPixel(ii, jj));
2020-05-01 17:39:40 +00:00
// unload the texture (don't destroy it, because it can be a dummy texture)
}
2020-05-01 18:25:06 +00:00
MapTexture.Apply(false, true);
2020-05-01 17:39:40 +00:00
2020-05-01 20:07:47 +00:00
var huds = TextureDictionary.Load("hud");
2020-05-01 18:25:06 +00:00
NorthBlip = huds.GetDiffuse("radar_north").Texture;
PlayerBlip = huds.GetDiffuse("radar_centre").Texture;
WaypointTexture = huds.GetDiffuse("radar_waypoint").Texture;
VehicleTexture = huds.GetDiffuse("radar_impound").Texture;
2020-05-01 17:39:40 +00:00
GreenHouseTexture = huds.GetDiffuse("radar_propertyG").Texture;
2020-05-01 18:25:06 +00:00
northImage.sprite = Sprite.Create(NorthBlip, new Rect(0, 0, NorthBlip.width, NorthBlip.height), new Vector2(NorthBlip.width, NorthBlip.height) / 2);
2020-05-01 17:39:40 +00:00
playerImage.texture = this.PlayerBlip;
2020-05-02 19:52:39 +00:00
mapImage.texture = MapTexture;
2020-05-01 17:39:40 +00:00
}
2020-05-31 17:07:22 +00:00
private void Awake()
{
Instance = this;
_canvas = this.GetComponentInParent<Canvas>();
2020-05-01 18:23:02 +00:00
curZoomPercentage = availableZooms[zoomSelector];
2020-05-31 17:07:22 +00:00
}
2020-05-01 17:03:23 +00:00
private void ReadInput()
{
2020-05-01 17:48:24 +00:00
bool zoomIn = Input.GetKeyDown(KeyCode.N);
bool zoomOut = Input.GetKeyDown(KeyCode.B);
2020-05-01 17:03:23 +00:00
2020-05-01 17:48:24 +00:00
if (zoomIn)
2020-05-31 17:07:22 +00:00
++zoomSelector;
2020-05-01 17:48:24 +00:00
else if (zoomOut)
2020-05-31 17:07:22 +00:00
--zoomSelector;
2020-05-01 17:48:24 +00:00
if (zoomIn || zoomOut)
2020-05-31 17:07:22 +00:00
{
2020-05-01 17:03:23 +00:00
if (zoomCoroutine != null)
StopCoroutine(zoomCoroutine);
2020-05-01 17:48:24 +00:00
zoomCoroutine = StartCoroutine(ChangeZoom(zoomIn));
2020-05-31 17:07:22 +00:00
}
}
public static Vector2 WorldPosToMapPos(Vector3 worldPos)
{
// map center is at (0,0) world coordinates
// this, for example, means that the left edge of the world is at: -mapEdge / 2.0f
// adjust world position, so that (0,0) world coordinates are mapped to (0,0) map coordinates
worldPos += new Vector3(mapEdge / 2.0f, 0, mapEdge / 2.0f);
float mul = mapSize / (float)mapEdge;
return new Vector2(worldPos.x * mul, worldPos.z * mul);
}
public static Vector3 MapPosToWorldPos(Vector2 mapPos)
{
// adjust map position, so that (0,0) map coordinated are mapped to (0,0) world coordinates
mapPos -= Vector2.one * (mapSize * 0.5f);
float mul = mapEdge / (float)mapSize;
return new Vector3(mapPos.x * mul, 0.0f, mapPos.y * mul);
}
private void LateUpdate()
{
if (!this.IsMinimapVisible)
return;
2020-05-31 17:07:22 +00:00
2020-05-01 16:21:22 +00:00
// update focus position
2020-05-01 18:53:28 +00:00
var ped = Ped.Instance;
2020-05-01 16:21:22 +00:00
if (ped != null)
this.FocusPos = ped.transform.position;
else if (Camera.main != null)
this.FocusPos = Camera.main.transform.position;
// update zoom based on ped's velocity
2020-05-01 18:53:28 +00:00
var playerController = PlayerController.Instance;
if (playerController != null)
2020-05-01 18:23:02 +00:00
zoom = Mathf.Lerp(.9f, 1.3f, 1 - Mathf.Clamp(playerController.CurVelocity, 0, maxVelocityForZooming) / maxVelocityForZooming) * curZoomPercentage;
2020-05-01 16:21:22 +00:00
// read input
if (GameManager.CanPlayerReadInput())
{
this.ReadInput();
}
// update position of UI
2020-05-01 16:21:22 +00:00
2020-05-02 20:13:25 +00:00
Vector3 mapPos = - new Vector3(this.FocusPos.x, this.FocusPos.z, 0f) * (mapSize / (float)mapEdge);
2020-04-28 21:39:30 +00:00
mapImage.rectTransform.localPosition = mapPos;
2020-05-31 17:07:22 +00:00
// update rotation of UI
2020-05-01 16:21:22 +00:00
2020-04-28 21:39:30 +00:00
float relAngle = Camera.main != null ? Camera.main.transform.eulerAngles.y : 0f;
//mapContainer.pivot = new Vector2(mapPos.x, mapPos.y);
mapContainer.localRotation = Quaternion.Euler(0, 0, relAngle);
2020-04-28 21:47:45 +00:00
2020-05-01 18:23:02 +00:00
mapContainer.localScale = new Vector3(zoom, zoom, 1);
2020-05-01 18:02:31 +00:00
2020-05-31 17:07:22 +00:00
if (northPivot != null)
northPivot.localRotation = Quaternion.Euler(0, 0, relAngle);
2020-05-01 18:53:28 +00:00
if (playerImage != null && ped != null)
playerImage.rectTransform.localRotation = Quaternion.Euler(0, 0, relAngle - (ped.transform.eulerAngles.y + 180));
2020-04-28 21:47:45 +00:00
2020-04-28 23:08:38 +00:00
// update zone name label
string currentZoneName = this.ZoneName;
if (currentZoneName != this.zoneNameLabel.text)
this.zoneNameLabel.text = currentZoneName;
2020-05-31 17:07:22 +00:00
}
private IEnumerator ChangeZoom(bool isIncreasing)
{
2020-05-01 18:02:31 +00:00
float fAlpha = 1;
2020-05-31 17:07:22 +00:00
zoomSelector = GetClampedZoomSelector(zoomSelector);
2020-05-01 18:23:02 +00:00
float curZoom = availableZooms[zoomSelector % availableZooms.Length],
lastZoom = availableZooms[GetClampedZoomSelector(zoomSelector - 1 * (isIncreasing ? 1 : -1)) % availableZooms.Length];
2020-05-31 17:07:22 +00:00
float t = 0;
while (t < zoomDuration)
{
curZoomPercentage = Mathf.Lerp(lastZoom, curZoom, t / zoomDuration);
yield return new WaitForFixedUpdate();
t += Time.fixedDeltaTime;
fAlpha -= Time.fixedDeltaTime / zoomDuration;
}
zoomCoroutine = null;
}
private int GetClampedZoomSelector(int? val = null)
{
int zoomVal = val == null ? zoomSelector : val.Value;
if (zoomVal < 0)
2020-05-01 18:23:02 +00:00
zoomVal = availableZooms.Length - 1;
2020-05-31 17:07:22 +00:00
return zoomVal;
}
}
}