| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Models; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class CameraModeArea : IEntityComponent, ICameraModeArea |
| | 12 | | { |
| | 13 | | [Serializable] |
| | 14 | | public class Model : BaseModel |
| | 15 | | { |
| | 16 | | [Serializable] |
| | 17 | | public class Area |
| | 18 | | { |
| | 19 | | public Vector3 box; |
| | 20 | | } |
| | 21 | |
|
| 20 | 22 | | public Area area = new Area(); |
| 20 | 23 | | public CameraMode.ModeId cameraMode = CameraMode.ModeId.ThirdPerson; |
| | 24 | |
|
| | 25 | | public override BaseModel GetDataFromJSON(string json) |
| | 26 | | { |
| 0 | 27 | | return Utils.SafeFromJson<Model>(json); |
| | 28 | | } |
| | 29 | | } |
| | 30 | |
|
| 1 | 31 | | private static CameraModeAreasController areasController { get; } = new CameraModeAreasController(); |
| | 32 | |
|
| | 33 | | private Collider playerCollider; |
| | 34 | | internal IUpdateEventHandler updateEventHandler; |
| 9 | 35 | | internal int validCameraModes = 1 << (int)CameraMode.ModeId.FirstPerson | 1 << (int)CameraMode.ModeId.ThirdPerso |
| | 36 | |
|
| | 37 | | internal bool isPlayerInside = false; |
| | 38 | |
|
| 9 | 39 | | public Model areaModel { private set; get; } = new Model(); |
| 0 | 40 | | public IParcelScene areaScene { private set; get; } |
| 1 | 41 | | public IDCLEntity areaEntity { private set; get; } |
| | 42 | |
|
| 8 | 43 | | CameraMode.ModeId ICameraModeArea.cameraMode => areaModel.cameraMode; |
| | 44 | |
|
| 0 | 45 | | IDCLEntity IEntityComponent.entity => areaEntity; |
| | 46 | |
|
| 0 | 47 | | IParcelScene IComponent.scene => areaScene; |
| | 48 | |
|
| 0 | 49 | | string IComponent.componentName => "CameraModeArea"; |
| | 50 | |
|
| | 51 | | void IComponent.UpdateFromJSON(string json) |
| | 52 | | { |
| 0 | 53 | | OnModelUpdated(areaModel.GetDataFromJSON(json) as Model); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | void IComponent.UpdateFromModel(BaseModel model) |
| | 57 | | { |
| 0 | 58 | | OnModelUpdated(model as Model); |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | IEnumerator IComponent.ApplyChanges(BaseModel model) => null; |
| | 62 | |
|
| 0 | 63 | | void IComponent.RaiseOnAppliedChanges() { } |
| | 64 | |
|
| 0 | 65 | | bool IComponent.IsValid() => true; |
| | 66 | |
|
| 0 | 67 | | BaseModel IComponent.GetModel() => areaModel; |
| | 68 | |
|
| 0 | 69 | | int IComponent.GetClassId() => (int)CLASS_ID_COMPONENT.CAMERA_MODE_AREA; |
| | 70 | |
|
| 0 | 71 | | Transform IMonoBehaviour.GetTransform() => areaEntity?.gameObject.transform; |
| | 72 | |
|
| | 73 | | void ICleanable.Cleanup() |
| | 74 | | { |
| 0 | 75 | | Dispose(); |
| 0 | 76 | | DataStore.i.player.ownPlayer.OnChange -= OnOwnPlayerChange; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | void IEntityComponent.Initialize(IParcelScene scene, IDCLEntity entity) |
| | 80 | | { |
| 0 | 81 | | Initialize(scene, entity, Environment.i.platform.updateEventHandler, DataStore.i.player.ownPlayer.Get()?.col |
| 0 | 82 | | DataStore.i.player.ownPlayer.OnChange += OnOwnPlayerChange; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | internal void OnModelUpdated(in Model newModel) |
| | 86 | | { |
| | 87 | | // NOTE: we don't want the ecs to be able to set "builder in world" camera or any other camera |
| | 88 | | // that might be added in the future. Only first and third person camera are allowed |
| 11 | 89 | | if (!IsValidCameraMode(newModel.cameraMode)) |
| | 90 | | { |
| 1 | 91 | | newModel.cameraMode = CommonScriptableObjects.cameraMode.Get(); |
| | 92 | | } |
| | 93 | |
|
| 11 | 94 | | bool cameraModeChanged = newModel.cameraMode != areaModel.cameraMode; |
| 11 | 95 | | areaModel = newModel; |
| | 96 | |
|
| 11 | 97 | | if (cameraModeChanged && isPlayerInside) |
| | 98 | | { |
| 1 | 99 | | areasController.ChangeAreaMode(this, areaModel.cameraMode); |
| | 100 | | } |
| 11 | 101 | | } |
| | 102 | |
|
| | 103 | | internal void Initialize(in IParcelScene scene, in IDCLEntity entity, in IUpdateEventHandler updateEventHandler, |
| | 104 | | { |
| 9 | 105 | | areaScene = scene; |
| 9 | 106 | | areaEntity = entity; |
| 9 | 107 | | this.updateEventHandler = updateEventHandler; |
| 9 | 108 | | this.playerCollider = playerCollider; |
| | 109 | |
|
| 9 | 110 | | updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 9 | 111 | | } |
| | 112 | |
|
| | 113 | | internal void Update() |
| | 114 | | { |
| 22 | 115 | | bool playerInside = IsPlayerInsideArea(); |
| | 116 | |
|
| | 117 | | switch (playerInside) |
| | 118 | | { |
| 11 | 119 | | case true when !isPlayerInside: |
| 8 | 120 | | areasController.AddInsideArea(this); |
| 8 | 121 | | break; |
| 11 | 122 | | case false when isPlayerInside: |
| 7 | 123 | | areasController.RemoveInsideArea(this); |
| | 124 | | break; |
| | 125 | | } |
| 22 | 126 | | isPlayerInside = playerInside; |
| 22 | 127 | | } |
| | 128 | |
|
| | 129 | | internal void Dispose() |
| | 130 | | { |
| 9 | 131 | | OnAreaDisabled(); |
| 9 | 132 | | updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 9 | 133 | | } |
| | 134 | |
|
| | 135 | | private bool IsValidCameraMode(in CameraMode.ModeId mode) |
| | 136 | | { |
| 11 | 137 | | if (validCameraModes == -1) |
| 9 | 138 | | return true; |
| | 139 | |
|
| 2 | 140 | | return ((1 << (int)mode) & validCameraModes) != 0; |
| | 141 | | } |
| | 142 | |
|
| | 143 | | private bool IsPlayerInsideArea() |
| | 144 | | { |
| 22 | 145 | | if (areaEntity == null || areaScene == null) |
| | 146 | | { |
| 0 | 147 | | return false; |
| | 148 | | } |
| | 149 | |
|
| 22 | 150 | | if (areaScene.sceneData.id != CommonScriptableObjects.sceneID.Get()) |
| | 151 | | { |
| 1 | 152 | | return false; |
| | 153 | | } |
| | 154 | |
|
| 21 | 155 | | Vector3 center = areaEntity.gameObject.transform.position; |
| 21 | 156 | | Quaternion rotation = areaEntity.gameObject.transform.rotation; |
| | 157 | |
|
| 21 | 158 | | Collider[] colliders = Physics.OverlapBox(center, areaModel.area.box * 0.5f, rotation, |
| | 159 | | PhysicsLayers.avatarTriggerMask, QueryTriggerInteraction.Collide); |
| | 160 | |
|
| 21 | 161 | | if (colliders.Length == 0) |
| | 162 | | { |
| 5 | 163 | | return false; |
| | 164 | | } |
| | 165 | |
|
| 84 | 166 | | for (int i = 0; i < colliders.Length; i++) |
| | 167 | | { |
| 37 | 168 | | if (colliders[i] == playerCollider) |
| | 169 | | { |
| 11 | 170 | | return true; |
| | 171 | | } |
| | 172 | | } |
| 5 | 173 | | return false; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | private void OnAreaDisabled() |
| | 177 | | { |
| 9 | 178 | | if (!isPlayerInside) |
| | 179 | | { |
| 8 | 180 | | return; |
| | 181 | | } |
| 1 | 182 | | areasController.RemoveInsideArea(this); |
| 1 | 183 | | isPlayerInside = false; |
| 1 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnOwnPlayerChange(Player current, Player previous) |
| | 187 | | { |
| 0 | 188 | | playerCollider = current.collider; |
| 0 | 189 | | } |
| | 190 | | } |
| | 191 | | } |