Scriptum/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs

157 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
2022-12-20 03:30:37 +01:00
[RequireComponent(typeof(NPC))]
[RequireComponent(typeof(NpcDialogueManager))]
2022-12-20 04:37:00 +01:00
public class NpcShopManager : MonoBehaviour
{
[SerializeField]
2022-12-20 04:37:00 +01:00
public LanguageDetector<Dialogue> languageDetector;
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
2022-12-20 03:30:37 +01:00
public Dialogue Dialogue;
[SerializeField]
public Shop shop;
2022-12-20 03:30:37 +01:00
bool CanShopBeOpened = false;
2022-12-21 12:19:57 +01:00
bool IsRegistered = false;
public void Awake()
{
gameObject.GetComponent<NpcDialogueManager>().OpenInDefaultWay = false;
}
2022-12-20 03:30:37 +01:00
public virtual void Start()
{
// 1. Set npc state to trading
gameObject.GetComponent<NPC>().State = NPCStateEnum.Trading;
2022-12-20 03:30:37 +01:00
// 2. Init dialogue model
CreateInstanceBasedOnLanguage();
}
2022-12-20 03:30:37 +01:00
public void Update()
{
2022-12-21 12:19:57 +01:00
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<NPC>().name && shop.Map == SceneManager.GetActiveScene().name;
}).First();
IsRegistered = true;
}
2022-12-20 03:30:37 +01:00
/*
* 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())
{
2022-12-20 03:30:37 +01:00
// Open dialogue panel (Shop will be opened later)
Dialogue.StartDialogue();
// TODO pass dialogue to npocDialManager and invoke from there
2022-12-20 03:30:37 +01:00
}
2022-12-20 03:30:37 +01:00
}
2022-12-20 03:30:37 +01:00
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)
{
2022-12-20 03:30:37 +01:00
// don't listen when component is disabled
if (!gameObject.GetComponent<NpcShopManager>().enabled)
return;
if (collision.gameObject.tag == "Player")
{
2022-12-20 03:30:37 +01:00
if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null)
Dialogue.BreakDialogueStep();
CloseShop();
}
}
2022-12-20 03:30:37 +01:00
public void CreateInstanceBasedOnLanguage()
{
2022-12-20 04:37:00 +01:00
Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage());
2022-12-20 03:30:37 +01:00
Dialogue.SetSpeakerName(gameObject.GetComponent<NPC>().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()
)
);
2022-12-21 12:19:57 +01:00
Debug.Log($"Shop {shop.Npc} registered");
}
public void OpenShop()
{
2022-12-20 03:30:37 +01:00
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()
{
2022-12-20 03:30:37 +01:00
ShopUIManager.Instance.CurrentShopOwnerName = "";
ShopUIManager.Instance.ClosePanel();
2022-12-20 03:30:37 +01:00
}
}