| | 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 | | private AvatarOnPointerDown onPointerDown; |
| | 22 | |
|
| | 23 | | private StringVariable currentPlayerInfoCardId; |
| | 24 | |
|
| 654 | 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 | | { |
| 653 | 38 | | model = new AvatarModel(); |
| 653 | 39 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 653 | 40 | | avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate; |
| 653 | 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 | | { |
| 653 | 52 | | Cleanup(); |
| | 53 | |
|
| 653 | 54 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 1 | 55 | | poolableObject.RemoveFromPool(); |
| | 56 | |
|
| 653 | 57 | | avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate; |
| 653 | 58 | | } |
| | 59 | |
|
| | 60 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 61 | | { |
| 1 | 62 | | DisablePassport(); |
| | 63 | |
|
| 1 | 64 | | var model = (AvatarModel) newModel; |
| | 65 | |
|
| 1 | 66 | | everythingIsLoaded = false; |
| | 67 | |
|
| 1 | 68 | | bool avatarDone = false; |
| 1 | 69 | | bool avatarFailed = false; |
| | 70 | |
|
| 1 | 71 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 72 | |
|
| 0 | 73 | | avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true); |
| | 74 | |
|
| 0 | 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 | | avatarRenderer.InitializeImpostor(); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void UpdatePlayerStatus(AvatarModel model) |
| | 119 | | { |
| | 120 | | // Remove the player status if the userId changes |
| 0 | 121 | | if (player != null && (player.id != model.id || player.name != model.name)) |
| 0 | 122 | | otherPlayers.Remove(player.id); |
| | 123 | |
|
| 0 | 124 | | if (string.IsNullOrEmpty(model?.id)) |
| 0 | 125 | | return; |
| | 126 | |
|
| 0 | 127 | | bool isNew = false; |
| 0 | 128 | | if (player == null) |
| | 129 | | { |
| 0 | 130 | | player = new Player(); |
| 0 | 131 | | isNew = true; |
| | 132 | | } |
| 0 | 133 | | player.id = model.id; |
| 0 | 134 | | player.name = model.name; |
| 0 | 135 | | player.isTalking = model.talking; |
| 0 | 136 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 137 | | player.renderer = avatarRenderer; |
| 0 | 138 | | if (isNew) |
| | 139 | | { |
| 0 | 140 | | visibleNames.Add(player.id); |
| 0 | 141 | | otherPlayers.Add(player.id, player); |
| | 142 | | } |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private void Update() |
| | 146 | | { |
| 0 | 147 | | if (player != null) |
| | 148 | | { |
| 0 | 149 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 150 | | player.forwardDirection = entity.gameObject.transform.forward; |
| | 151 | | } |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | public void DisablePassport() |
| | 155 | | { |
| 1 | 156 | | if (onPointerDown.collider == null) |
| 0 | 157 | | return; |
| | 158 | |
|
| 1 | 159 | | onPointerDown.collider.enabled = false; |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | public void EnablePassport() |
| | 163 | | { |
| 0 | 164 | | if (onPointerDown.collider == null) |
| 0 | 165 | | return; |
| | 166 | |
|
| 0 | 167 | | onPointerDown.collider.enabled = true; |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private void OnEntityTransformChanged(object newModel) |
| | 171 | | { |
| 0 | 172 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 173 | | lastAvatarPosition = newTransformModel.position; |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | public override void OnPoolGet() |
| | 177 | | { |
| 1 | 178 | | base.OnPoolGet(); |
| | 179 | |
|
| 1 | 180 | | everythingIsLoaded = false; |
| 1 | 181 | | initializedPosition = false; |
| 1 | 182 | | oldModel = new AvatarModel(); |
| 1 | 183 | | model = new AvatarModel(); |
| 1 | 184 | | lastAvatarPosition = null; |
| 1 | 185 | | player = null; |
| 1 | 186 | | } |
| | 187 | |
|
| 0 | 188 | | void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue |
| | 189 | |
|
| | 190 | | public override void Cleanup() |
| | 191 | | { |
| 655 | 192 | | base.Cleanup(); |
| | 193 | |
|
| 655 | 194 | | if (player != null) |
| | 195 | | { |
| 0 | 196 | | otherPlayers.Remove(player.id); |
| 0 | 197 | | player = null; |
| | 198 | | } |
| | 199 | |
|
| 655 | 200 | | avatarRenderer.CleanupAvatar(); |
| | 201 | |
|
| 655 | 202 | | if (poolableObject != null) |
| | 203 | | { |
| 3 | 204 | | poolableObject.OnRelease -= Cleanup; |
| | 205 | | } |
| | 206 | |
|
| 655 | 207 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| | 208 | |
|
| 655 | 209 | | if (entity != null) |
| | 210 | | { |
| 1 | 211 | | entity.OnTransformChange = null; |
| 1 | 212 | | entity = null; |
| | 213 | | } |
| 655 | 214 | | } |
| | 215 | |
|
| 0 | 216 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 217 | | } |
| | 218 | | } |