| | 1 | | using DCL.Configuration; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | /* |
| | 7 | | * Scene utils file for scene bounds related stuff |
| | 8 | | */ |
| | 9 | |
|
| | 10 | | public static partial class UtilsScene |
| | 11 | | { |
| | 12 | | public static Bounds CalculateOuterBounds(IReadOnlyList<Vector2Int> parcels, Vector3 baseParcelWorldPos) |
| | 13 | | { |
| 0 | 14 | | Bounds outerBounds = new Bounds(); |
| 0 | 15 | | outerBounds.SetMinMax(new Vector3(baseParcelWorldPos.x, 0f, baseParcelWorldPos.z), |
| | 16 | | new Vector3(baseParcelWorldPos.x + ParcelSettings.PARCEL_SIZE, 0f, baseParcelWorldPos.z + ParcelSettings.PAR |
| | 17 | |
|
| 0 | 18 | | for (int i = 0; i < parcels.Count; i++) |
| | 19 | | { |
| | 20 | | // Update outer bounds with parcel's size |
| 0 | 21 | | var parcel = parcels[i]; |
| | 22 | |
|
| 0 | 23 | | Vector3 parcelWorldPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(parcel.x, parcel.y)); |
| 0 | 24 | | outerBounds.Encapsulate(new Vector3(parcelWorldPos.x, 0, parcelWorldPos.z)); |
| 0 | 25 | | outerBounds.Encapsulate(new Vector3(parcelWorldPos.x + ParcelSettings.PARCEL_SIZE, 0, parcelWorldPos.z + Par |
| | 26 | | } |
| | 27 | |
|
| | 28 | | // Apply outer bounds extra threshold |
| 0 | 29 | | outerBounds.SetMinMax(new Vector3(outerBounds.min.x - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD, 0f, outerBound |
| | 30 | | new Vector3(outerBounds.max.x + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD, 0f, outerBounds.max.z + ParcelSe |
| | 31 | |
|
| 0 | 32 | | return outerBounds; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static bool IsInsideSceneInnerBounds(HashSet<Vector2Int> sceneParcels, float sceneHeightLimit, Vector3 target |
| | 36 | | { |
| 0 | 37 | | if (sceneParcels.Count == 0) |
| 0 | 38 | | return false; |
| | 39 | |
|
| 0 | 40 | | if (height > sceneHeightLimit) |
| 0 | 41 | | return false; |
| | 42 | |
|
| 0 | 43 | | int noThresholdZCoordinate = Mathf.FloorToInt(targetWorldPosition.z / ParcelSettings.PARCEL_SIZE); |
| 0 | 44 | | int noThresholdXCoordinate = Mathf.FloorToInt(targetWorldPosition.x / ParcelSettings.PARCEL_SIZE); |
| | 45 | |
|
| | 46 | | // We check the target world position |
| 0 | 47 | | Vector2Int targetCoordinate = new Vector2Int(noThresholdXCoordinate, noThresholdZCoordinate); |
| | 48 | |
|
| 0 | 49 | | if (sceneParcels.Contains(targetCoordinate)) |
| 0 | 50 | | return true; |
| | 51 | |
|
| | 52 | | // We need to check using a threshold from the target point, in order to cover correctly the parcel "border/edge |
| 0 | 53 | | Vector2Int coordinateMin = new Vector2Int(); |
| 0 | 54 | | coordinateMin.x = Mathf.FloorToInt((targetWorldPosition.x - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / Parcel |
| 0 | 55 | | coordinateMin.y = Mathf.FloorToInt((targetWorldPosition.z - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / Parcel |
| | 56 | |
|
| 0 | 57 | | Vector2Int coordinateMax = new Vector2Int(); |
| 0 | 58 | | coordinateMax.x = Mathf.FloorToInt((targetWorldPosition.x + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / Parcel |
| 0 | 59 | | coordinateMax.y = Mathf.FloorToInt((targetWorldPosition.z + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / Parcel |
| | 60 | |
|
| | 61 | | // We check the east/north-threshold position |
| 0 | 62 | | targetCoordinate.Set(coordinateMax.x, coordinateMax.y); |
| | 63 | |
|
| 0 | 64 | | if (sceneParcels.Contains(targetCoordinate)) |
| 0 | 65 | | return true; |
| | 66 | |
|
| | 67 | | // We check the east/south-threshold position |
| 0 | 68 | | targetCoordinate.Set(coordinateMax.x, coordinateMin.y); |
| | 69 | |
|
| 0 | 70 | | if (sceneParcels.Contains(targetCoordinate)) |
| 0 | 71 | | return true; |
| | 72 | |
|
| | 73 | | // We check the west/north-threshold position |
| 0 | 74 | | targetCoordinate.Set(coordinateMin.x, coordinateMax.y); |
| | 75 | |
|
| 0 | 76 | | if (sceneParcels.Contains(targetCoordinate)) |
| 0 | 77 | | return true; |
| | 78 | |
|
| | 79 | | // We check the west/south-threshold position |
| 0 | 80 | | targetCoordinate.Set(coordinateMin.x, coordinateMin.y); |
| | 81 | |
|
| 0 | 82 | | if (sceneParcels.Contains(targetCoordinate)) |
| 0 | 83 | | return true; |
| | 84 | |
|
| 0 | 85 | | return false; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public static bool IsInsideSceneOuterBounds(Bounds sceneOuterBounds, Vector3 targetUnityPosition) |
| | 89 | | { |
| 0 | 90 | | targetUnityPosition.y = 0f; |
| 0 | 91 | | return sceneOuterBounds.Contains(targetUnityPosition); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | public static bool IsInsideSceneInnerBounds(HashSet<Vector2Int> sceneParcels, float sceneHeightLimit, Bounds targetB |
| | 95 | | { |
| 0 | 96 | | var worldOffset = CommonScriptableObjects.worldOffset.Get(); |
| | 97 | |
|
| 0 | 98 | | if (!IsInsideSceneInnerBounds(sceneParcels, sceneHeightLimit, targetBounds.min + worldOffset, targetBounds.max.y |
| 0 | 99 | | return false; |
| | 100 | |
|
| 0 | 101 | | if (!IsInsideSceneInnerBounds(sceneParcels, sceneHeightLimit, targetBounds.max + worldOffset, targetBounds.max.y |
| 0 | 102 | | return false; |
| | 103 | |
|
| 0 | 104 | | return true; |
| | 105 | | } |
| | 106 | | } |