| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using Variables.SpawnPoints; |
| | 6 | |
|
| | 7 | | internal interface ISpawnPointsDataHandler : IDisposable |
| | 8 | | { |
| | 9 | | void CreateSpawnPoints(in string sceneId, in SceneSpawnPoint[] spawnPoints); |
| | 10 | | void RemoveSpawnPoints(in string sceneId); |
| | 11 | | } |
| | 12 | |
|
| | 13 | | internal class SpawnPointsDataHandler : ISpawnPointsDataHandler |
| | 14 | | { |
| | 15 | | internal const string POINT_TYPE_NAME = "SpawnPoint"; |
| | 16 | | internal const string AREA_TYPE_NAME = "SpawnArea"; |
| | 17 | | internal const string DEFAULT_NAME = "Unnamed"; |
| | 18 | | internal const string NAME_DEFAULT_INDICATOR = "*"; |
| | 19 | | internal const string NAME_FORMAT = "{0}: {1}{2}"; |
| | 20 | |
|
| | 21 | | internal const float SIZE_MIN_XZ = 0.5f; |
| | 22 | | internal const float SIZE_MIN_Y = 2.0f; |
| | 23 | |
|
| 3 | 24 | | internal Dictionary<string, List<ISpawnPointIndicator>> indicatorsBySceneId = new Dictionary<string, List<ISpawnPoin |
| | 25 | | internal ISpawnPointIndicatorInstantiator instantiator; |
| | 26 | |
|
| 3 | 27 | | public SpawnPointsDataHandler(ISpawnPointIndicatorInstantiator instantiator) |
| | 28 | | { |
| 3 | 29 | | this.instantiator = instantiator; |
| 3 | 30 | | } |
| | 31 | |
|
| | 32 | | void ISpawnPointsDataHandler.CreateSpawnPoints(in string sceneId, in SceneSpawnPoint[] spawnPoints) |
| | 33 | | { |
| 2 | 34 | | List<ISpawnPointIndicator> sceneSpawnPoints = new List<ISpawnPointIndicator>(spawnPoints.Length); |
| 12 | 35 | | for (int i = 0; i < spawnPoints.Length; i++) |
| | 36 | | { |
| 4 | 37 | | var spawnPoint = spawnPoints[i]; |
| | 38 | |
|
| 4 | 39 | | var name = GetName(spawnPoint); |
| 4 | 40 | | var size = GetSize(spawnPoint); |
| 4 | 41 | | var position = GetPosition(spawnPoint, size); |
| 4 | 42 | | var rotation = GetLookAtRotation(spawnPoint, position); |
| | 43 | |
|
| 4 | 44 | | var indicator = instantiator.Instantiate(); |
| 4 | 45 | | indicator.SetName(name); |
| 4 | 46 | | indicator.SetPosition(position); |
| 4 | 47 | | indicator.SetSize(size); |
| 4 | 48 | | indicator.SetRotation(rotation); |
| | 49 | |
|
| 4 | 50 | | sceneSpawnPoints.Add(indicator); |
| | 51 | | } |
| 2 | 52 | | indicatorsBySceneId.Add(sceneId, sceneSpawnPoints); |
| 2 | 53 | | } |
| | 54 | |
|
| | 55 | | void ISpawnPointsDataHandler.RemoveSpawnPoints(in string sceneId) |
| | 56 | | { |
| 1 | 57 | | if (DisposeIndicatorsOfSceneId(sceneId, indicatorsBySceneId)) |
| | 58 | | { |
| 1 | 59 | | indicatorsBySceneId.Remove(sceneId); |
| | 60 | | } |
| 1 | 61 | | } |
| | 62 | |
|
| | 63 | | void IDisposable.Dispose() |
| | 64 | | { |
| 1 | 65 | | var sceneIds = indicatorsBySceneId.Keys.ToArray(); |
| 4 | 66 | | foreach (var sceneId in sceneIds) |
| | 67 | | { |
| 1 | 68 | | DisposeIndicatorsOfSceneId(sceneId, indicatorsBySceneId); |
| | 69 | | } |
| 1 | 70 | | indicatorsBySceneId.Clear(); |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | private static bool DisposeIndicatorsOfSceneId(in string sceneId, in IDictionary<string, List<ISpawnPointIndicator>> |
| | 74 | | { |
| 2 | 75 | | if (!indicatorsBySceneId.TryGetValue(sceneId, out List<ISpawnPointIndicator> indicators)) |
| | 76 | | { |
| 0 | 77 | | return false; |
| | 78 | | } |
| 16 | 79 | | foreach (var indicator in indicators) |
| | 80 | | { |
| 6 | 81 | | indicator.Dispose(); |
| | 82 | | } |
| 2 | 83 | | return true; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | internal static string GetName(in SceneSpawnPoint spawnPoint) |
| | 87 | | { |
| 8 | 88 | | string nameType = IsSpawnArea(spawnPoint) ? AREA_TYPE_NAME : POINT_TYPE_NAME; |
| 8 | 89 | | string name = string.IsNullOrEmpty(spawnPoint.name) ? DEFAULT_NAME : spawnPoint.name; |
| 8 | 90 | | string defaultIndicator = spawnPoint.@default.HasValue && spawnPoint.@default.Value ? NAME_DEFAULT_INDICATOR : s |
| 8 | 91 | | return string.Format(NAME_FORMAT, nameType, name, defaultIndicator); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | internal static Vector3 GetSize(in SceneSpawnPoint spawnPoint) |
| | 95 | | { |
| 7 | 96 | | float x = spawnPoint.position.x.Length > 1 ? (spawnPoint.position.x[1] - spawnPoint.position.x[0]) : 0; |
| 7 | 97 | | float y = spawnPoint.position.y.Length > 1 ? (spawnPoint.position.y[1] - spawnPoint.position.y[0]) : 0; |
| 7 | 98 | | float z = spawnPoint.position.z.Length > 1 ? (spawnPoint.position.z[1] - spawnPoint.position.z[0]) : 0; |
| | 99 | |
|
| 7 | 100 | | if (x < SIZE_MIN_XZ) |
| 5 | 101 | | x = SIZE_MIN_XZ; |
| | 102 | |
|
| 7 | 103 | | if (z < SIZE_MIN_XZ) |
| 7 | 104 | | z = SIZE_MIN_XZ; |
| | 105 | |
|
| 7 | 106 | | if (y < SIZE_MIN_Y) |
| 6 | 107 | | y = SIZE_MIN_Y; |
| | 108 | |
|
| 7 | 109 | | return new Vector3(x, y, z); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | internal static Vector3 GetPosition(in SceneSpawnPoint spawnPoint, in Vector3 size) |
| | 113 | | { |
| 6 | 114 | | float x = spawnPoint.position.x[0] + size.x * 0.5f; |
| 6 | 115 | | float y = spawnPoint.position.y[0] + size.y * 0.5f; |
| 6 | 116 | | float z = spawnPoint.position.z[0] + size.z * 0.5f; |
| | 117 | |
|
| 6 | 118 | | return new Vector3(x, y, z); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | internal static Quaternion? GetLookAtRotation(in SceneSpawnPoint spawnPoint, in Vector3 position) |
| | 122 | | { |
| 7 | 123 | | if (spawnPoint.cameraTarget == null) |
| | 124 | | { |
| 5 | 125 | | return null; |
| | 126 | | } |
| | 127 | |
|
| 2 | 128 | | return Quaternion.LookRotation(spawnPoint.cameraTarget.Value - position); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | private static bool IsSpawnArea(in SceneSpawnPoint spawnPoint) |
| | 132 | | { |
| 8 | 133 | | return spawnPoint.position.x.Length > 1 || spawnPoint.position.y.Length > 1 || spawnPoint.position.y.Length > 1; |
| | 134 | | } |
| | 135 | | } |