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