| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Configuration; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// This controller checks if an avatar entered or exited a scene by checking its position every frame |
| | 11 | | /// </summary> |
| | 12 | | public class AvatarReporterController : IAvatarReporterController |
| | 13 | | { |
| | 14 | | private string avatarId; |
| | 15 | | private string lastSceneId; |
| | 16 | | private Vector2Int lastCoords; |
| | 17 | | private Vector3 lastPositionChecked; |
| 419 | 18 | | private bool isInitialReport = true; |
| | 19 | |
|
| | 20 | | private readonly IWorldState worldState; |
| | 21 | |
|
| 419 | 22 | | IReporter IAvatarReporterController.reporter { get; set; } = new Reporter(); |
| | 23 | |
|
| 419 | 24 | | public AvatarReporterController(IWorldState worldState) |
| | 25 | | { |
| 419 | 26 | | this.worldState = worldState; |
| 419 | 27 | | } |
| | 28 | |
|
| | 29 | | void IAvatarReporterController.SetUp(string sceneId, string avatarId) |
| | 30 | | { |
| | 31 | | // NOTE: do not report avatars that doesn't belong to the global scene |
| 0 | 32 | | if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID) |
| 0 | 33 | | return; |
| | 34 | |
|
| 0 | 35 | | this.avatarId = avatarId; |
| 0 | 36 | | isInitialReport = true; |
| 0 | 37 | | lastSceneId = null; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | string GetcurrentSceneIdNonAlloc(Vector2Int coords) |
| | 41 | | { |
| 0 | 42 | | return worldState.GetSceneIdByCoords(coords); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | void IAvatarReporterController.ReportAvatarPosition(Vector3 position) |
| | 46 | | { |
| 0 | 47 | | if (!CanReport()) |
| 0 | 48 | | return; |
| | 49 | |
|
| 0 | 50 | | bool wasInLoadedScene = WasInLoadedScene(); |
| | 51 | |
|
| 0 | 52 | | if (wasInLoadedScene && !HasMoved(position)) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position); |
| | 56 | |
|
| 0 | 57 | | if (wasInLoadedScene && lastCoords == coords) |
| | 58 | | { |
| 0 | 59 | | return; |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | string currentSceneId = GetcurrentSceneIdNonAlloc(coords); |
| | 63 | |
|
| 0 | 64 | | if (currentSceneId == lastSceneId && !isInitialReport) |
| | 65 | | { |
| 0 | 66 | | return; |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(avatarId, currentSceneId); |
| | 70 | |
|
| 0 | 71 | | lastSceneId = currentSceneId; |
| 0 | 72 | | lastCoords = coords; |
| 0 | 73 | | lastPositionChecked = position; |
| 0 | 74 | | isInitialReport = false; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | void IAvatarReporterController.ReportAvatarRemoved() |
| | 78 | | { |
| 424 | 79 | | if (!CanReport()) |
| 424 | 80 | | return; |
| | 81 | |
|
| 0 | 82 | | ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(avatarId); |
| | 83 | |
|
| 0 | 84 | | avatarId = null; |
| 0 | 85 | | lastSceneId = null; |
| 0 | 86 | | isInitialReport = true; |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private bool CanReport() |
| | 90 | | { |
| 0 | 91 | | return !string.IsNullOrEmpty(avatarId); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private bool HasMoved(Vector3 currentPosition) |
| | 95 | | { |
| 0 | 96 | | return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private bool WasInLoadedScene() |
| | 100 | | { |
| 0 | 101 | | return !string.IsNullOrEmpty(lastSceneId); |
| | 102 | | } |
| | 103 | | } |