76. Unity 최종 프로젝트 6주차(3)

이규성·2024년 2월 19일
0

TIL

목록 보기
83/106

02/15

📌팀 프로젝트 진행

목표

Android build 최적화
Ios 빌드 알아보기

금일 구현 설계

금일 구현한 사항

방어구 저장

public class ArmorSystem : MonoBehaviour
{
    public int _defense;

    public QuickSlot[] _linkedSlots;

    public event Action<QuickSlot> EquipArmor;
    public event Action<QuickSlot> UnEquipArmor;

    private void Awake()
    {
        _linkedSlots = new QuickSlot[2];

        Managers.Game.Player.ToolSystem.OnEquip += DefenseOfTheEquippedArmor;
        Managers.Game.Player.ToolSystem.OnUnEquip += DefenseOfTheUnEquippedArmor;
        Managers.Game.Player.OnHit += Duration;

        Load();

        Managers.Game.OnSaveCallback += Save;
    }

    private void Start()
    {
        Managers.Game.Player.Inventory.OnUpdated += OnInventoryUpdated;
    }

    public void DefenseOfTheEquippedArmor(QuickSlot quickSlot)
    {
        EquipItemData toolItemData = ArmorDefense(quickSlot);
        _defense += toolItemData.defense;
        Managers.Game.Player.playerDefense = _defense;

        if ((int)toolItemData.part == 0 || (int)toolItemData.part == 1)
        {
            _linkedSlots[(int)toolItemData.part] = quickSlot;
            EquipArmor?.Invoke(quickSlot);
        }
    }

    public void DefenseOfTheUnEquippedArmor(QuickSlot quickSlot)
    {
        EquipItemData toolItemData = ArmorDefense(quickSlot);
        _defense -= toolItemData.defense;
        Managers.Game.Player.playerDefense = _defense;

        if ((int)toolItemData.part == 0 || (int)toolItemData.part == 1)
        {
            _linkedSlots[(int)toolItemData.part] = null;
            UnEquipArmor?.Invoke(quickSlot);
        }
    }

    public void Duration() // Player Hit에서 호출
    {
        for (int i = 0; i < _linkedSlots.Length; i++)
        {
            if (_linkedSlots[i] != null && _linkedSlots[i].targetIndex != -1)
            {
                Managers.Game.Player.Inventory.UseToolItemByIndex(_linkedSlots[i].targetIndex, 1);
            }
        }
    }

    public void OnInventoryUpdated(int inventoryIndex, ItemSlot itemSlot)
    {
        for (int i = 0; i < 2; i++)
        {
            if (_linkedSlots[i] != null)
            {
                if (_linkedSlots[i].targetIndex == inventoryIndex)
                {
                    if (itemSlot.itemData == null)
                    {
                        EquipItemData toolItemData = ArmorDefense(_linkedSlots[i]);
                        _defense -= toolItemData.defense;
                        Managers.Game.Player.playerDefense = _defense;

                        UnEquipArmor?.Invoke(_linkedSlots[i]);
                        _linkedSlots[i].Clear();
                        Managers.Game.Player.ToolSystem.UnEquipArmor(_linkedSlots[i]);
                    }
                }
            }            
        }
    }

    private EquipItemData ArmorDefense(QuickSlot quickSlot)
    {
        ItemData armor = quickSlot.itemSlot.itemData;
        EquipItemData toolItemDate = (EquipItemData)armor;
        return toolItemDate;
    }

    private void UpdatePlayerDefense()
    {
        Managers.Game.Player.playerDefense = _defense;
    }

    private void Load()
    {
        if (SaveGame.TryLoadJsonToObject(this, SaveGame.SaveType.Runtime, "ArmorSystem"))
        {
            for (int i = 0; i < _linkedSlots.Length; ++i)
            {
                _linkedSlots[i].itemSlot.LoadData();
            }
            UpdatePlayerDefense();
        }
    }

    private void Save()
    {
        var json = JsonUtility.ToJson(this);
        SaveGame.CreateJsonFile("ArmorSystem", json, SaveGame.SaveType.Runtime);
    }
}
public class UIArmorSlot : UIItemSlot
{
    enum Images
    {
        Icon,
    }

    public ItemParts part;    

    public override void Initialize()
    {
        Bind<Image>(typeof(Images));
        Get<Image>((int)Images.Icon).gameObject.SetActive(false);
        Get<Image>((int)Images.Icon).raycastTarget = false;
    }

    private void Awake()
    {
        Initialize();        

        Managers.Game.Player.ArmorSystem.EquipArmor += EquipArmor;
        Managers.Game.Player.ArmorSystem.UnEquipArmor += UnEquipArmor;
    }

    public void EquipArmor(QuickSlot quickSlot)
    {
        if (quickSlot == null) return;
        int parts = GetPart(quickSlot);

        if (part == (ItemParts)parts)
        {
            Set(quickSlot.itemSlot);
        }
    }

    public void UnEquipArmor(QuickSlot quickSlot)
    {
        int parts = GetPart(quickSlot);

        if (part == (ItemParts)parts)
        {
            Clear();
        }
    }

    public override void Set(ItemSlot itemSlot)
    {
        Get<Image>((int)Images.Icon).sprite = itemSlot.itemData.iconSprite;
        Get<Image>((int)Images.Icon).gameObject.SetActive(true);
    }

    public override void Clear()
    {
        Get<Image>((int)Images.Icon).gameObject.SetActive(false);
    }

    private int GetPart(QuickSlot slot)
    {
        var itemData = slot.itemSlot.itemData as EquipItemData;
        if (itemData == null) return -1;
        return (int)itemData.part;
    }
}
public class ArmorSlot : MonoBehaviour
{
    [SerializeField] private GameObject _armorSlotPrefab;
    [SerializeField] private Transform _position;

    private UIArmorSlot _armorSlot;
    private List<GameObject> _armorSlots = new List<GameObject>();

    private void Awake()
    {
        CreatArmorSlot();

        var armorItemSlots = Managers.Game.Player.ArmorSystem._linkedSlots;

        foreach (var armor in armorItemSlots)
        {
            for (int i = 0; i < _armorSlots.Count; i++)
            {
                var armorSlot = _armorSlots[i].GetComponent<UIArmorSlot>();
                armorSlot.EquipArmor(armor);
            }            
        }
    }

    private void CreatArmorSlot()
    {
        for (int i = 0; i < 2; i++)
        {
            var armorSlotPrefab = Instantiate(_armorSlotPrefab, _position);
            _armorSlot = GetComponentInChildren<UIArmorSlot>();
            SetParts(i);
            _armorSlots.Add(armorSlotPrefab);
        }
    }

    private void SetParts(int index)
    {  
        _armorSlot.part = (ItemParts)index;
    }
}
public class ToolSystem : MonoBehaviour
{
	private void Load()
	{
    	if (SaveGame.TryLoadJsonToObject(this, SaveGame.SaveType.Runtime, "ToolSystem"))
    	{
        	for (int i = 0; i < 2; i++)
        	{
            	Equipments[i].itemSlot.LoadData();
        	}
    	}
	}

	private void Save()
	{
    	var json = JsonUtility.ToJson(this);
    	SaveGame.CreateJsonFile("ToolSystem", json, SaveGame.SaveType.Runtime);
	}
}

금일 이슈

금일 커밋한 사항

[➕Update] Armor system save system
방어구 장착 정보 저장 기능을 1차 구현하였습니다.

🤸🏻‍♀️Feedback

json에 대한 이해도가 부족하여 구현하는 데에 쓸데없이 많은 시간이 소요되었다.

0개의 댓글