| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.CameraTool; |
| | 3 | | using DCL.NotificationModel; |
| | 4 | |
|
| | 5 | | namespace DCL.Components |
| | 6 | | { |
| | 7 | | public interface ICameraModeAreasController |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// This will change the camera to the passed mode, |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="area"></param> |
| | 13 | | /// <param name="mode"></param> |
| | 14 | | void ChangeAreaMode(in ICameraModeArea area); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Adds an area |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="area"></param> |
| | 20 | | void AddInsideArea(in ICameraModeArea area); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Remove an area |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="area"></param> |
| | 26 | | void RemoveInsideArea(in ICameraModeArea area); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public class CameraModeAreasController : ICameraModeAreasController |
| | 30 | | { |
| | 31 | | private const string NOTIFICATION_GROUP = "CameraModeLockedByScene"; |
| | 32 | | private const float NOTIFICATION_TIME = 3; |
| | 33 | |
|
| 1 | 34 | | private static readonly Model notificationModel = new Model() |
| | 35 | | { |
| | 36 | | type = Type.CAMERA_MODE_LOCKED_BY_SCENE, |
| | 37 | | groupID = NOTIFICATION_GROUP, |
| | 38 | | timer = NOTIFICATION_TIME |
| | 39 | | }; |
| | 40 | |
|
| | 41 | | private CameraMode.ModeId initialCameraMode; |
| 4 | 42 | | private readonly List<ICameraModeArea> insideAreasList = new List<ICameraModeArea>(); |
| | 43 | |
|
| | 44 | | public void AddInsideArea(in ICameraModeArea area) |
| | 45 | | { |
| 17 | 46 | | if (!IsPlayerInsideAnyArea()) |
| | 47 | | { |
| 10 | 48 | | initialCameraMode = CommonScriptableObjects.cameraMode.Get(); |
| 10 | 49 | | CommonScriptableObjects.cameraModeInputLocked.Set(true); |
| 10 | 50 | | ShowCameraModeLockedNotification(); |
| | 51 | | } |
| | 52 | |
|
| 17 | 53 | | CommonScriptableObjects.cameraMode.Set(area.cameraMode); |
| 17 | 54 | | insideAreasList.Add(area); |
| 17 | 55 | | } |
| | 56 | |
|
| | 57 | | public void RemoveInsideArea(in ICameraModeArea area) |
| | 58 | | { |
| 17 | 59 | | int affectingAreasCount = insideAreasList.Count; |
| | 60 | |
|
| 17 | 61 | | if (affectingAreasCount == 0) |
| | 62 | | { |
| 0 | 63 | | return; |
| | 64 | | } |
| | 65 | |
|
| 17 | 66 | | if (affectingAreasCount == 1) |
| | 67 | | { |
| | 68 | | // reset to initial state of camera mode |
| 10 | 69 | | ResetCameraMode(); |
| | 70 | |
|
| | 71 | | //remove notification |
| 10 | 72 | | HideCameraModeLockedNotification(); |
| 10 | 73 | | } |
| 7 | 74 | | else if (IsTheActivelyAffectingArea(area)) |
| | 75 | | { |
| | 76 | | // set camera mode to the previous area the player is in |
| 1 | 77 | | CommonScriptableObjects.cameraMode.Set(insideAreasList[affectingAreasCount - 2].cameraMode); |
| | 78 | | } |
| | 79 | |
|
| 17 | 80 | | insideAreasList.Remove(area); |
| 17 | 81 | | } |
| | 82 | |
|
| | 83 | | public void ChangeAreaMode(in ICameraModeArea area) |
| | 84 | | { |
| 2 | 85 | | if (IsTheActivelyAffectingArea(area)) |
| | 86 | | { |
| 2 | 87 | | CommonScriptableObjects.cameraMode.Set(area.cameraMode); |
| | 88 | | } |
| 2 | 89 | | } |
| | 90 | |
|
| | 91 | | private bool IsPlayerInsideAnyArea() |
| | 92 | | { |
| 0 | 93 | | return insideAreasList.Count > 0; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | private void ResetCameraMode() |
| | 97 | | { |
| 10 | 98 | | CommonScriptableObjects.cameraMode.Set(initialCameraMode); |
| 10 | 99 | | CommonScriptableObjects.cameraModeInputLocked.Set(false); |
| 10 | 100 | | } |
| | 101 | |
|
| | 102 | | private bool IsTheActivelyAffectingArea(in ICameraModeArea area) |
| | 103 | | { |
| 9 | 104 | | int affectingAreasCount = insideAreasList.Count; |
| | 105 | |
|
| 9 | 106 | | if (affectingAreasCount == 0) |
| | 107 | | { |
| 0 | 108 | | return false; |
| | 109 | | } |
| | 110 | |
|
| 9 | 111 | | return insideAreasList[affectingAreasCount - 1] == area; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | internal virtual void ShowCameraModeLockedNotification() |
| | 115 | | { |
| 10 | 116 | | NotificationsController.i?.ShowNotification(notificationModel); |
| 10 | 117 | | } |
| | 118 | |
|
| | 119 | | internal virtual void HideCameraModeLockedNotification() |
| | 120 | | { |
| 10 | 121 | | NotificationsController.i?.DismissAllNotifications(NOTIFICATION_GROUP); |
| 10 | 122 | | } |
| | 123 | | } |
| | 124 | | } |