| | 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 | | public class AvatarReporterController : IAvatarReporterController |
| | 10 | | { |
| | 11 | | private string avatarId; |
| | 12 | | private string lastSceneId; |
| | 13 | | private Vector2Int lastCoords; |
| | 14 | | private Vector3 lastPositionChecked; |
| 404 | 15 | | private bool isInitialReport = true; |
| | 16 | |
|
| | 17 | | private readonly IWorldState worldState; |
| | 18 | |
|
| 443 | 19 | | IReporter IAvatarReporterController.reporter { get; set; } = new Reporter(); |
| | 20 | |
|
| 404 | 21 | | public AvatarReporterController(IWorldState worldState) |
| | 22 | | { |
| 404 | 23 | | this.worldState = worldState; |
| 404 | 24 | | } |
| | 25 | |
|
| | 26 | | void IAvatarReporterController.SetUp(string sceneId, string avatarId) |
| | 27 | | { |
| | 28 | | // NOTE: do not report avatars that doesn't belong to the global scene |
| 9 | 29 | | if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID) |
| 1 | 30 | | return; |
| | 31 | |
|
| 8 | 32 | | this.avatarId = avatarId; |
| 8 | 33 | | isInitialReport = true; |
| 8 | 34 | | lastSceneId = null; |
| 8 | 35 | | } |
| | 36 | |
|
| | 37 | | string GetcurrentSceneIdNonAlloc(Vector2Int coords) |
| | 38 | | { |
| 45 | 39 | | foreach (KeyValuePair<string, IParcelScene> parcelScene in worldState.loadedScenes) |
| | 40 | | { |
| 14 | 41 | | var parcels = parcelScene.Value.sceneData.parcels; |
| | 42 | |
|
| 14 | 43 | | if (parcels != null && parcels.Contains(coords)) |
| | 44 | | { |
| 7 | 45 | | return parcelScene.Key; |
| | 46 | | } |
| | 47 | | } |
| | 48 | |
|
| 5 | 49 | | return null; |
| 7 | 50 | | } |
| | 51 | |
|
| | 52 | | void IAvatarReporterController.ReportAvatarPosition(Vector3 position) |
| | 53 | | { |
| 16 | 54 | | if (!CanReport()) |
| 3 | 55 | | return; |
| | 56 | |
|
| 13 | 57 | | bool wasInLoadedScene = WasInLoadedScene(); |
| | 58 | |
|
| 13 | 59 | | if (wasInLoadedScene && !HasMoved(position)) |
| 1 | 60 | | return; |
| | 61 | |
|
| 12 | 62 | | Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position); |
| | 63 | |
|
| 12 | 64 | | if (wasInLoadedScene && lastCoords == coords) |
| | 65 | | { |
| 0 | 66 | | return; |
| | 67 | | } |
| | 68 | |
|
| 12 | 69 | | string currentSceneId = GetcurrentSceneIdNonAlloc(coords); |
| | 70 | |
|
| 12 | 71 | | if (currentSceneId == lastSceneId && !isInitialReport) |
| | 72 | | { |
| 1 | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| 11 | 76 | | ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(avatarId, currentSceneId); |
| | 77 | |
|
| 11 | 78 | | lastSceneId = currentSceneId; |
| 11 | 79 | | lastCoords = coords; |
| 11 | 80 | | lastPositionChecked = position; |
| 11 | 81 | | isInitialReport = false; |
| 11 | 82 | | } |
| | 83 | |
|
| | 84 | | void IAvatarReporterController.ReportAvatarRemoved() |
| | 85 | | { |
| 405 | 86 | | if (!CanReport()) |
| 404 | 87 | | return; |
| | 88 | |
|
| 1 | 89 | | ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(avatarId); |
| | 90 | |
|
| 1 | 91 | | avatarId = null; |
| 1 | 92 | | lastSceneId = null; |
| 1 | 93 | | isInitialReport = true; |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | private bool CanReport() |
| | 97 | | { |
| 0 | 98 | | return !string.IsNullOrEmpty(avatarId); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | private bool HasMoved(Vector3 currentPosition) |
| | 102 | | { |
| 3 | 103 | | return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private bool WasInLoadedScene() |
| | 107 | | { |
| 0 | 108 | | return !string.IsNullOrEmpty(lastSceneId); |
| | 109 | | } |
| | 110 | | } |