Scriptum/Assets/OpenPanelOnCollision.cs

173 lines
5.3 KiB
C#

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