| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using DCL.Models; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class AvatarShape : BaseComponent |
| | 14 | | { |
| | 15 | | private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 16 | |
|
| | 17 | | public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated; |
| | 18 | |
|
| | 19 | | public AvatarName avatarName; |
| | 20 | | public AvatarRenderer avatarRenderer; |
| | 21 | | public Collider avatarCollider; |
| | 22 | | public AvatarMovementController avatarMovementController; |
| | 23 | |
|
| | 24 | | [SerializeField] |
| | 25 | | private AvatarOnPointerDown onPointerDown; |
| | 26 | |
|
| | 27 | | private StringVariable currentPlayerInfoCardId; |
| | 28 | |
|
| 521 | 29 | | private AvatarModel oldModel = new AvatarModel(); |
| | 30 | |
|
| | 31 | | public bool everythingIsLoaded; |
| | 32 | |
|
| | 33 | | private Vector3? lastAvatarPosition = null; |
| 521 | 34 | | private MinimapMetadata.MinimapUserInfo avatarUserInfo = new MinimapMetadata.MinimapUserInfo(); |
| | 35 | | bool initializedPosition = false; |
| | 36 | |
|
| | 37 | | private void Awake() |
| | 38 | | { |
| 1 | 39 | | model = new AvatarModel(); |
| 1 | 40 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | private void PlayerClicked() |
| | 44 | | { |
| 0 | 45 | | if (model == null) |
| 0 | 46 | | return; |
| 0 | 47 | | currentPlayerInfoCardId.Set(((AvatarModel) model).id); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnDestroy() |
| | 51 | | { |
| 1 | 52 | | Cleanup(); |
| | 53 | |
|
| 1 | 54 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 1 | 55 | | poolableObject.RemoveFromPool(); |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 59 | | { |
| 1 | 60 | | DisablePassport(); |
| | 61 | |
|
| 1 | 62 | | var model = (AvatarModel) newModel; |
| | 63 | |
|
| 1 | 64 | | everythingIsLoaded = false; |
| | 65 | |
|
| 1 | 66 | | bool avatarDone = false; |
| 1 | 67 | | bool avatarFailed = false; |
| | 68 | |
|
| 1 | 69 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 70 | |
|
| 0 | 71 | | avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true); |
| | 72 | |
|
| 0 | 73 | | yield return new WaitUntil(() => avatarDone || avatarFailed); |
| | 74 | |
|
| 0 | 75 | | onPointerDown.Initialize( |
| | 76 | | new OnPointerDown.Model() |
| | 77 | | { |
| | 78 | | type = OnPointerDown.NAME, |
| | 79 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 80 | | hoverText = "view profile" |
| | 81 | | }, |
| | 82 | | entity |
| | 83 | | ); |
| | 84 | |
|
| 0 | 85 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 0 | 86 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 87 | |
|
| 0 | 88 | | entity.OnTransformChange -= avatarMovementController.OnTransformChanged; |
| 0 | 89 | | entity.OnTransformChange += avatarMovementController.OnTransformChanged; |
| | 90 | |
|
| 0 | 91 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 0 | 92 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 93 | |
|
| 0 | 94 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 0 | 95 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| | 96 | |
|
| | 97 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 0 | 98 | | if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM)) |
| | 99 | | { |
| 0 | 100 | | initializedPosition = true; |
| | 101 | |
|
| 0 | 102 | | avatarMovementController.MoveTo( |
| | 103 | | entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle |
| | 104 | | entity.gameObject.transform.localRotation, true); |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | avatarUserInfo.userId = model.id; |
| 0 | 108 | | avatarUserInfo.userName = model.name; |
| 0 | 109 | | avatarUserInfo.worldPosition = lastAvatarPosition != null ? lastAvatarPosition.Value : entity.gameObject.tra |
| 0 | 110 | | MinimapMetadataController.i?.UpdateMinimapUserInformation(avatarUserInfo); |
| | 111 | |
|
| 0 | 112 | | avatarName.SetName(model.name); |
| 0 | 113 | | avatarName.SetTalking(model.talking); |
| | 114 | |
|
| 0 | 115 | | avatarCollider.gameObject.SetActive(true); |
| | 116 | |
|
| 0 | 117 | | everythingIsLoaded = true; |
| 0 | 118 | | OnAvatarShapeUpdated?.Invoke(entity, this); |
| | 119 | |
|
| 0 | 120 | | EnablePassport(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void DisablePassport() |
| | 124 | | { |
| 1 | 125 | | if (onPointerDown.collider == null) |
| 0 | 126 | | return; |
| | 127 | |
|
| 1 | 128 | | onPointerDown.collider.enabled = false; |
| 1 | 129 | | } |
| | 130 | |
|
| | 131 | | public void EnablePassport() |
| | 132 | | { |
| 0 | 133 | | if (onPointerDown.collider == null) |
| 0 | 134 | | return; |
| | 135 | |
|
| 0 | 136 | | onPointerDown.collider.enabled = true; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 140 | | { |
| 0 | 141 | | avatarUserInfo.worldPosition = entity.gameObject.transform.position; |
| 0 | 142 | | MinimapMetadataController.i?.UpdateMinimapUserInformation(avatarUserInfo); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private void OnEntityTransformChanged(object newModel) |
| | 146 | | { |
| 0 | 147 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 148 | | lastAvatarPosition = newTransformModel.position; |
| | 149 | |
|
| 0 | 150 | | var model = (AvatarModel) this.model; |
| 0 | 151 | | avatarUserInfo.userId = model.id; |
| 0 | 152 | | avatarUserInfo.userName = model.name; |
| 0 | 153 | | avatarUserInfo.worldPosition = newTransformModel.position; |
| 0 | 154 | | MinimapMetadataController.i?.UpdateMinimapUserInformation(avatarUserInfo); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | public override void OnPoolGet() |
| | 158 | | { |
| 1 | 159 | | base.OnPoolGet(); |
| | 160 | |
|
| 1 | 161 | | everythingIsLoaded = false; |
| 1 | 162 | | initializedPosition = false; |
| 1 | 163 | | oldModel = new AvatarModel(); |
| 1 | 164 | | model = new AvatarModel(); |
| 1 | 165 | | lastAvatarPosition = null; |
| 1 | 166 | | avatarName.SetName(String.Empty); |
| 1 | 167 | | } |
| | 168 | |
|
| | 169 | | public override void Cleanup() |
| | 170 | | { |
| 3 | 171 | | base.Cleanup(); |
| | 172 | |
|
| 3 | 173 | | avatarRenderer.CleanupAvatar(); |
| | 174 | |
|
| 3 | 175 | | if (poolableObject != null) |
| | 176 | | { |
| 3 | 177 | | poolableObject.OnRelease -= Cleanup; |
| | 178 | | } |
| | 179 | |
|
| 3 | 180 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 3 | 181 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| | 182 | |
|
| 3 | 183 | | if (entity != null) |
| | 184 | | { |
| 1 | 185 | | entity.OnTransformChange = null; |
| 1 | 186 | | entity = null; |
| | 187 | | } |
| | 188 | |
|
| 3 | 189 | | var model = (AvatarModel) this.model; |
| 3 | 190 | | if (model != null) |
| 3 | 191 | | avatarUserInfo.userId = model.id; |
| 3 | 192 | | MinimapMetadataController.i?.UpdateMinimapUserInformation(avatarUserInfo, true); |
| 3 | 193 | | } |
| | 194 | |
|
| 0 | 195 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 196 | | } |
| | 197 | | } |