다양한 기록

[Fortress Craft] 골드, 경험치 본문

유니티 엔진/Fortress Craft

[Fortress Craft] 골드, 경험치

라구넹 2025. 2. 5. 21:49
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RewardManager : MonoBehaviour
{
    public static RewardManager Instance { get; set; }
    public int Gold { get; set; }
    public float Exp { get; set; }

    private void Awake()
    {
        Instance = this;
    }
}

싱글톤 Reward 매니저에서 골드랑 경험치 프로퍼티로 가짐

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Agit.FortressCraft
{
    public class ArrowAttackCollider : AttackCollider
    {
        private Arrow arrow;

        private void Awake()
        {
            arrow = GetComponent<Arrow>();
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (TargetUnit == null) return;
            if (collision.CompareTag(TargetUnit))
            {
                if (collision.TryGetComponent<BodyCollider>(out BodyCollider bodycoliider))
                {
                    if (collision.transform.parent != null)
                    {
                        if (collision.transform.parent.TryGetComponent<NormalUnitRigidBodyMovement>(
                        out NormalUnitRigidBodyMovement normal))
                        {
                            Debug.Log("Hit! , " + normal.HP + ", " + Damage);
                            if (normal.HP - Damage * normal.Defense <= 0.0f && !normal.NoReward)
                            {
                                normal.NoReward = true;
                                RewardManager.Instance.Gold += normal.gold;
                                RewardManager.Instance.Exp += normal.exp;
                            }
                        }
                    }
                    bodycoliider.RPCSetDamage(Damage);
                    bodycoliider.CallDamageCheck();
                    arrow.Release();
                }
            }
        }
    }
}

공격할 때 유닛이나 몬스터를 죽이면 설정된 골드나 경험치를 받을 수 있음

* 몬스터는 Scriptable Object로 해당 프로퍼티 관리

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Agit.FortressCraft
{
    public class GoldUI : MonoBehaviour
    {
        private Text moneyText;

        private void Awake()
        {
            moneyText = GetComponentInChildren<Text>();
        }

        private void FixedUpdate()
        {
            moneyText.text = RewardManager.Instance.Gold.ToString();
        }
    }
}

골드 수치는 UI에 붙여서 업데이트 되도록 함