| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System.Collections; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class AvatarShape : BaseComponent |
| | 11 | | { |
| | 12 | | private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 13 | |
|
| | 14 | | public static event Action<IDCLEntity, AvatarShape> OnAvatarShapeUpdated; |
| | 15 | |
|
| | 16 | | public AvatarRenderer avatarRenderer; |
| | 17 | | public Collider avatarCollider; |
| | 18 | | public AvatarMovementController avatarMovementController; |
| | 19 | |
|
| | 20 | | [SerializeField] |
| | 21 | | internal AvatarOnPointerDown onPointerDown; |
| | 22 | |
|
| | 23 | | private StringVariable currentPlayerInfoCardId; |
| | 24 | |
|
| 655 | 25 | | private AvatarModel oldModel = new AvatarModel(); |
| | 26 | |
|
| | 27 | | public bool everythingIsLoaded; |
| | 28 | |
|
| | 29 | | private Vector3? lastAvatarPosition = null; |
| | 30 | | bool initializedPosition = false; |
| | 31 | |
|
| | 32 | | private Player player = null; |
| 0 | 33 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| 0 | 34 | | private BaseHashSet<string> visibleNames => DataStore.i.avatarsLOD.visibleNames; |
| | 35 | |
|
| | 36 | | private void Awake() |
| | 37 | | { |
| 654 | 38 | | model = new AvatarModel(); |
| 654 | 39 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 654 | 40 | | avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate; |
| 654 | 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 | | { |
| 654 | 52 | | Cleanup(); |
| | 53 | |
|
| 654 | 54 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 2 | 55 | | poolableObject.RemoveFromPool(); |
| | 56 | |
|
| 654 | 57 | | avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate; |
| 654 | 58 | | } |
| | 59 | |
|
| | 60 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 61 | | { |
| 2 | 62 | | DisablePassport(); |
| | 63 | |
|
| 2 | 64 | | var model = (AvatarModel) newModel; |
| | 65 | |
|
| 2 | 66 | | everythingIsLoaded = false; |
| | 67 | |
|
| 2 | 68 | | bool avatarDone = false; |
| 2 | 69 | | bool avatarFailed = false; |
| | 70 | |
|
| 2 | 71 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 72 | |
|
| 1 | 73 | | avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true); |
| | 74 | |
|
| 2 | 75 | | yield return new WaitUntil(() => avatarDone || avatarFailed); |
| | 76 | |
|
| 0 | 77 | | onPointerDown.Initialize( |
| | 78 | | new OnPointerDown.Model() |
| | 79 | | { |
| | 80 | | type = OnPointerDown.NAME, |
| | 81 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 82 | | hoverText = "view profile" |
| | 83 | | }, |
| | 84 | | entity |
| | 85 | | ); |
| | 86 | |
|
| 0 | 87 | | entity.OnTransformChange -= avatarMovementController.OnTransformChanged; |
| 0 | 88 | | entity.OnTransformChange += avatarMovementController.OnTransformChanged; |
| | 89 | |
|
| 0 | 90 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 0 | 91 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 92 | |
|
| 0 | 93 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 0 | 94 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| | 95 | |
|
| | 96 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 0 | 97 | | if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM)) |
| | 98 | | { |
| 0 | 99 | | initializedPosition = true; |
| | 100 | |
|
| 0 | 101 | | avatarMovementController.MoveTo( |
| | 102 | | entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle |
| | 103 | | entity.gameObject.transform.localRotation, true); |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | UpdatePlayerStatus(model); |
| | 107 | |
|
| 0 | 108 | | avatarCollider.gameObject.SetActive(true); |
| | 109 | |
|
| 0 | 110 | | everythingIsLoaded = true; |
| 0 | 111 | | OnAvatarShapeUpdated?.Invoke(entity, this); |
| | 112 | |
|
| 0 | 113 | | EnablePassport(); |
| | 114 | |
|
| 0 | 115 | | KernelConfig.i.EnsureConfigInitialized() |
| | 116 | | .Then(config => |
| | 117 | | { |
| 0 | 118 | | if(config.features.enableAvatarLODs) |
| 0 | 119 | | avatarRenderer.InitializeImpostor(); |
| 0 | 120 | | }); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void UpdatePlayerStatus(AvatarModel model) |
| | 124 | | { |
| | 125 | | // Remove the player status if the userId changes |
| 0 | 126 | | if (player != null && (player.id != model.id || player.name != model.name)) |
| 0 | 127 | | otherPlayers.Remove(player.id); |
| | 128 | |
|
| 0 | 129 | | if (string.IsNullOrEmpty(model?.id)) |
| 0 | 130 | | return; |
| | 131 | |
|
| 0 | 132 | | bool isNew = false; |
| 0 | 133 | | if (player == null) |
| | 134 | | { |
| 0 | 135 | | player = new Player(); |
| 0 | 136 | | isNew = true; |
| | 137 | | } |
| 0 | 138 | | player.id = model.id; |
| 0 | 139 | | player.name = model.name; |
| 0 | 140 | | player.isTalking = model.talking; |
| 0 | 141 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 142 | | player.renderer = avatarRenderer; |
| 0 | 143 | | if (isNew) |
| | 144 | | { |
| 0 | 145 | | visibleNames.Add(player.id); |
| 0 | 146 | | otherPlayers.Add(player.id, player); |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | private void Update() |
| | 151 | | { |
| 1 | 152 | | if (player != null) |
| | 153 | | { |
| 0 | 154 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 155 | | player.forwardDirection = entity.gameObject.transform.forward; |
| | 156 | | } |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | public void DisablePassport() |
| | 160 | | { |
| 2 | 161 | | if (onPointerDown.collider == null) |
| 0 | 162 | | return; |
| | 163 | |
|
| 2 | 164 | | onPointerDown.collider.enabled = false; |
| 2 | 165 | | } |
| | 166 | |
|
| | 167 | | public void EnablePassport() |
| | 168 | | { |
| 0 | 169 | | if (onPointerDown.collider == null) |
| 0 | 170 | | return; |
| | 171 | |
|
| 0 | 172 | | onPointerDown.collider.enabled = true; |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private void OnEntityTransformChanged(object newModel) |
| | 176 | | { |
| 0 | 177 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 178 | | lastAvatarPosition = newTransformModel.position; |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public override void OnPoolGet() |
| | 182 | | { |
| 2 | 183 | | base.OnPoolGet(); |
| | 184 | |
|
| 2 | 185 | | everythingIsLoaded = false; |
| 2 | 186 | | initializedPosition = false; |
| 2 | 187 | | oldModel = new AvatarModel(); |
| 2 | 188 | | model = new AvatarModel(); |
| 2 | 189 | | lastAvatarPosition = null; |
| 2 | 190 | | player = null; |
| 2 | 191 | | } |
| | 192 | |
|
| 0 | 193 | | void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue |
| | 194 | |
|
| | 195 | | public override void Cleanup() |
| | 196 | | { |
| 658 | 197 | | base.Cleanup(); |
| | 198 | |
|
| 658 | 199 | | if (player != null) |
| | 200 | | { |
| 0 | 201 | | otherPlayers.Remove(player.id); |
| 0 | 202 | | player = null; |
| | 203 | | } |
| | 204 | |
|
| 658 | 205 | | avatarRenderer.CleanupAvatar(); |
| | 206 | |
|
| 658 | 207 | | if (poolableObject != null) |
| | 208 | | { |
| 6 | 209 | | poolableObject.OnRelease -= Cleanup; |
| | 210 | | } |
| | 211 | |
|
| 658 | 212 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| | 213 | |
|
| 658 | 214 | | if (entity != null) |
| | 215 | | { |
| 2 | 216 | | entity.OnTransformChange = null; |
| 2 | 217 | | entity = null; |
| | 218 | | } |
| 658 | 219 | | } |
| | 220 | |
|
| 0 | 221 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 222 | | } |
| | 223 | | } |