| | 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; |
| | 34 | |
|
| | 35 | | private void Awake() |
| | 36 | | { |
| 1 | 37 | | model = new AvatarModel(); |
| 1 | 38 | | currentPlayerInfoCardId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 1 | 39 | | avatarRenderer.OnImpostorAlphaValueUpdate += OnImpostorAlphaValueUpdate; |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | private void PlayerClicked() |
| | 43 | | { |
| 0 | 44 | | if (model == null) |
| 0 | 45 | | return; |
| 0 | 46 | | currentPlayerInfoCardId.Set(((AvatarModel) model).id); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void OnDestroy() |
| | 50 | | { |
| 1 | 51 | | Cleanup(); |
| | 52 | |
|
| 1 | 53 | | if (poolableObject != null && poolableObject.isInsidePool) |
| 1 | 54 | | poolableObject.RemoveFromPool(); |
| | 55 | |
|
| 1 | 56 | | avatarRenderer.OnImpostorAlphaValueUpdate -= OnImpostorAlphaValueUpdate; |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 60 | | { |
| 1 | 61 | | DisablePassport(); |
| | 62 | |
|
| 1 | 63 | | var model = (AvatarModel) newModel; |
| | 64 | |
|
| 1 | 65 | | everythingIsLoaded = false; |
| | 66 | |
|
| 1 | 67 | | bool avatarDone = false; |
| 1 | 68 | | bool avatarFailed = false; |
| | 69 | |
|
| 1 | 70 | | yield return null; //NOTE(Brian): just in case we have a Object.Destroy waiting to be resolved. |
| | 71 | |
|
| 0 | 72 | | avatarRenderer.ApplyModel(model, () => avatarDone = true, () => avatarFailed = true); |
| | 73 | |
|
| 0 | 74 | | yield return new WaitUntil(() => avatarDone || avatarFailed); |
| | 75 | |
|
| 0 | 76 | | onPointerDown.Initialize( |
| | 77 | | new OnPointerDown.Model() |
| | 78 | | { |
| | 79 | | type = OnPointerDown.NAME, |
| | 80 | | button = WebInterface.ACTION_BUTTON.POINTER.ToString(), |
| | 81 | | hoverText = "view profile" |
| | 82 | | }, |
| | 83 | | entity |
| | 84 | | ); |
| | 85 | |
|
| 0 | 86 | | entity.OnTransformChange -= avatarMovementController.OnTransformChanged; |
| 0 | 87 | | entity.OnTransformChange += avatarMovementController.OnTransformChanged; |
| | 88 | |
|
| 0 | 89 | | entity.OnTransformChange -= OnEntityTransformChanged; |
| 0 | 90 | | entity.OnTransformChange += OnEntityTransformChanged; |
| | 91 | |
|
| 0 | 92 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| 0 | 93 | | onPointerDown.OnPointerDownReport += PlayerClicked; |
| | 94 | |
|
| | 95 | | // To deal with the cases in which the entity transform was configured before the AvatarShape |
| 0 | 96 | | if (!initializedPosition && entity.components.ContainsKey(DCL.Models.CLASS_ID_COMPONENT.TRANSFORM)) |
| | 97 | | { |
| 0 | 98 | | initializedPosition = true; |
| | 99 | |
|
| 0 | 100 | | avatarMovementController.MoveTo( |
| | 101 | | entity.gameObject.transform.localPosition - Vector3.up * DCLCharacterController.i.characterControlle |
| | 102 | | entity.gameObject.transform.localRotation, true); |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | UpdatePlayerStatus(model); |
| | 106 | |
|
| 0 | 107 | | avatarCollider.gameObject.SetActive(true); |
| | 108 | |
|
| 0 | 109 | | everythingIsLoaded = true; |
| 0 | 110 | | OnAvatarShapeUpdated?.Invoke(entity, this); |
| | 111 | |
|
| 0 | 112 | | EnablePassport(); |
| | 113 | |
|
| 0 | 114 | | avatarRenderer.InitializeImpostor(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void UpdatePlayerStatus(AvatarModel model) |
| | 118 | | { |
| | 119 | | // Remove the player status if the userId changes |
| 0 | 120 | | if (player != null && (player.id != model.id || player.name != model.name)) |
| 0 | 121 | | otherPlayers.Remove(player.id); |
| | 122 | |
|
| 0 | 123 | | if (string.IsNullOrEmpty(model?.id)) |
| 0 | 124 | | return; |
| | 125 | |
|
| 0 | 126 | | bool isNew = false; |
| 0 | 127 | | if (player == null) |
| | 128 | | { |
| 0 | 129 | | player = new Player(); |
| 0 | 130 | | isNew = true; |
| | 131 | | } |
| 0 | 132 | | player.id = model.id; |
| 0 | 133 | | player.name = model.name; |
| 0 | 134 | | player.isTalking = model.talking; |
| 0 | 135 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 136 | | player.renderer = avatarRenderer; |
| 0 | 137 | | if (isNew) |
| 0 | 138 | | otherPlayers.Add(player.id, player); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void Update() |
| | 142 | | { |
| 0 | 143 | | if (player != null) |
| | 144 | | { |
| 0 | 145 | | player.worldPosition = entity.gameObject.transform.position; |
| 0 | 146 | | player.forwardDirection = entity.gameObject.transform.forward; |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void DisablePassport() |
| | 151 | | { |
| 1 | 152 | | if (onPointerDown.collider == null) |
| 0 | 153 | | return; |
| | 154 | |
|
| 1 | 155 | | onPointerDown.collider.enabled = false; |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | public void EnablePassport() |
| | 159 | | { |
| 0 | 160 | | if (onPointerDown.collider == null) |
| 0 | 161 | | return; |
| | 162 | |
|
| 0 | 163 | | onPointerDown.collider.enabled = true; |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private void OnEntityTransformChanged(object newModel) |
| | 167 | | { |
| 0 | 168 | | DCLTransform.Model newTransformModel = (DCLTransform.Model)newModel; |
| 0 | 169 | | lastAvatarPosition = newTransformModel.position; |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public override void OnPoolGet() |
| | 173 | | { |
| 1 | 174 | | base.OnPoolGet(); |
| | 175 | |
|
| 1 | 176 | | everythingIsLoaded = false; |
| 1 | 177 | | initializedPosition = false; |
| 1 | 178 | | oldModel = new AvatarModel(); |
| 1 | 179 | | model = new AvatarModel(); |
| 1 | 180 | | lastAvatarPosition = null; |
| 1 | 181 | | player = null; |
| 1 | 182 | | } |
| | 183 | |
|
| 0 | 184 | | void OnImpostorAlphaValueUpdate(float newAlphaValue) { avatarMovementController.movementLerpWait = newAlphaValue |
| | 185 | |
|
| | 186 | | public override void Cleanup() |
| | 187 | | { |
| 3 | 188 | | base.Cleanup(); |
| | 189 | |
|
| 3 | 190 | | if (player != null) |
| | 191 | | { |
| 0 | 192 | | otherPlayers.Remove(player.id); |
| 0 | 193 | | player = null; |
| | 194 | | } |
| | 195 | |
|
| 3 | 196 | | avatarRenderer.CleanupAvatar(); |
| | 197 | |
|
| 3 | 198 | | if (poolableObject != null) |
| | 199 | | { |
| 3 | 200 | | poolableObject.OnRelease -= Cleanup; |
| | 201 | | } |
| | 202 | |
|
| 3 | 203 | | onPointerDown.OnPointerDownReport -= PlayerClicked; |
| | 204 | |
|
| 3 | 205 | | if (entity != null) |
| | 206 | | { |
| 1 | 207 | | entity.OnTransformChange = null; |
| 1 | 208 | | entity = null; |
| | 209 | | } |
| 3 | 210 | | } |
| | 211 | |
|
| 0 | 212 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_SHAPE; } |
| | 213 | | } |
| | 214 | | } |