| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCLServices.MapRendererV2.CoordsUtils |
| | 6 | | { |
| | 7 | | internal class ChunkCoordsUtils : ICoordsUtils |
| | 8 | | { |
| 1 | 9 | | private static readonly Vector2Int WORLD_MIN_COORDS = new (-150, -150); |
| 1 | 10 | | private static readonly Vector2Int WORLD_MAX_COORDS = new (175, 175); // DCL map is not squared, there are some |
| | 11 | |
|
| 1 | 12 | | private static readonly Vector2Int VISIBLE_WORLD_MIN_COORDS = new (-175, -175); |
| 1 | 13 | | private static readonly Vector2Int VISIBLE_WORLD_MAX_COORDS = new (175, 175); // DCL map is not squared, there a |
| | 14 | |
|
| 1 | 15 | | private static readonly Rect[] INTERACTABLE_WORLD_BOUNDS = |
| | 16 | | { |
| | 17 | | Rect.MinMaxRect(-150, -150, 150, 150), |
| | 18 | | Rect.MinMaxRect(62, 151, 162, 158), |
| | 19 | | Rect.MinMaxRect(151, 59, 163, 150) |
| | 20 | | }; |
| | 21 | |
|
| | 22 | | private readonly List<Rect> interactableWorldBoundsInLocalCoordinates; |
| | 23 | |
|
| 398 | 24 | | public Vector2Int WorldMinCoords => WORLD_MIN_COORDS; |
| 866 | 25 | | public Vector2Int WorldMaxCoords => WORLD_MAX_COORDS; |
| | 26 | |
|
| 28099 | 27 | | public int ParcelSize { get; } |
| 0 | 28 | | public Rect VisibleWorldBounds { get; } |
| | 29 | |
|
| 97 | 30 | | public ChunkCoordsUtils(int parcelSize) |
| | 31 | | { |
| 97 | 32 | | ParcelSize = parcelSize; |
| | 33 | |
|
| 97 | 34 | | var min = (VISIBLE_WORLD_MIN_COORDS - Vector2Int.one) * parcelSize; |
| 97 | 35 | | var max = VISIBLE_WORLD_MAX_COORDS * parcelSize; |
| 97 | 36 | | VisibleWorldBounds = Rect.MinMaxRect(min.x, min.y, max.x, max.y); |
| | 37 | |
|
| 97 | 38 | | interactableWorldBoundsInLocalCoordinates = INTERACTABLE_WORLD_BOUNDS |
| 291 | 39 | | .Select(chunk => Rect.MinMaxRect((chunk.xMin - 1) * parcelSize, ( |
| | 40 | | .ToList(); |
| 97 | 41 | | } |
| | 42 | |
|
| | 43 | | public bool TryGetCoordsWithinInteractableBounds(Vector3 pos, out Vector2Int coords) |
| | 44 | | { |
| 0 | 45 | | coords = default; |
| | 46 | |
|
| 0 | 47 | | foreach (Rect rect in interactableWorldBoundsInLocalCoordinates) |
| | 48 | | { |
| 0 | 49 | | if (rect.Contains(pos)) |
| | 50 | | { |
| 0 | 51 | | coords = PositionToCoords(pos); |
| 0 | 52 | | return true; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | return false; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public Vector2Int PositionToCoords(Vector3 pos) => |
| 0 | 60 | | new (Mathf.CeilToInt(pos.x / ParcelSize), Mathf.CeilToInt(pos.y / ParcelSize)); |
| | 61 | |
|
| | 62 | | public Vector2 PositionToCoordsUnclamped(Vector3 pos) => |
| 0 | 63 | | pos / ParcelSize; |
| | 64 | |
|
| | 65 | | public Vector3 CoordsToPositionUnclamped(Vector2 coords) => |
| 0 | 66 | | coords * ParcelSize; |
| | 67 | |
|
| | 68 | | public Vector3 CoordsToPosition(Vector2Int coords) => |
| 96 | 69 | | (Vector2)(coords * ParcelSize); |
| | 70 | |
|
| | 71 | | public Vector3 CoordsToPositionWithOffset(Vector2 coords) => |
| 0 | 72 | | (coords * ParcelSize) - new Vector2(ParcelSize / 2f, ParcelSize / 2f); |
| | 73 | | } |
| | 74 | | } |