2022-12-19 03:34:45 +01:00
|
|
|
|
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))]
|
2022-12-20 15:23:09 +01:00
|
|
|
|
[RequireComponent(typeof(NpcDialogueManager))]
|
2022-12-20 04:37:00 +01:00
|
|
|
|
public class NpcShopManager : MonoBehaviour
|
2022-12-19 03:34:45 +01:00
|
|
|
|
{
|
|
|
|
|
[SerializeField]
|
2022-12-20 04:37:00 +01:00
|
|
|
|
public LanguageDetector<Dialogue> languageDetector;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
|
|
|
|
|
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
|
2022-12-20 03:30:37 +01:00
|
|
|
|
public Dialogue Dialogue;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
public Shop shop;
|
|
|
|
|
|
2022-12-20 03:30:37 +01:00
|
|
|
|
bool CanShopBeOpened = false;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
|
2022-12-21 12:19:57 +01:00
|
|
|
|
bool IsRegistered = false;
|
|
|
|
|
|
2022-12-20 15:23:09 +01:00
|
|
|
|
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-19 03:34:45 +01:00
|
|
|
|
|
2022-12-20 03:30:37 +01:00
|
|
|
|
// 2. Init dialogue model
|
|
|
|
|
CreateInstanceBasedOnLanguage();
|
2022-12-19 03:34:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 03:30:37 +01:00
|
|
|
|
public void Update()
|
2022-12-19 03:34:45 +01:00
|
|
|
|
{
|
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
|
|
|
|
|
*/
|
2022-12-20 15:23:09 +01:00
|
|
|
|
if (gameObject.GetComponent<NpcDialogueManager>().CanBeOpened && Input.GetKeyDown(ShopUIManager.Instance.keyToOpen) && !ShopUIManager.Instance.GetPanelStatus())
|
2022-12-19 03:34:45 +01:00
|
|
|
|
{
|
2022-12-20 03:30:37 +01:00
|
|
|
|
// Open dialogue panel (Shop will be opened later)
|
|
|
|
|
Dialogue.StartDialogue();
|
2022-12-20 15:23:09 +01:00
|
|
|
|
|
|
|
|
|
// TODO pass dialogue to npocDialManager and invoke from there
|
2022-12-20 03:30:37 +01:00
|
|
|
|
}
|
2022-12-19 03:34:45 +01:00
|
|
|
|
|
2022-12-20 03:30:37 +01:00
|
|
|
|
}
|
2022-12-19 03:34:45 +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())
|
|
|
|
|
{
|
2022-12-20 15:23:09 +01:00
|
|
|
|
gameObject.GetComponent<NpcDialogueManager>().CanBeOpened = true;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
if (collision.gameObject.tag == "Player")
|
|
|
|
|
{
|
2022-12-20 03:30:37 +01:00
|
|
|
|
if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null)
|
|
|
|
|
Dialogue.BreakDialogueStep();
|
|
|
|
|
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
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");
|
2022-12-19 03:34:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2022-12-19 03:34:45 +01:00
|
|
|
|
|
|
|
|
|
// 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 = "";
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
ShopUIManager.Instance.ClosePanel();
|
2022-12-20 03:30:37 +01:00
|
|
|
|
}
|
2022-12-19 03:34:45 +01:00
|
|
|
|
}
|
|
|
|
|
|