2022-06-12 11:28:14 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-10-02 18:45:58 +02:00
|
|
|
using System.Linq;
|
|
|
|
using UnityEngine.SceneManagement;
|
2022-06-12 11:28:14 +02:00
|
|
|
|
|
|
|
public class OpenPanelOnCollision : MonoBehaviour
|
|
|
|
{
|
|
|
|
|
|
|
|
public GameObject Panel;
|
|
|
|
public GameObject OtherPanel;
|
|
|
|
public GameObject SecondPanel;
|
|
|
|
public GameObject PanelAbove;
|
|
|
|
|
2022-06-17 22:22:19 +02:00
|
|
|
// Handle dynamic build GameObject's
|
|
|
|
public GameObject _tmpPanel;
|
|
|
|
public GameObject _tmpOtherPanel;
|
|
|
|
public GameObject _tmpSecondPanel;
|
|
|
|
public GameObject _tmpPanelAbove;
|
|
|
|
|
|
|
|
private GameObject globalGUI;
|
|
|
|
|
2022-06-12 17:18:43 +02:00
|
|
|
public GameObject obj;
|
|
|
|
|
|
|
|
private bool inRange = false;
|
|
|
|
|
2022-06-17 22:22:19 +02:00
|
|
|
public bool isAllowedToShowPanels = true; // TODO: controle its value - mprove later with cooperaition with "closePossibleButtonInfo" script
|
|
|
|
|
|
|
|
public void Awake()
|
|
|
|
{
|
|
|
|
this.globalGUI = GameObject.FindGameObjectsWithTag("GUI")[0];
|
|
|
|
}
|
|
|
|
|
2022-10-02 18:45:58 +02:00
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
if(obj == null && SceneManager.GetActiveScene().name == "SampleScene")
|
|
|
|
obj = GameObject.FindGameObjectsWithTag("NPC").Where(obj => obj.name == "BossThug").ToArray()[0];
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:22:19 +02:00
|
|
|
public void ClearPanels()
|
|
|
|
{
|
|
|
|
if(_tmpPanel) Destroy(_tmpPanel);
|
|
|
|
if(_tmpSecondPanel) Destroy(_tmpSecondPanel);
|
|
|
|
if(_tmpPanelAbove) Destroy(_tmpPanelAbove);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
this.ClearPanels();
|
|
|
|
}
|
|
|
|
|
2022-06-12 11:28:14 +02:00
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(collision.tag == "Player" && isAllowedToShowPanels)
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
inRange = true;
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpSecondPanel)
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpPanel) {_tmpPanel = Instantiate(Panel, Panel.transform.position, Quaternion.identity, globalGUI.transform); _tmpPanel.transform.localPosition = Panel.transform.position;}
|
|
|
|
//Panel.SetActive(true);
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpPanelAbove) { _tmpPanelAbove = Instantiate(PanelAbove, PanelAbove.transform.position, Quaternion.identity, globalGUI.transform); _tmpPanelAbove.transform.localPosition = PanelAbove.transform.position;}
|
|
|
|
//PanelAbove.SetActive(true);
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 17:18:43 +02:00
|
|
|
private void OnTriggerStay2D(Collider2D collision)
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if (collision.tag == "Player" && isAllowedToShowPanels)
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
|
|
|
inRange = true;
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpOtherPanel) //OtherPanel.active == true
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if (!_tmpSecondPanel) //SecondPanel.active == false
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpPanel) Destroy(_tmpPanel);
|
|
|
|
//Panel.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpSecondPanel) Destroy(_tmpSecondPanel);
|
|
|
|
if(_tmpPanelAbove) Destroy(_tmpPanelAbove);
|
|
|
|
// SecondPanel.SetActive(false);
|
|
|
|
// PanelAbove.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-17 22:22:19 +02:00
|
|
|
else if (!_tmpSecondPanel) // SecondPanel.active == false
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpPanel) { _tmpPanel = Instantiate(Panel, Panel.transform.position, Quaternion.identity, globalGUI.transform); _tmpPanel.transform.localPosition = Panel.transform.position;}
|
|
|
|
|
|
|
|
//Panel.SetActive(true);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpPanelAbove) { _tmpPanelAbove = Instantiate(PanelAbove, PanelAbove.transform.position, Quaternion.identity, globalGUI.transform); _tmpPanelAbove.transform.localPosition = PanelAbove.transform.position;}
|
|
|
|
//PanelAbove.SetActive(true);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerator Timer1()
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.3f);
|
|
|
|
if(inRange == false)
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpPanel) Destroy(_tmpPanel);
|
|
|
|
//Panel.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator Timer2()
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.3f);
|
|
|
|
if (inRange == false)
|
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpPanelAbove) Destroy(_tmpPanelAbove);
|
|
|
|
//PanelAbove.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 11:28:14 +02:00
|
|
|
void Update()
|
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
|
2022-10-02 18:45:58 +02:00
|
|
|
if(obj != null && obj.active == false)
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if (_tmpPanel) //Panel.active == true
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
Destroy(_tmpPanel);
|
|
|
|
//Panel.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
2022-06-17 22:22:19 +02:00
|
|
|
else if (_tmpPanelAbove) //PanelAbove.active == true
|
2022-06-12 17:18:43 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
Destroy(_tmpPanelAbove);
|
|
|
|
//PanelAbove.SetActive(false);
|
2022-06-12 17:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpOtherPanel) //OtherPanel.active == true
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
StartCoroutine(Timer1());
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
2022-06-17 22:22:19 +02:00
|
|
|
if(!_tmpSecondPanel) //SecondPanel.active == false
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpPanelAbove) //PanelAbove.active == true
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
StartCoroutine(Timer2());
|
2022-06-17 22:22:19 +02:00
|
|
|
Destroy(_tmpPanelAbove);
|
|
|
|
//PanelAbove.SetActive(false);
|
|
|
|
if(!_tmpPanel) { _tmpPanel = Instantiate(Panel, Panel.transform.position, Quaternion.identity, globalGUI.transform); _tmpPanel.transform.localPosition = Panel.transform.position;}
|
|
|
|
|
|
|
|
//Panel.SetActive(true);
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerExit2D(Collider2D collision)
|
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
inRange = false;
|
2022-06-17 22:22:19 +02:00
|
|
|
if(_tmpPanel) //Panel.active == true
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
StartCoroutine(Timer1());
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
2022-06-17 22:22:19 +02:00
|
|
|
else if(_tmpPanelAbove) //PanelAbove.active == true
|
2022-06-12 11:28:14 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
StartCoroutine(Timer2());
|
2022-06-12 11:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|