init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System.Collections;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using VOID.Generic.Objects.Abstract.Events;
|
||||
using VOID.Generic.Objects.Unit;
|
||||
using VOID.Generic.Objects.Unit.Actions.Exceptions;
|
||||
using VOID.Generic.Objects.Unit.Events;
|
||||
|
||||
namespace VOID.UserInterface.Game.AlertUI
|
||||
{
|
||||
public class AlertUI : MonoBehaviour
|
||||
{
|
||||
public static AlertUI current { get; private set; }
|
||||
|
||||
[SerializeField, MinValue(0)] private float timeToHideAlert = 3f;
|
||||
[SerializeField] private string DEATH_MESSAGE = "Died!";
|
||||
[SerializeField] private string TURN_START = "Turn start";
|
||||
[SerializeField] private string ACTION_ERROR_MESSAGE = "Action prevented: {0}";
|
||||
|
||||
private Label _label;
|
||||
private VisualElement _root;
|
||||
private Coroutine _hideAlertCoroutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
current = this;
|
||||
|
||||
var uiDocument = gameObject.GetComponent<UIDocument>();
|
||||
_root = uiDocument.rootVisualElement;
|
||||
_label = _root.Q<Label>("alert-label");
|
||||
HideAlert();
|
||||
}
|
||||
|
||||
private void ShowAlert(string message)
|
||||
{
|
||||
if (_hideAlertCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_hideAlertCoroutine);
|
||||
}
|
||||
|
||||
_label.text = message;
|
||||
_label.style.display = DisplayStyle.Flex;
|
||||
_hideAlertCoroutine = StartCoroutine(HideAlertAfterDelay(timeToHideAlert));
|
||||
}
|
||||
|
||||
private IEnumerator HideAlertAfterDelay(float delay)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
HideAlert();
|
||||
}
|
||||
|
||||
private void HideAlert()
|
||||
{
|
||||
_label.text = "";
|
||||
_label.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void ListenFor(UnitObject unit)
|
||||
{
|
||||
unit.basicEvents.onDeathAfter.AddListener(OnDeathAfter);
|
||||
unit.basicEvents.onTurnSelfStart.AddListener(OnTurnSelfStart);
|
||||
unit.unitEvents.onActionError.AddListener(OnActionError);
|
||||
}
|
||||
|
||||
public void StopListenFor(UnitObject unit)
|
||||
{
|
||||
unit.basicEvents.onDeathAfter.RemoveListener(OnDeathAfter);
|
||||
unit.basicEvents.onTurnSelfStart.RemoveListener(OnTurnSelfStart);
|
||||
unit.unitEvents.onActionError.RemoveListener(OnActionError);
|
||||
}
|
||||
|
||||
/* EVENTS BELOW */
|
||||
/* for register and unregister */
|
||||
|
||||
private void OnDeathAfter(DeathEvent e) => ShowAlert(DEATH_MESSAGE);
|
||||
private void OnTurnSelfStart(TurnEvent e) => ShowAlert(TURN_START);
|
||||
private void OnActionError(ActionErrorEvent e) => ShowAlert(string.Format(ACTION_ERROR_MESSAGE, UnitActionErrorMessage.Get(e.cause)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1431d504a115b844cbbfb202c996a38e
|
||||
@@ -0,0 +1,10 @@
|
||||
#alert-ui {
|
||||
position: absolute;
|
||||
top: 20%;
|
||||
}
|
||||
|
||||
#alert-label {
|
||||
color: red;
|
||||
font-size: 36px;
|
||||
font-weight: bold;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8910e204f06c65342b7cec98902b019c
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
|
||||
disableValidation: 0
|
||||
@@ -0,0 +1,6 @@
|
||||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
|
||||
<Style src="project://database/Assets/92%20-%20UserInterface/Generic/Game/AlertUI/AlertUI.uss?fileID=7433441132597879392&guid=8910e204f06c65342b7cec98902b019c&type=3#AlertUI" />
|
||||
<ui:VisualElement name="alert-ui" style="flex-direction: column; justify-content: center; align-self: center; align-items: center;">
|
||||
<ui:Label name="alert-label" text="Alert!" style="color: red; -unity-text-align: upper-left;" />
|
||||
</ui:VisualElement>
|
||||
</ui:UXML>
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc08ee0446ff0eb4594fe4fd762b958a
|
||||
ScriptedImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
|
||||
Reference in New Issue
Block a user