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