Snippets

John McAllister TargetOfTarget.cs

Created by John McAllister
using UnityEngine.UI;
using UnityEngine;
using Atavism;
using TMPro;
using UnityEngine.EventSystems;

namespace FirstGenMods
{
    public class TargetOfTarget : MonoBehaviour, IPointerClickHandler
    {
        [Header("Main Frame")]
        public bool EnableToT = true;
        [SerializeField]
        private Vector2 framePosition = Vector2.zero;
        [SerializeField]
        private string deadPropertyName = "deadstate";
        [Header("ToT Frame")]
        [SerializeField]
        private GameObject targetOfTargetFrame = null;
        [Header("Text Fields")]
        [SerializeField]
        private TextMeshProUGUI nameText = null;
        [SerializeField]
        private TextMeshProUGUI descriptionText = null;
        [SerializeField]
        private string descriptionPropertyName = "subTitle";
        [SerializeField]
        private TextMeshProUGUI speciesText = null;
        [SerializeField]
        private string speciesPropertyName = "species";
        [SerializeField]
        private TextMeshProUGUI levelText = null;
        [SerializeField]
        private string levelPropertyName = "level";
        [Header("Portrait")]
        [SerializeField]
        private Sprite defaultSprite = null;
        [SerializeField]
        private Image portraitImage = null;
        [SerializeField]
        private string[] portraitPropertyName = new string[] { "portrait", "custom:portrait" };
        [Header("Health Bar")]
        [SerializeField]
        private string currentHealthPropertyName = "health";
        [SerializeField]
        private string maxHealthPropertyName = "health-max";
        [SerializeField]
        private TextMeshProUGUI healthText = null;
        [SerializeField]
        private Image fillImage = null;

        private RectTransform mainFrameRec = null;

        private void Start()
        {
            if (targetOfTargetFrame != null && EnableToT == true)
            {
                targetOfTargetFrame.SetActive(false);
                mainFrameRec = GetComponent<RectTransform>();
                Vector2 currentWH = mainFrameRec.rect.size;
                mainFrameRec.localPosition = framePosition;
                if (fillImage != null)
                {
                    fillImage.fillAmount = 1f;
                }
            }
        }

        private void Update()
        {
            if(EnableToT == true)
            {
                if (ClientAPI.GetPlayerObject() != null)
                {
                    if ((bool)ClientAPI.GetPlayerObject().GetProperty(deadPropertyName) == false)
                    {
                        UpdateFrame();
                    }
                    else
                    {
                        ResetFrame();
                    }
                }
            }
        }

        private void UpdateFrame()
        {
            if (ClientAPI.GetTargetObject() != null && targetOfTargetFrame != null)
            {
                if (ClientAPI.GetTargetObject().MobController != null)
                {
                    long totOid = ClientAPI.GetTargetObject().MobController.Target;
                    if (totOid > 0)
                    {
                        AtavismObjectNode objectNode = ClientAPI.GetObjectNode(totOid);
                        targetOfTargetFrame.SetActive(true);
                        if(nameText != null) nameText.text = objectNode.Name;
                        if (levelText != null) levelText.text = GetLevel(objectNode).ToString();
                        if (portraitImage != null) portraitImage.sprite = GetPortraitSprite(objectNode);
                        if (speciesText != null) speciesText.text = GetSpeciesText(objectNode);
                        if (descriptionText != null) descriptionText.text = GetDescriptionText(objectNode);
                        UpdateHealthBar(objectNode);
                    }
                    else
                    {
                        ResetFrame();
                    }
                }
                else
                {
                    ResetFrame();
                }
            }
            else
            {
                ResetFrame();
            }
        }

        private void ResetFrame()
        {
            if(targetOfTargetFrame != null)
            {
                if (targetOfTargetFrame.activeInHierarchy == true)
                {
                    targetOfTargetFrame.SetActive(false);
                    fillImage.fillAmount = 1f;
                }
            }
        }

        private int GetLevel(AtavismObjectNode totNode)
        {
            int outInt = 0;
            if (totNode.PropertyExists(levelPropertyName))
            {
                outInt = (int)totNode.GetProperty(levelPropertyName);
            }
            return outInt;
        }

        private void UpdateHealthBar(AtavismObjectNode totNode)
        {
            if (fillImage != null && totNode != null)
            {
                int currentHealth = 0;
                int maxHealth = 0;

                if (totNode.PropertyExists(currentHealthPropertyName) == true && totNode.PropertyExists(maxHealthPropertyName) == true)
                {
                    currentHealth = (int)totNode.GetProperty(currentHealthPropertyName);
                    maxHealth = (int)totNode.GetProperty(maxHealthPropertyName);
                }
                else
                {
                    currentHealth = 100;
                    maxHealth = 100;
                }
                float fillPercent = (float)currentHealth / (float)maxHealth;
                fillImage.fillAmount = fillPercent;

                if (healthText != null)
                {
                    healthText.text = currentHealth + "/" + maxHealth;
                }
            }
        }

        private Sprite GetPortraitSprite(AtavismObjectNode totNode)
        {
            Sprite portraitSprite = PortraitManager.Instance.LoadPortrait(totNode.GameObject.GetComponent<AtavismNode>());
            if (portraitSprite == null)
            {
                if (totNode != null)
                {
                    for (int i = 0; i < portraitPropertyName.Length; i++)
                    {
                        if(totNode.PropertyExists(portraitPropertyName[i]) == true)
                        {
                            portraitSprite = PortraitManager.Instance.LoadPortrait((string)totNode.GetProperty(portraitPropertyName[i]));
                            break;
                        }
                    }
                }

                if (portraitSprite == null)
                {
                    portraitSprite = defaultSprite;
                }
            }
            return portraitSprite;
        }

        private string GetSpeciesText(AtavismObjectNode totNode)
        {
            string outString = "";
            if (totNode.PropertyExists(speciesPropertyName) == true)
            {
                outString = (string)totNode.GetProperty(speciesPropertyName);
            }
            return outString;
        }

        private string GetDescriptionText(AtavismObjectNode totNode)
        {
            string outString = "";
            if (totNode.PropertyExists(descriptionPropertyName) == true)
            {
                outString = (string)totNode.GetProperty(descriptionPropertyName);
            }
            return outString;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (targetOfTargetFrame.activeInHierarchy == true)
            {
                if (eventData.pointerPress.gameObject == this.gameObject)
                {
                    ChangePlayerTarget();
                }
            }
        }

        public void ChangePlayerTarget()
        {
            AtavismMobNode targetNode = ClientAPI.GetTargetObject();
            if(targetNode != null)
            {
                if (targetNode.MobController != null)
                {
                    long totOid = targetNode.MobController.Target;
                    if (totOid > 0)
                    {
                        AtavismObjectNode totNode = ClientAPI.GetObjectNode(totOid);
                        ClientAPI.SetTarget(totNode.Oid);
                    }
                }
            }
        }
    }

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.