6-8. 15조 PlayerInventory, Brick(던질 오브젝트)

keubung·2024년 12월 5일

1. PlayerInventory

양손 시스템

  • 총 인벤토리가 2칸 (왼손, 오른손)
    public class PlayerInventory : MonoBehaviour
    {
        private int curCount = 0;
        private int maxCount = 0;
        private const int handCount = 2;
    
        [SerializeField] private Tablet Tablet;
        private List<HandheldItem> handList = new List<HandheldItem>(2);
        private List<HandheldItem> bagList = new List<HandheldItem>();
    
        public void Init(Player player)
        {
            maxCount += handCount;
            Tablet.Init(player);
        }
    
        /// <summary>
        /// 테블릿 설정 함수
        /// </summary>
        public void SetTablet(Tablet tablet)
        {
            if (tablet != null)
                return;
    
            Tablet = tablet;
        }
    
        /// <summary>
        /// 아이템 설정 함수
        /// </summary>
        public void SetHand(HandheldItem item, DesignEnums.ItemCarryType type)
        {
            if (type == DesignEnums.ItemCarryType.None)
                return;
    
            int temp = (int)type + curCount;
    
            item.gameObject.SetActive(false);
    
            if (temp > maxCount)
                OverHandItem(temp);
    
            handList.Add(item);
    
            curCount = temp;
        }
    
        private int OverHandItem(int temp)
        {
            if (handList.Count == 0)
            {
                return temp;
            }
    
            if (handList.Count == handCount)
            {
                RemoveItem();
            }
    
            RemoveItem();
    
            temp = maxCount;
    
            return temp;
        }
    
        /// <summary>
        /// 아이템 삭제 함수
        /// </summary>
        private void RemoveItem()
        {
            DropItem();
            handList.RemoveAt(0);
        }
    
        private void DropItem() 
        { 
            HandheldItem item;
    
            item = handList[0];
            item.gameObject.SetActive(true);
        }
    }

2. Brick

bool변수 isHeld 사용 (임시 테스트용)

  public class Brick : HandheldItem
{
    public float throwSpeed = 10f;
    public Transform startPosition;
    public GameObject handBrickPrefab;
    public Transform handPosition;

    public bool isHeld = false;

    private Rigidbody rigidbody;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    public override void Interact(Player player)
    {
        transform.position = startPosition.position;

        // 손에 들기
        if (!isHeld)
        {
            isHeld = true;
            SpawnHandBrick(player);
            Destroy(gameObject);
        }
        else
        {
            isHeld = false;
            // 던지기
            Vector3 throwDirection = ThrowDirection();
            Vector3 initialVelocity = throwDirection * throwSpeed;

            rigidbody.isKinematic = false;
            rigidbody.velocity = Vector3.zero;
            rigidbody.AddForce(initialVelocity, ForceMode.VelocityChange);
        }
    }

    private Vector3 ThrowDirection()
    {
        Vector3 direction = transform.forward;

        direction.y += 0.3f;    // 포물선
        return direction.normalized;
    }

    private void SpawnHandBrick(Player player)
    {
        GameObject handBrick = Instantiate(handBrickPrefab, handPosition.position, Quaternion.identity);
        handBrick.transform.parent = handPosition; // 손에 고정
    }
}
profile
김나영(Unity_6기)

0개의 댓글