Scriptum/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs
2023-01-05 15:47:02 +01:00

159 lines
4.9 KiB
C#

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<Dialogue> languageDetector;
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
public Dialogue Dialogue;
[SerializeField]
public ShopBuildModel shopModel;
[NonSerialized]
public Shop shop;
bool CanShopBeOpened = false;
bool IsRegistered = false;
public void Awake()
{
gameObject.GetComponent<NpcDialogueManager>().OpenInDefaultWay = false;
}
public virtual void Start()
{
// 1. Set npc state to trading
gameObject.GetComponent<NPC>().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 shopModel.ShopName == gameObject.GetComponent<NPC>().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<NpcDialogueManager>().CanBeOpened && Input.GetKeyDown(ShopUIManager.Instance.keyToOpen) && !ShopUIManager.Instance.GetPanelStatus())
{
CreateInstanceBasedOnLanguage();
// 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<NpcShopManager>().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<NPC>().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus())
{
gameObject.GetComponent<NpcDialogueManager>().CanBeOpened = true;
}
}
public void OnTriggerExit2D(Collider2D collision)
{
// don't listen when component is disabled
if (!gameObject.GetComponent<NpcShopManager>().enabled)
return;
if (collision.gameObject.tag == "Player")
{
if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null)
Dialogue.BreakDialogueStep();
gameObject.GetComponent<NpcDialogueManager>().CanBeOpened = false;
CloseShop();
}
}
public void CreateInstanceBasedOnLanguage()
{
Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage());
Dialogue.SetSpeakerName(gameObject.GetComponent<NPC>().name);
Dialogue.SetActionAfterDialogueStep(0, Dialogue.ResetDialogue);
Dialogue.SetActionAfterDialogueStep(0, OpenShop);
}
public void RegisterShop()
{
// 1. Set owner name
shopModel.ShopName = gameObject.GetComponent<NPC>().Name;
// 2. Create new Shop instance in scene registry list
((SceneShopDataManager)SceneShopDataManager.Instance)
.RegisterShop(
shopModel.MapBuildModelToShop()
);
Debug.Log($"Shop {shopModel.ShopName} 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<NPC>().name;
ShopUIManager.Instance.OpenPanel();
}
public void CloseShop()
{
ShopUIManager.Instance.CurrentShopOwnerName = "";
ShopUIManager.Instance.ClosePanel();
}
}