Encounter / Assets / Scripts / Bounty.cs
Bounty.cs
Raw
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Bounty : MonoBehaviour
{
    [Header("Enemy FF-Idle Sprites")]
    [SerializeField] private List<GameObject> enemyBounties;

    [Header("Bounty GUI Elements")]
    [SerializeField] private TextMeshProUGUI bountyOneText;
    [SerializeField] private TextMeshProUGUI bountyTwoText;
    [SerializeField] private TextMeshProUGUI bountyOneGold;
    [SerializeField] private TextMeshProUGUI bountyTwoGold;
    [SerializeField] private Button bountyOneButton;
    [SerializeField] private Button bountyTwoButton;
    [SerializeField] private Image bountyOneImage;
    [SerializeField] private Image bountyTwoImage;

    private Enemy bountyOne;
    private int goldAmountOne;

    private Enemy bountyTwo;
    private int goldAmountTwo;

    bool flag;

    public void InjectBounties()
    {
        bountyOne = enemyBounties[Random.Range(0, enemyBounties.Count)].GetComponent<Enemy>();

        // Loop prevents a duplicate enemy bounty from spawning
        do
        {
            bountyTwo = enemyBounties[Random.Range(0, enemyBounties.Count)].GetComponent<Enemy>();

        } while (bountyOne.GetName().Equals(bountyTwo.GetName()));

        ListInformation();
    }

    private void ListInformation()
    {
        bountyOneText.text = bountyOne.GetName();
        bountyTwoText.text = bountyTwo.GetName();

        bountyOneImage.sprite = bountyOne.transform.GetChild(1).GetComponent<SpriteRenderer>().sprite;
        bountyTwoImage.sprite = bountyTwo.transform.GetChild(1).GetComponent<SpriteRenderer>().sprite;

        goldAmountOne = Random.Range(50, 500) * ((int)bountyOne.GetEnemyDifficulty() + 1);
        goldAmountTwo = Random.Range(50, 500) * ((int)bountyOne.GetEnemyDifficulty() + 1);

        bountyOneGold.text = $"Reward\n {goldAmountOne} Gold";
        bountyTwoGold.text = $"Reward\n {goldAmountTwo} Gold";

        flag = false;
        PositionImage(bountyOne, bountyOneImage);

        flag = true;
        PositionImage(bountyTwo, bountyTwoImage);

        SetUpButtons();
    }

    private void PositionImage(Enemy bounty, Image bountyImage)
    {
        RectTransform imageRect = bountyImage.GetComponent<RectTransform>();

        switch (bounty.GetName())
        {
            case "Undead":
                // Move down
                imageRect.offsetMax = new Vector2(0f, 33f);
                imageRect.offsetMin = new Vector2(0f, -33f);
                break;
            case "Demonic Soldier":
                // Move up slightly
                imageRect.offsetMax = new Vector2(0f, -8.6f);
                imageRect.offsetMin = new Vector2(0f, 8.6f);
                break;
            case "Rasorian Berserker":
                // Move down
                imageRect.offsetMax = new Vector2(0f, 25f);
                imageRect.offsetMin = new Vector2(0f, -25f);
                break;
            case "Lava Golem":
                // Scale slightly and move down
                imageRect.localScale = new Vector2(flag is false ? 3f : -3f, 3f);
                imageRect.offsetMax = new Vector2(0f, 8.3f);
                imageRect.offsetMin = new Vector2(0f, -8.3f);
                break;
            case "Ice Golem":
                // Scale slightly
                imageRect.localScale = new Vector2(flag is false ? 3f : -3f, 3f);
                break;
            case "Rock Golem":
                // Scale slightly
                imageRect.localScale = new Vector2(flag is false ? 3f : -3f, 3f);
                break;
            case "Air Golem":
                // Scale slightly and move down
                imageRect.localScale = new Vector2(flag is false ? 3f : -3f, 3f);
                imageRect.offsetMax = new Vector2(0f, 16f);
                imageRect.offsetMin = new Vector2(0f, -16f);
                break;
            case "Damned Soul":
                // Since the sprite is switched around compared to others, needs to be flipped
                imageRect.localScale = new Vector2(flag is false ? -2f : 2f, 2f);
                break;
            default:
                // Nothing is needed
                break;
        }
    }

    private void SetUpButtons()
    {
        bountyOneButton.onClick.AddListener(() =>
        {
            ChooseBounty(0);
        });

        bountyTwoButton.onClick.AddListener(() =>
        {
            ChooseBounty(1);
        });
    }

    public void ChooseBounty(int side)
    {
        Enemy chosenBounty;
        int chosenGold;

        if (side is 0)
        {
            chosenBounty = bountyOne;
            chosenGold = goldAmountOne;
        }
        else
        {
            chosenBounty = bountyTwo;
            chosenGold = goldAmountTwo;
        }

        TakenBounty playerBounty = new TakenBounty(chosenBounty, chosenGold);
        Ability abRef = GameManager.Instance.GetPlayerObject().GetComponent<Player>().GetInventory().Abilities.Find(ability => ability.AbilityID is 70);

        abRef.ReferenceObject = playerBounty;

        Destroy(this.gameObject);
    }   
}

/// <summary>
/// Handles data retention of the chosen bounty for the player
/// </summary>
public class TakenBounty
{
    public Enemy Bounty { get; private set; }
    public int GoldReward { get; private set; }
    public bool HasBeenKilled { get; private set; }

    public TakenBounty(Enemy bounty, int goldAmount)
    {
        this.Bounty = bounty;
        this.GoldReward = goldAmount;

        this.HasBeenKilled = false;
    }

    public void IndicateBountyHasFallen()
        => this.HasBeenKilled = true;
}