| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace NFTShape_Internal |
| | 6 | | { |
| | 7 | | public class NFTShapeHQImageConfig |
| | 8 | | { |
| | 9 | | public string name; |
| | 10 | | public string imageUrl; |
| | 11 | | public Transform transform; |
| | 12 | | public Collider collider; |
| | 13 | | public INFTAsset asset; |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public class NFTShapeHQImageHandler : IDisposable |
| | 17 | | { |
| | 18 | | public static bool VERBOSE = false; |
| | 19 | |
|
| | 20 | | readonly NFTShapeHQImageConfig hqImageConfig; |
| | 21 | | readonly INFTAsset asset; |
| | 22 | | readonly Camera camera; |
| | 23 | | readonly Transform nftControllerT; |
| | 24 | |
|
| | 25 | | bool isPlayerNear; |
| | 26 | | bool isCameraInFront; |
| | 27 | | bool isPlayerLooking; |
| | 28 | |
|
| | 29 | | public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config) |
| | 30 | | { |
| 14 | 31 | | if (config.asset == null || !config.collider || !config.transform) |
| | 32 | | { |
| 0 | 33 | | return null; |
| | 34 | | } |
| | 35 | |
|
| 14 | 36 | | return new NFTShapeHQImageHandler(config); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public void Dispose() |
| | 40 | | { |
| 1 | 41 | | CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged; |
| 1 | 42 | | asset.Dispose(); |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Update() |
| | 46 | | { |
| 23 | 47 | | if (!hqImageConfig.collider) |
| 0 | 48 | | return; |
| | 49 | |
|
| 23 | 50 | | if (!isPlayerNear) |
| 1 | 51 | | return; |
| | 52 | |
|
| 22 | 53 | | var config = DataStore.i.Get<DataStore_NFTShape>(); |
| | 54 | |
|
| 22 | 55 | | isCameraInFront = camera == null || |
| | 56 | | Vector3.Dot(nftControllerT.forward, |
| | 57 | | nftControllerT.position - camera.transform.position) |
| | 58 | | > config.hqImgInFrontDotProdMinValue; |
| | 59 | |
|
| 22 | 60 | | if (VERBOSE) |
| | 61 | | { |
| 0 | 62 | | Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}"); |
| | 63 | | } |
| | 64 | |
|
| 22 | 65 | | if (!isCameraInFront) |
| | 66 | | { |
| 1 | 67 | | RestorePreviewTextureIfInHQ(); |
| 1 | 68 | | return; |
| | 69 | | } |
| | 70 | |
|
| 21 | 71 | | isPlayerLooking = camera == null || |
| | 72 | | Vector3.Dot(nftControllerT.forward, camera.transform.forward) >= |
| | 73 | | config.hqImgFacingDotProdMinValue; |
| | 74 | |
|
| 21 | 75 | | if (VERBOSE) |
| | 76 | | { |
| 0 | 77 | | Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}"); |
| | 78 | | } |
| | 79 | |
|
| 21 | 80 | | if (isPlayerLooking) |
| | 81 | | { |
| 20 | 82 | | FetchHQTexture(); |
| 20 | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 1 | 86 | | RestorePreviewTextureIfInHQ(); |
| | 87 | | } |
| 1 | 88 | | } |
| | 89 | |
|
| 14 | 90 | | private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig) |
| | 91 | | { |
| 14 | 92 | | this.hqImageConfig = hqImageConfig; |
| 14 | 93 | | this.asset = hqImageConfig.asset; |
| | 94 | |
|
| 14 | 95 | | camera = Camera.main; |
| 14 | 96 | | nftControllerT = hqImageConfig.transform; |
| | 97 | |
|
| 14 | 98 | | CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged; |
| 14 | 99 | | OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero); |
| 14 | 100 | | } |
| | 101 | |
|
| | 102 | | private void OnPlayerPositionChanged(Vector3 current, Vector3 prev) |
| | 103 | | { |
| 67 | 104 | | isPlayerNear = false; |
| | 105 | |
|
| 67 | 106 | | if (!hqImageConfig.transform || !hqImageConfig.collider) |
| 38 | 107 | | return; |
| | 108 | |
|
| 29 | 109 | | var config = DataStore.i.Get<DataStore_NFTShape>(); |
| | 110 | |
|
| 29 | 111 | | isPlayerNear = ((current - hqImageConfig.collider.ClosestPoint(current)).sqrMagnitude |
| | 112 | | <= (config.hqImgMinDistance * |
| | 113 | | config.hqImgMinDistance)); |
| | 114 | |
|
| 29 | 115 | | if (!isPlayerNear) |
| | 116 | | { |
| 11 | 117 | | RestorePreviewTextureIfInHQ(); |
| | 118 | | } |
| | 119 | |
|
| 29 | 120 | | if (VERBOSE) |
| | 121 | | { |
| 0 | 122 | | Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}"); |
| | 123 | | } |
| 29 | 124 | | } |
| | 125 | |
|
| | 126 | | private void FetchHQTexture() |
| | 127 | | { |
| 20 | 128 | | if (asset.isHQ) |
| 0 | 129 | | return; |
| | 130 | |
|
| 20 | 131 | | Action debugSuccess = null; |
| 20 | 132 | | Action<Exception> debugFail = null; |
| | 133 | |
|
| 20 | 134 | | if (VERBOSE) |
| | 135 | | { |
| 0 | 136 | | debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image"); |
| 0 | 137 | | debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}"); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere. |
| 20 | 141 | | asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail); |
| | 142 | |
|
| 20 | 143 | | if (VERBOSE) |
| | 144 | | { |
| 0 | 145 | | Debug.Log($"Fetch {hqImageConfig.name} HQ image"); |
| | 146 | | } |
| 20 | 147 | | } |
| | 148 | |
|
| | 149 | | private void RestorePreviewTexture() |
| | 150 | | { |
| 3 | 151 | | asset.RestorePreviewAsset(); |
| 3 | 152 | | if (VERBOSE) |
| | 153 | | { |
| 0 | 154 | | Debug.Log($"Restore {hqImageConfig.name} preview image"); |
| | 155 | | } |
| 3 | 156 | | } |
| | 157 | |
|
| | 158 | | private void RestorePreviewTextureIfInHQ() |
| | 159 | | { |
| 13 | 160 | | if (!asset.isHQ) |
| 10 | 161 | | return; |
| | 162 | |
|
| 3 | 163 | | RestorePreviewTexture(); |
| 3 | 164 | | } |
| | 165 | | } |
| | 166 | | } |