코딩의 신 13

김동관·2025년 10월 16일

오늘 한 내용 간단 정리(아이템 강화기능, 몬스터 처치시 아이템 드롭)

ItemEnhance.cs

private static readonly int[] EnhanceCosts = { 500, 1000, 3000 };
private static readonly int[] EnhanceChance = { 70, 60, 50 };
private static readonly int[] EnhanceBonus = { 5, 10, 15 };
//간단하게 아이템 강화시 골드,확률,증가량을 추가.

Console.WriteLine("=== 강화 가능한 아이템 ===");
for (int i = 0; i < enhancableItems.Count; i++)
    {
        Console.WriteLine($"{i + 1}. {enhancableItems[i].GetInfo()}");
            }
            Console.WriteLine("0. 취소");
            Console.Write("강화할 아이템 번호를 입력하세요: ");
//인벤토리 창 1번에 강화기능을 추가해서 다른 아이템은 i+1로 번호를 증가시킴


if (chance <= EnhanceChance[item.EnhanceLevel])
      {
         item.EnhanceLevel++;
         if (item.Attack > 0) item.Attack += EnhanceBonus[item.EnhanceLevel - 1];
         if (item.Defense > 0) item.Defense += EnhanceBonus[item.EnhanceLevel - 1];

         item.Count -= 1; // 같은 아이템 1개 소모
         Console.WriteLine($"{item.Name} 강화 성공! [+{item.EnhanceLevel}]");
            }
 //장비 아이템 강화 성공시 레벨이 증가하고 같은 아이템 -1개의 기능.
 
 Item.cs
 
 puvlic string Get info()
 {
  if (EnhanceLevel > 0) info += $"+{EnhanceLevel} ";
   }
//Iem.cs에도 강화레벨이 0보다 높을시에 EnhanceLevel을 보여줌.

Inventory.cs

if (input == "1")
   {
        ItemEnhance.EnhanceItem(character);
                   }
//이렇게 1.은 강화기능으로 한다.

public void RemoveItem(Item item, int count = 1)
        {
            var existing = items.FirstOrDefault(i => i.Name == item.Name);
            if (existing != null)
            {
                existing.Count -= count;
                if (existing.Count <= 0)
                    items.Remove(existing);
            }
        }
public bool TryRemoveItem(string itemName, int count = 1)
        {
            var existing = items.FirstOrDefault(i => i.Name == itemName);
            if (existing != null && existing.Count >= count)
            {
                existing.Count -= count;
                if (existing.Count <= 0)
                    items.Remove(existing);
                return true;
            }
            return false;
        }
//게임 코드가 Item 객체를 직접 갖고 있는 경우 → RemoveItem

아이템 이름만 있는 경우, 안전하게 체크하고 싶을 때 → TryRemoveItem
아이템을 사용했을때 줄어드는 기능임
profile
아이디어 뱅크

0개의 댓글