Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Buildings / Building.cs
  1. using Eflatun.SceneReference;
  2. using MyCollections.SceneManagement;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. public class Building : MonoBehaviour, INotifyObservers, IDisplayable, IInteractable
  9. {
  10. [Header("Building Information: ")]
  11. [SerializeField] private string shopName;
  12. [Header("Loading References: ")]
  13. [SerializeField] private SceneReference sceneToLoad;
  14. [SerializeField] private SceneReference sceneToUnload;
  15. [SerializeField] private SceneDataSaver sceneDataSaver;
  16. [Header("Display message on interact: ")]
  17. [SerializeField] private DisplayMessage displayMessage;
  18. [Header("UI GameObject Prefab: ")]
  19. [SerializeField] private GameObject uiObjectPrefab;
  20. private SceneLoader sceneLoader;
  21. private GameManager gameManager;
  22. private List<IObserver> observers = new List<IObserver>();
  23. private string uiObjectID = string.Empty;
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. gameManager = GameManager.Instance;
  28. sceneLoader = SceneLoader.Instance;
  29. }
  30. // Generate a unique ID for this instance
  31. private string GenerateID() => Guid.NewGuid().ToString();
  32. private void OnTriggerEnter2D(Collider2D collision)
  33. {
  34. if (collision.gameObject.tag == "Player")
  35. {
  36. GameObject go = uiObjectPrefab;
  37. string buttonAction = InputManager.GetKey(InputAction.Interact).ToString();
  38. uiObjectID = GenerateID();
  39. //Notify the UI observer
  40. NotifyObservers(NotificationType.UI, ("UIObject", go), ("Message", displayMessage.GetFormattedMessage(buttonAction)), ("ObjectID", uiObjectID));
  41. }
  42. }
  43. private void OnTriggerExit2D(Collider2D collision)
  44. {
  45. if (collision.gameObject.tag == "Player")
  46. {
  47. //Notify the UI observer to destroy the instance
  48. //I don't know how to do it
  49. NotifyObservers(NotificationType.UI, ("DestroyUI", uiObjectID));
  50. }
  51. }
  52. //Interaction message
  53. public string GetDisplayMessage(DisplayMessage message) =>
  54. message.GetFormattedMessage(InputManager.GetKey(InputAction.Interact).ToString());
  55. //Notify the observers
  56. public void NotifyObservers(NotificationType type, params (string key, object value)[] parameters)
  57. {
  58. ObserverNotification notification = new ObserverNotification(type);
  59. foreach ((string key, object value) param in parameters)
  60. {
  61. notification.AddData(param.key, param.value);
  62. }
  63. foreach (IObserver observer in observers)
  64. {
  65. observer.OnNotify(notification);
  66. }
  67. }
  68. public void AddObserver(IObserver observer)
  69. {
  70. if (!observers.Contains(observer))
  71. {
  72. observers.Add(observer);
  73. }
  74. }
  75. public void RemoveObserver(IObserver observer)
  76. {
  77. if (observers.Contains(observer))
  78. {
  79. observers.Remove(observer);
  80. }
  81. }
  82. //Interaction Action
  83. public void OnInteract()
  84. {
  85. sceneLoader.LoadSceneWithLoading(sceneToLoad, sceneToUnload);
  86. }
  87. }