간단한 인벤토리 만들기

sejun-Lee·2025년 5월 13일

UnityEngine

목록 보기
11/12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEditor.Progress;

public class Inventory : MonoBehaviour
{
    // 인벤토리 클래스
    // 인벤토리 클래스는 플레이어의 아이템 목록을 관리합니다.
    // 아이템 추가, 제거, 사용 등의 기능을 제공합니다.

    #region Singleton	// 하나의 인벤토리만 존재하기 위해 싱글톤 사용
    private static Inventory instance;
    public static Inventory Instance
    {
        get
        {
            if (instance == null)
            {
                Inventory prefab = Resources.Load<Inventory>("Inventory"); // 인벤토리 로드
                Instantiate(prefab); // 인스턴스 생성
            }
            return instance;
        }
    }

    private void Awake()
    {
        if (instance == null)
        {
            instance = this; // 인스턴스 설정
            //DontDestroyOnLoad(gameObject); // 씬 전환 시 파괴되지 않도록 설정
        }
        else
        {
            Destroy(gameObject); // 중복 인스턴스 제거
        }
    }
    #endregion

    #region Item
    public List<Item> items = new List<Item>(); // 아이템 목록

    // 아이템 추가
    public void AddItem(Item item)
    {
        // 아이템 추가 로직
        items.Add(item); // 아이템 추가
        item.gameObject.SetActive(false); // 아이템 비활성화
        item.transform.parent = transform; // 아이템 부모를 인벤토리로 설정
    }

    //public void AddPotion(GameObject potionPrefab)
    //{
    //    GameObject newPotion = Instantiate(potionPrefab);
    //    // 인벤토리 UI에 연결 등 추가 작업
    //}

    // 아이템 사용
    public void UseItem(Item item)
    {
        // 아이템 사용 로직
        item.Use(); // 아이템 사용
        RemoveItem(item); // 아이템 제거
    }
    public void UseItem(int index)
    {
        // 아이템 사용 로직
        Item item = items[index]; // 사용하려는 아이템
        items.RemoveAt(index); // 아이템 목록에서 제거
        item.Use(); // 아이템 사용
        Destroy(item.gameObject); // 아이템 오브젝트 파괴

    }

    // 아이템 드롭 로직
    public void DropItem(Item item)
    {        
        items.Remove(item); // 아이템 목록에서 제거
        item.gameObject.SetActive(true); // 아이템 활성화
        item.transform.parent = null; // 아이템 부모를 null로 설정
    }

    public void DropItem(int index)
    {
        Item item = items[index]; // 드롭할 아이템
        items.RemoveAt(index); // 아이템 목록에서 제거
        item.gameObject.SetActive(true); // 아이템 활성화
        item.transform.parent = null; // 아이템 부모를 null로 설정
    }


    // 아이템 제거
    public void RemoveItem(Item item)
    {
        // 아이템 제거 로직
        items.Remove(item); // 아이템 제거
    }


    // 아이템 목록 반환
    //public List<Item> GetItems()
    //{
    //    return items; // 아이템 목록 반환
    //}
    #endregion


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

public abstract class Item : MonoBehaviour
{
    public string itemName; // 아이템 이름
    public string itemDescription; // 아이템 설명

    public abstract void Use();

    //private void OnCollisionEnter(Collision collision)    
    //{
    //    if (collision.gameObject.CompareTag("Player"))
    //    {
    //        Use();
    //        Destroy(gameObject); // 아이템 사용 후 파괴
    //    }
    //}

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

public class Potion : Item
{
    Player player; // 플레이어 객체
    private float rotSpeed = 100f;        // 돌아가는 속도

    public Potion()
    {
        itemName = "힐링 포션"; // 아이템 이름 설정ㄴㄴㄴ
        itemDescription = "소량의 체력을 회복하는 아이템"; // 아이템 설명 설정
    }

    public override void Use()
    {
        Debug.Log("포션 사용 체력 30 회복"); // 포션 사용 로그 출력
        player.Heal(30); // 플레이어 체력 회복
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.CompareTag("Player"))
        {
            player = collision.gameObject.GetComponent<Player>(); // 플레이어 컴포넌트 가져오기
            Inventory inventory = collision.gameObject.GetComponentInChildren<Inventory>();
            inventory.AddItem(this); 
            Use(); // 포션 사용
            //Destroy(gameObject); // 포션 오브젝트 파괴
        }
    }

    void Update()
    {
        Rotate(); // 포션 회전
    }

    private void Rotate()
    {
        transform.Rotate(Vector3.up, rotSpeed * Time.deltaTime);    // Vector3.up 을 기준으로 돈다. -> Up 위를 기준!으로 도는 것 
    }
}
profile
초보 개발자

0개의 댓글