카테고리 없음
TEXT RPG 만들기3 (상점 기능 만들)
danpat77
2025. 2. 5. 20:48
오늘은 Text RPG에 상점 기능을 구현 해볼 것이다. 상점은 어제 만들었던 인벤토리와 비슷해서 구현 하는데 큰 무리가 없었다.
public class Shop
{
private List<ShopItem> ShopItemList;
public Shop()
{
ShopItemList = new List<ShopItem>();
}
public void SettingShop() // 상점 아이템 기본 세팅
{
ShopItemList.Add(new ShopItem("수련자 갑옷", 7, "방어력", "수련자용 갑옷이다.", 500, false));
ShopItemList.Add(new ShopItem("무쇠갑옷", 10, "방어력", "무쇠로 만들어져 튼튼한 갑옷이다.",750, false));
ShopItemList.Add(new ShopItem("낡은 검", 7, "공격력", "수련자용 갑옷이다.", 500, false));
ShopItemList.Add(new ShopItem("청동 도끼", 10, "공격력", "수련자용 갑옷이다.", 750, false));
}
public void ViewShopList()
{
for (int i = 0; i < ShopItemList.Count; i++)
{
Console.WriteLine($"{ShopItemList[i].Name} | {ShopItemList[i].ItemType} + {ShopItemList[i].State} | {ShopItemList[i].Description} | {ShopItemList[i].Price}");
}
}
}
전에 인벤토리를 만들듯 상점용 리스트를 만들어주고 상점 기본 셋팅 메소드를 만들어준다.
이제 상점에 있는 아이템을 출력 해 줄 메소드를 준비해 준다.
public void ShopManger(Player player, Inventory inventory) // 상점 아이템 구매 기능
{
bool game = true;
while (game)
{
Console.Clear();
Console.WriteLine("상점 - 아이템 판매");
Console.WriteLine("필요한 아이템을 얻을 수 있는 상점입니다.\n");
Console.WriteLine("[보유 골드]");
Console.WriteLine($"{player.Gold} G");
Console.WriteLine("[아이템 목록]");
for (int i = 0; i < ShopItemList.Count; i++)
{
Console.Write($"- {i + 1} ");
if (ShopItemList[i].IsBuy != true) Console.WriteLine($"{ShopItemList[i].Name} | {ShopItemList[i].ItemType} + {ShopItemList[i].State} | {ShopItemList[i].Description} | {ShopItemList[i].Price}");
else Console.WriteLine($"{ShopItemList[i].Name} | {ShopItemList[i].ItemType} + {ShopItemList[i].State} | {ShopItemList[i].Description} | 구매완료");
}
Console.WriteLine("0. 나가기\n");
Console.WriteLine("원하시는 행동을 입력해주세요.");
Console.Write(">> ");
int select = int.Parse(Console.ReadLine());
if (select == 0) game = false;
else if (select > 0 && select <= ShopItemList.Count && ShopItemList[select - 1] != null)
{//선택한 숫자가 상점 리스트 안에 있을때
if (ShopItemList[select - 1].IsBuy == false)
{//선택한 아이템이 구매가 false일때
if (player.Gold >= ShopItemList[select - 1].Price)
{//플레이어 골드가 아이템 가격보다 많을때
ShopItemList[select - 1].IsBuy = true;
player.Gold -= ShopItemList[select - 1].Price;
float sellPrice = (ShopItemList[select - 1].Price * 0.85f);
Items items = new Items(ShopItemList[select - 1].Name, ShopItemList[select - 1].ItemType,
ShopItemList[select - 1].State, ShopItemList[select - 1].Description, false, Convert.ToInt32(sellPrice));
inventory.AddItems(items);
}
else if (player.Gold < ShopItemList[select - 1].Price)
{
Console.WriteLine("돈이 부족합니다. 계속하려면 아무 키나 누르세요...");
Console.ReadKey();
}
}
else
{
Console.WriteLine("이미 구매한 아이템입니다. 계속하려면 아무 키나 누르세요...");
Console.ReadKey();
}
}
else
{
Console.WriteLine("잘못된 입력입니다. 계속하려면 아무 키나 누르세요...");
Console.ReadKey();
}
}
}
이렇게 상점에서 아이템을 구매 로직을 구현을 해주면 된다.
이번에도 if문으로 도배해서 구현을 했지만 작동은 되니.......