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