Add chests animations

This commit is contained in:
kabix09 2023-01-05 20:02:44 +01:00
parent 442a3617f1
commit aa3e62b78e
4 changed files with 118 additions and 94 deletions

View File

@ -5,6 +5,7 @@ using UnityEngine.UI;
public class KeyBindScript : MonoBehaviour
{
/*
/// <summary>
/// This is the dictionary, that contains all our keybinds
@ -49,7 +50,7 @@ public class KeyBindScript : MonoBehaviour
}
/*
// Update is called once per frame
void Update()
{
@ -83,7 +84,7 @@ public class KeyBindScript : MonoBehaviour
}
}
}*/
}
void OnGUI()
{
if (currentKey != null) //If we have selected a key, that we want to edit
@ -128,5 +129,6 @@ public class KeyBindScript : MonoBehaviour
PlayerPrefs.Save();
}
}*/
}

View File

@ -21,11 +21,11 @@ public class ChestController : MonoBehaviour
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "Player")
if (collision.tag == "Player" && ChestUIManager.Instance.CurrentChestName != null)
{
ChestUIManager.Instance.CurrentChestName = null;
ChestUIManager.Instance.ClosePanel();
ChestUIManager.Instance.CurrentChestName = null;
}
}
}

View File

@ -34,66 +34,23 @@ public abstract class UIBaseManager<T> : MonoBehaviour, ManagerInterface<T>
}
else
{
Debug.Log(gameObject);
Debug.Log(Instance);
Destroy(gameObject);
}
}
//public GameObject Chest1 = GameObject.Find("Gold Chest");
public async void Update()
// async
public virtual void Update()
{
if (Input.GetKeyDown(keyToOpen))
{
if (!DynamicPanel)
{
//condition to be near to the chest
if (ChestUIManager.Instance.CurrentChestName != null && ChestUIManager.Instance.CurrentChestName != "")
{
//delay using async Yield and triggering chests to start open animation
var end = Time.time + 0.3;
animateOpening();
while (Time.time < end)
{
await System.Threading.Tasks.Task.Yield();
}
}
this.OpenPanel();
}
else
{
this.ClosePanel();
var end2 = Time.time + 0.1;
while (Time.time < end2)
{
await System.Threading.Tasks.Task.Yield();
}
animateClosing();
end2 = Time.time + 0.3;
while (Time.time < end2)
{
await System.Threading.Tasks.Task.Yield();
}
resettriggers();
}
}
if (ChestUIManager.Instance.CurrentChestName == null || ChestUIManager.Instance.CurrentChestName == "")
{
var end2 = Time.time + 0.1;
while (Time.time < end2)
{
await System.Threading.Tasks.Task.Yield();
this.OpenPanel();
}
animateClosing();
end2 = Time.time + 0.3;
while (Time.time < end2)
else
{
await System.Threading.Tasks.Task.Yield();
this.ClosePanel();
}
resettriggers();
}
}
@ -107,39 +64,6 @@ public abstract class UIBaseManager<T> : MonoBehaviour, ManagerInterface<T>
//DynamicPanel = dynamicPanelController;
}
private async void animateOpening()
{
/* GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("OpenIt");
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("OpenIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("OpenIt");*/
}
private async void animateClosing()
{
/* GameObject.Find("Gold Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
GameObject.Find("Prize Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("CloseIt");
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("CloseIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("CloseIt");*/
}
private async void resettriggers()
{
var end3 = Time.time + 0.0;
while (Time.time < end3)
{
await System.Threading.Tasks.Task.Yield();
}
/* GameObject.Find("Gold Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
GameObject.Find("Prize Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("reactivate");
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("reactivate");
GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("reactivate");*/
}
public virtual bool OpenPanel()
{
Console.WriteLine("Panel opened");

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@ -14,6 +15,11 @@ public class ChestUIManager : UIBaseManager<Chest>
public string CurrentChestName = null;
public bool OpenAfterDelay = false;
public bool CloseAfterDelay = false;
private IEnumerator coroutine;
public void Awake()
{
if (Instance == null)
@ -26,12 +32,44 @@ public class ChestUIManager : UIBaseManager<Chest>
}
}
public void Update()
public override void Update()
{
keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interact"));
base.Update();
/* if (OpenAfterDelay && DynamicPanel == null && CurrentChestName != null && CurrentChestName != "")
{
Debug.Log("Open in Update");
OpenAfterDelay = false;
// Open additiona panels
if (!EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.OpenPanel();
if (!InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.OpenPanel();
base.OpenPanel();
}
//keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interact"));
if (CloseAfterDelay && DynamicPanel != null && CurrentChestName != null && CurrentChestName != "")
{
CloseAfterDelay = false;
base.ClosePanel();
ChestContentUIManager.Instance.DynamicPanel = null;
// Close additionals panels
if (EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.ClosePanel();
if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel();
}*/
}
public override bool OpenPanel()
{
if (CurrentChestName == null || CurrentChestName == "")
@ -41,12 +79,13 @@ public class ChestUIManager : UIBaseManager<Chest>
return false;
}
// Open additiona panels
if (!EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.OpenPanel();
if (!InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.OpenPanel();
// Animacje otwierania
OpeningAnimate();
return base.OpenPanel();
StartCoroutine(OpenWithDelay(0.3f));
return true;
}
public override bool ClosePanel()
@ -60,6 +99,11 @@ public class ChestUIManager : UIBaseManager<Chest>
if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel();
// Animacje zmykania
ClosingAnimate(CurrentChestName);
StartCoroutine(CloseWithDelay(0.3f, CurrentChestName)); // delay to display closing without breaking
//CloseWithDelay(0.3f);
return true;
}
@ -112,4 +156,58 @@ public class ChestUIManager : UIBaseManager<Chest>
{
return Elements.Find(chest => chest.Name == _chestName);
}
#region Animation API
private IEnumerator OpenWithDelay(float waitTime)
{
yield return new WaitForSeconds(waitTime);
// Open additiona panels
if (!EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.OpenPanel();
if (!InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.OpenPanel();
base.OpenPanel();
}
private IEnumerator CloseWithDelay(float waitTime, string chestName)
{
yield return new WaitForSeconds(waitTime);
ResetingTriggerAnimate(chestName);
}
public void OpeningAnimate()
{
var currentChest = GameObject.FindGameObjectWithTag("ChestCollection").transform.Find(CurrentChestName);
if(currentChest != null)
{
currentChest.GetComponent<Animator>().SetTrigger("OpenIt");
}
}
public void ClosingAnimate(string chestName)
{
var currentChest = GameObject.FindGameObjectWithTag("ChestCollection").transform.Find(chestName);
if (currentChest != null)
{
currentChest.GetComponent<Animator>().ResetTrigger("OpenIt");
currentChest.GetComponent<Animator>().SetTrigger("CloseIt");
}
}
public void ResetingTriggerAnimate(string chestName)
{
var currentChest = GameObject.FindGameObjectWithTag("ChestCollection").transform.Find(chestName);
if (currentChest != null)
{
currentChest.GetComponent<Animator>().ResetTrigger("CloseIt");
currentChest.GetComponent<Animator>().SetTrigger("reactivate");
}
}
#endregion
}