| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Components |
| | 7 | | { |
| | 8 | | public class NFTShape : LoadableShape<LoadWrapper_NFT, NFTShape.Model> |
| | 9 | | { |
| | 10 | | [System.Serializable] |
| | 11 | | public new class Model : LoadableShape.Model |
| | 12 | | { |
| 37 | 13 | | public Color color = new Color(0.6404918f, 0.611472f, 0.8584906f); // "light purple" default, same as in exp |
| | 14 | | public int style = 0; |
| | 15 | |
|
| 12 | 16 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 17 | | } |
| | 18 | |
|
| 9 | 19 | | public override string componentName => "NFT Shape"; |
| | 20 | |
|
| | 21 | | private INFTInfoRetriever infoRetriever; |
| | 22 | | private INFTAssetRetriever assetRetriever; |
| | 23 | |
|
| 11 | 24 | | public NFTShape(INFTInfoRetriever infoRetriever, INFTAssetRetriever assetRetriever) |
| | 25 | | { |
| 11 | 26 | | model = new Model(); |
| 11 | 27 | | this.infoRetriever = infoRetriever; |
| 11 | 28 | | this.assetRetriever = assetRetriever; |
| 11 | 29 | | } |
| | 30 | |
|
| 5 | 31 | | public override int GetClassId() { return (int) CLASS_ID.NFT_SHAPE; } |
| | 32 | |
|
| | 33 | | protected override void AttachShape(IDCLEntity entity) |
| | 34 | | { |
| 9 | 35 | | if (string.IsNullOrEmpty(model.src)) |
| | 36 | | { |
| | 37 | | #if UNITY_EDITOR |
| 0 | 38 | | Debug.LogError($"NFT SHAPE with url '{model.src}' couldn't be loaded."); |
| | 39 | | #endif |
| 0 | 40 | | return; |
| | 41 | | } |
| | 42 | |
|
| 9 | 43 | | entity.meshesInfo.meshRootGameObject = NFTShapeFactory.InstantiateLoaderController(model.style); |
| 9 | 44 | | entity.meshesInfo.currentShape = this; |
| | 45 | |
|
| 9 | 46 | | entity.meshRootGameObject.name = componentName + " mesh"; |
| 9 | 47 | | entity.meshRootGameObject.transform.SetParent(entity.gameObject.transform); |
| 9 | 48 | | entity.meshRootGameObject.transform.ResetLocalTRS(); |
| | 49 | |
|
| 9 | 50 | | var loaderController = entity.meshRootGameObject.GetComponent<NFTShapeLoaderController>(); |
| | 51 | |
|
| 9 | 52 | | if (loaderController) |
| 9 | 53 | | loaderController.Initialize(infoRetriever, assetRetriever); |
| | 54 | |
|
| 9 | 55 | | entity.OnShapeUpdated += UpdateBackgroundColor; |
| | 56 | |
|
| 9 | 57 | | var loadableShape = Environment.i.world.state.GetOrAddLoaderForEntity<LoadWrapper_NFT>(entity); |
| | 58 | |
|
| 9 | 59 | | loadableShape.entity = entity; |
| | 60 | |
|
| 9 | 61 | | bool initialVisibility = model.visible; |
| 9 | 62 | | if (!DataStore.i.debugConfig.isDebugMode.Get()) |
| 6 | 63 | | initialVisibility &= entity.isInsideSceneBoundaries; |
| 9 | 64 | | loadableShape.initialVisibility = initialVisibility; |
| | 65 | |
|
| 9 | 66 | | loadableShape.withCollisions = model.withCollisions && entity.isInsideSceneBoundaries; |
| 9 | 67 | | loadableShape.backgroundColor = model.color; |
| | 68 | |
|
| 9 | 69 | | loadableShape.Load(model.src, OnLoadCompleted, OnLoadFailed); |
| 9 | 70 | | } |
| | 71 | |
|
| | 72 | | protected override void DetachShape(IDCLEntity entity) |
| | 73 | | { |
| 7 | 74 | | if (entity == null || entity.meshRootGameObject == null) |
| 0 | 75 | | return; |
| | 76 | |
|
| 7 | 77 | | entity.OnShapeUpdated -= UpdateBackgroundColor; |
| | 78 | |
|
| 7 | 79 | | base.DetachShape(entity); |
| 7 | 80 | | } |
| | 81 | |
|
| 18 | 82 | | protected override void ConfigureColliders(IDCLEntity entity) { CollidersManager.i.ConfigureColliders(entity.mes |
| | 83 | |
|
| | 84 | | void UpdateBackgroundColor(IDCLEntity entity) |
| | 85 | | { |
| 18 | 86 | | if (previousModel is NFTShape.Model && model.color == previousModel.color) |
| 14 | 87 | | return; |
| | 88 | |
|
| 4 | 89 | | var loadableShape = Environment.i.world.state.GetLoaderForEntity(entity) as LoadWrapper_NFT; |
| 4 | 90 | | loadableShape?.loaderController.UpdateBackgroundColor(model.color); |
| 4 | 91 | | } |
| | 92 | |
|
| | 93 | | public override string ToString() |
| | 94 | | { |
| 0 | 95 | | if (model == null) |
| 0 | 96 | | return base.ToString(); |
| | 97 | |
|
| 0 | 98 | | return $"{componentName} (src = {model.src})"; |
| | 99 | | } |
| | 100 | | } |
| | 101 | | } |