using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; [RequireComponent(typeof(NPC))] [RequireComponent(typeof(NpcDialogueManager))] public class NpcShopManager : MonoBehaviour { [SerializeField] public LanguageDetector languageDetector; /* We user object CLONED TO PREVENT overwritting changes in main object inassets */ public Dialogue Dialogue; [SerializeField] public Shop shop; bool CanShopBeOpened = false; bool IsRegistered = false; public void Awake() { gameObject.GetComponent().OpenInDefaultWay = false; } public virtual void Start() { // 1. Set npc state to trading gameObject.GetComponent().State = NPCStateEnum.Trading; // 2. Init dialogue model CreateInstanceBasedOnLanguage(); } public void Update() { if(!IsRegistered) { // if its new game or we dont have any save from this map - use deffault shop settings / content // else overwrite value by saved one if (OnMapAppearanceMethod.GameStatus == GameStatus.NewGame || (OnMapAppearanceMethod.Gateway != OnMapAppearanceMethodEnum.NewGame || ShopUIManager.Instance.GetList().Count == 0) ) { // 0. Register shop in scene shop manager this.RegisterShop(); //ShopUIManager.Instance.Add(shop); } else shop = ShopUIManager.Instance.GetList().Where(shop => { return shop.Npc == gameObject.GetComponent().name && shop.Map == SceneManager.GetActiveScene().name; }).First(); IsRegistered = true; } /* * Conditions: * - player must be in range of shopping * - user must press specific keyboard button * - dialogue panel must be closed * - shop panel must be closed */ if (gameObject.GetComponent().CanBeOpened && Input.GetKeyDown(ShopUIManager.Instance.keyToOpen) && !ShopUIManager.Instance.GetPanelStatus()) { // Open dialogue panel (Shop will be opened later) Dialogue.StartDialogue(); // TODO pass dialogue to npocDialManager and invoke from there } } public void OnTriggerEnter2D(Collider2D collision) { // don't listen when component is disabled if (!gameObject.GetComponent().enabled) return; /* * Conditions: * - agent in collision must be player * - npc bust me in Trading mode * - shop must be currently closed */ if (collision.gameObject.tag == "Player" && gameObject.GetComponent().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus()) { gameObject.GetComponent().CanBeOpened = true; } } public void OnTriggerExit2D(Collider2D collision) { // don't listen when component is disabled if (!gameObject.GetComponent().enabled) return; if (collision.gameObject.tag == "Player") { if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null) Dialogue.BreakDialogueStep(); CloseShop(); } } public void CreateInstanceBasedOnLanguage() { Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage()); Dialogue.SetSpeakerName(gameObject.GetComponent().name); Dialogue.SetActionAfterDialogueStep(0, Dialogue.ResetDialogue); Dialogue.SetActionAfterDialogueStep(0, OpenShop); } public void RegisterShop() { // 1. Set owner name shop.Npc = gameObject.name; // 2. Create new Shop instance in scene registry list ((SceneShopDataManager)SceneShopDataManager.Instance) .RegisterShop( new Shop( SceneManager.GetActiveScene().name, gameObject.name, shop.GetContent() ) ); Debug.Log($"Shop {shop.Npc} registered"); } public void OpenShop() { if (EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.ClosePanel(); // Set info about current shop in shop UI manager (and wait for event - pressing E) // Open shopa action - invoked as dialogue end action ShopUIManager.Instance.CurrentShopOwnerName = gameObject.GetComponent().name; ShopUIManager.Instance.OpenPanel(); } public void CloseShop() { ShopUIManager.Instance.CurrentShopOwnerName = ""; ShopUIManager.Instance.ClosePanel(); } }