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