| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | [CreateAssetMenu(fileName = "MinimapMetadata", menuName = "MinimapMetadata")] |
| | 7 | | public class MinimapMetadata : ScriptableObject |
| | 8 | | { |
| | 9 | | public enum TileType |
| | 10 | | { |
| | 11 | | MyParcel = 0, |
| | 12 | | MyParcelsOnSale = 1, |
| | 13 | | MyEstates = 2, |
| | 14 | | MyEstatesOnSale = 3, |
| | 15 | | WithAccess = 4, |
| | 16 | | District = 5, |
| | 17 | | Contribution = 6, |
| | 18 | | Roads = 7, |
| | 19 | | Plaza = 8, |
| | 20 | | Taken = 9, |
| | 21 | | OnSale = 10, |
| | 22 | | Unowned = 11, |
| | 23 | | Background = 12, |
| | 24 | | Loading = 13, |
| | 25 | | } |
| | 26 | |
|
| | 27 | | private static MinimapMetadata minimapMetadata; |
| | 28 | |
|
| 1 | 29 | | private readonly HashSet<MinimapSceneInfo> scenesInfo = new (); |
| | 30 | |
|
| 1 | 31 | | private readonly Dictionary<Vector2Int, MinimapSceneInfo> sceneInfoMap = new (); |
| | 32 | |
|
| | 33 | | public event Action<MinimapSceneInfo> OnSceneInfoUpdated; |
| | 34 | |
|
| 96 | 35 | | public IReadOnlyCollection<MinimapSceneInfo> SceneInfos => scenesInfo; |
| | 36 | |
|
| | 37 | | public MinimapSceneInfo GetSceneInfo(int x, int y) => |
| 48 | 38 | | sceneInfoMap.TryGetValue(new Vector2Int(x, y), out MinimapSceneInfo result) |
| | 39 | | ? result |
| | 40 | | : null; |
| | 41 | |
|
| | 42 | | public void AddSceneInfo(MinimapSceneInfo sceneInfo) |
| | 43 | | { |
| 1 | 44 | | if (scenesInfo.Contains(sceneInfo)) |
| 0 | 45 | | return; |
| | 46 | |
|
| 1 | 47 | | int parcelsCount = sceneInfo.parcels.Count; |
| | 48 | |
|
| 8 | 49 | | for (var i = 0; i < parcelsCount; i++) |
| | 50 | | { |
| 3 | 51 | | if (sceneInfoMap.ContainsKey(sceneInfo.parcels[i])) |
| | 52 | |
|
| | 53 | | // NOTE: This removes outdated information for a particular parcel. Subsequent calls to update the |
| | 54 | | // information for a parcel must override previously submitted information. |
| 0 | 55 | | sceneInfoMap.Remove(sceneInfo.parcels[i]); |
| | 56 | |
|
| 3 | 57 | | sceneInfoMap.Add(sceneInfo.parcels[i], sceneInfo); |
| | 58 | | } |
| | 59 | |
|
| 1 | 60 | | scenesInfo.Add(sceneInfo); |
| | 61 | |
|
| | 62 | | // it's not clear why we invoke the callback if `scenesInfo` already contains the scene |
| 1 | 63 | | OnSceneInfoUpdated?.Invoke(sceneInfo); |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Clear() |
| | 67 | | { |
| 1 | 68 | | scenesInfo.Clear(); |
| 1 | 69 | | sceneInfoMap.Clear(); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | public static MinimapMetadata GetMetadata() |
| | 73 | | { |
| 168 | 74 | | if (minimapMetadata == null) |
| 1 | 75 | | minimapMetadata = Resources.Load<MinimapMetadata>("ScriptableObjects/MinimapMetadata"); |
| | 76 | |
|
| 168 | 77 | | return minimapMetadata; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | [Serializable] |
| | 81 | | public class MinimapSceneInfo : IEquatable<MinimapSceneInfo> |
| | 82 | | { |
| | 83 | | public string name; |
| | 84 | | public TileType type; |
| | 85 | | public List<Vector2Int> parcels; |
| | 86 | |
|
| | 87 | | public bool isPOI; |
| | 88 | | public string owner; |
| | 89 | | public string description; |
| | 90 | | public string previewImageUrl; |
| | 91 | |
|
| | 92 | | [NonSerialized] private int? cachedHash; |
| | 93 | |
|
| | 94 | | public bool Equals(MinimapSceneInfo other) |
| | 95 | | { |
| 0 | 96 | | if (ReferenceEquals(null, other)) return false; |
| 0 | 97 | | if (ReferenceEquals(this, other)) return true; |
| | 98 | |
|
| | 99 | | // skip `previewImageUrl` on purpose |
| 0 | 100 | | return name == other.name |
| | 101 | | && type == other.type |
| | 102 | | && parcels.SequenceEqual(other.parcels) |
| | 103 | | && isPOI == other.isPOI |
| | 104 | | && owner == other.owner |
| | 105 | | && description == other.description; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public override bool Equals(object obj) |
| | 109 | | { |
| 0 | 110 | | if (ReferenceEquals(null, obj)) return false; |
| 0 | 111 | | if (ReferenceEquals(this, obj)) return true; |
| 0 | 112 | | if (obj.GetType() != this.GetType()) return false; |
| 0 | 113 | | return Equals((MinimapSceneInfo)obj); |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public override int GetHashCode() => |
| 1 | 117 | | cachedHash ??= HashCode.Combine(name, (int)type, GetParcelsHashCode(), isPOI, owner, description); |
| | 118 | |
|
| | 119 | | private int GetParcelsHashCode() |
| | 120 | | { |
| 1 | 121 | | if (parcels == null || parcels.Count == 0) |
| 0 | 122 | | return 0; |
| | 123 | |
|
| 1 | 124 | | var hashCode = parcels[0].GetHashCode(); |
| | 125 | |
|
| 6 | 126 | | for (var i = 1; i < parcels.Count; i++) |
| 2 | 127 | | hashCode = HashCode.Combine(hashCode, parcels[i].GetHashCode()); |
| | 128 | |
|
| 1 | 129 | | return hashCode; |
| | 130 | | } |
| | 131 | | } |
| | 132 | | } |