using Eflatun.SceneReference; using MyCollections.SceneManagement; using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; public class Building : MonoBehaviour, INotifyObservers, IDisplayable, IInteractable { [Header("Building Information: ")] [SerializeField] private string shopName; [SerializeField] private SceneDataSaver sceneDataSaver; [Header("Display message on interact: ")] [SerializeField] private DisplayMessage displayMessage; [Header("UI GameObject Prefab: ")] [SerializeField] private GameObject uiObjectPrefab; private SceneLoader sceneLoader; private GameManager gameManager; private List<IObserver> observers = new List<IObserver>(); private string uiObjectID = string.Empty; // Start is called before the first frame update void Start() { gameManager = GameManager.Instance; sceneLoader = SceneLoader.Instance; } // Generate a unique ID for this instance private string GenerateID() => Guid.NewGuid().ToString(); private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Player") { GameObject go = uiObjectPrefab; string buttonAction = InputManager.GetKey(InputAction.Interact).ToString(); uiObjectID = GenerateID(); //Notify the UI observer NotifyObservers(NotificationType.UI, ("UIObject", go), ("Message", displayMessage.GetFormattedMessage(buttonAction)), ("ObjectID", uiObjectID)); } } private void OnTriggerExit2D(Collider2D collision) { if (collision.gameObject.tag == "Player") { //Notify the UI observer to destroy the instance //I don't know how to do it NotifyObservers(NotificationType.UI, ("DestroyUI", uiObjectID)); } } //Interaction message public string GetDisplayMessage(DisplayMessage message) => message.GetFormattedMessage(InputManager.GetKey(InputAction.Interact).ToString()); //Notify the observers public void NotifyObservers(NotificationType type, params (string key, object value)[] parameters) { ObserverNotification notification = new ObserverNotification(type); foreach ((string key, object value) param in parameters) { notification.AddData(param.key, param.value); } foreach (IObserver observer in observers) { observer.OnNotify(notification); } } public void AddObserver(IObserver observer) { if (!observers.Contains(observer)) { observers.Add(observer); } } public void RemoveObserver(IObserver observer) { if (observers.Contains(observer)) { observers.Remove(observer); } } //Interaction Action public void OnInteract() { sceneLoader.LoadScenes(sceneDataSaver.groupName, LoadType.Loading); } }