| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using Decentraland.Sdk.Ecs6; |
| | 6 | |
|
| | 7 | | namespace DCL.Components |
| | 8 | | { |
| | 9 | | public abstract class BaseComponent : MonoBehaviour, IEntityComponent, IDelayedComponent, IPoolLifecycleHandler, IPo |
| | 10 | | { |
| | 11 | | protected ComponentUpdateHandler updateHandler; |
| 0 | 12 | | public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction; |
| 102 | 13 | | public Coroutine routine => updateHandler.routine; |
| 0 | 14 | | public bool isRoutineRunning => updateHandler.isRoutineRunning; |
| | 15 | |
|
| 292 | 16 | | public IParcelScene scene { get; set; } |
| | 17 | |
|
| 47072 | 18 | | public IDCLEntity entity { get; set; } |
| | 19 | |
|
| 756 | 20 | | public IPoolableObject poolableObject { get; set; } |
| | 21 | |
|
| 81 | 22 | | string IComponent.componentName => componentName; |
| | 23 | |
|
| | 24 | | public abstract string componentName { get; } |
| | 25 | |
|
| | 26 | | protected BaseModel model; |
| | 27 | |
|
| 155 | 28 | | public void RaiseOnAppliedChanges() { } |
| | 29 | |
|
| | 30 | | public virtual void Initialize(IParcelScene scene, IDCLEntity entity) |
| | 31 | | { |
| 112 | 32 | | this.scene = scene; |
| 112 | 33 | | this.entity = entity; |
| | 34 | |
|
| 112 | 35 | | if (transform.parent != entity.gameObject.transform) |
| 112 | 36 | | transform.SetParent(entity.gameObject.transform, false); |
| 112 | 37 | | } |
| | 38 | |
|
| 310 | 39 | | public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); } |
| | 40 | |
|
| | 41 | | public virtual void UpdateFromPb(ComponentBodyPayload payload) |
| | 42 | | { |
| 0 | 43 | | UpdateFromModel(model.GetDataFromPb(payload)); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public virtual void UpdateFromModel(BaseModel newModel) |
| | 47 | | { |
| 157 | 48 | | model = newModel; |
| 157 | 49 | | updateHandler.ApplyChangesIfModified(model); |
| 157 | 50 | | } |
| | 51 | |
|
| | 52 | | public abstract IEnumerator ApplyChanges(BaseModel model); |
| | 53 | |
|
| | 54 | | protected void OnEnable() |
| | 55 | | { |
| 738 | 56 | | if (updateHandler == null) |
| 726 | 57 | | updateHandler = CreateUpdateHandler(); |
| 738 | 58 | | } |
| | 59 | |
|
| 2 | 60 | | public virtual BaseModel GetModel() => model; |
| | 61 | |
|
| 726 | 62 | | protected virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); } |
| | 63 | |
|
| 157 | 64 | | public bool IsValid() { return this != null; } |
| | 65 | |
|
| | 66 | | public virtual void Cleanup() |
| | 67 | | { |
| 700 | 68 | | updateHandler?.Cleanup(); |
| 700 | 69 | | } |
| | 70 | |
|
| 38 | 71 | | public virtual void OnPoolRelease() { Cleanup(); } |
| | 72 | |
|
| | 73 | | public virtual void OnPoolGet() |
| | 74 | | { |
| 12 | 75 | | if (updateHandler == null) |
| 0 | 76 | | updateHandler = CreateUpdateHandler(); |
| 12 | 77 | | } |
| | 78 | |
|
| | 79 | | public abstract int GetClassId(); |
| | 80 | |
|
| 31 | 81 | | public Transform GetTransform() => transform; |
| | 82 | | } |
| | 83 | | } |