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

99 lines
2.8 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
class NpcShopManager : MonoBehaviour
{
[SerializeField]
public Dialogue DialogueTemplate;
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
private Dialogue Dialogue;
[SerializeField]
public Shop shop;
public void Start()
{
// TODO - remove - we want ot register ir manually because condition below is not meeted in test env
this.RegisterShop();
// 1. Create dialogue object from template
// 2. Set speaker name
Dialogue = Instantiate(DialogueTemplate);
Dialogue.SetSpeakerName(gameObject.GetComponent<NPC>().name);
// 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();
}
public void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player" && gameObject.GetComponent<NPC>().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus())
{
// 1. Open dialogue panel
// 2. Open shop
OpenShop();
}
}
public void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
CloseShop();
}
}
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()
)
);
}
public void OpenShop()
{
Debug.Log("OpenShop");
// Open shopa action - invoked as dialogue end action
ShopUIManager.Instance.CurrentShopOwnerName = gameObject.GetComponent<NPC>().name;
ShopUIManager.Instance.OpenPanel();
}
public void CloseShop()
{
ShopUIManager.Instance.ClosePanel();
ShopUIManager.Instance.CurrentShopOwnerName = "";
}
}