목표
Text RPG 팀 과제 시작
학습 내용
내가 담당하게 된 부분은 아이템, 상점, 인벤토리, 저장 기능(+게임 매니저 관리) 이다.
이전에 개인 과제에서 대부분 구현했던 부분이라, 고치고 싶었던 구조와 포션을 추가하며 아이템, 상점, 인벤토리 기능은 완성했다.
더보기
아이템 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text_RPG_11
{
public abstract class Items
{
public string Name { get; } // 아이템 이름
public string Desc { get; } // 아이템 설명
public int Price { get; set; } // 아이템 가격
public bool IsEquipped { get; set; } //아이템 장착 유무
public bool IsPurchased { get; set; } // 아이템 구매 유무
public Items(string name, string desc, int price, bool isEquipped = false, bool isPurchased = false)
{
Name = name;
Desc = desc;
Price = price;
IsEquipped = isEquipped;
IsPurchased = isPurchased;
}
public abstract string ItemType();
public abstract string ItemStats();
}
public static class ItemDatabase
{
public static Items[] CreateItemsData()
{
return new Items[]
{
//무기
new Weapon("낡은 검", "쉽게 볼 수 있는 낡은 검 입니다.", 2, 600),
//방어구
new Armor("수련자 갑옷", "수련에 도움을 주는 갑옷입니다.", 5, 1000),
//포션
new Potion("체력 물약", "체력을 30 회복시켜줍니다.", 30, 500),
};
}
}
public class Weapon : Items
{
public int AttackPower { get; }
public Weapon(string name, string desc, int attackPower, int price, bool isEquipped = false, bool isPurchased = false) : base(name, desc, price, isEquipped, isPurchased)
{
AttackPower = attackPower;
}
public override string ItemType()
{
return "무기";
}
public override string ItemStats()
{
return $"공격력 +{AttackPower}";
}
}
public class Armor : Items
{
public int DefensePower { get; }
public Armor(string name, string desc, int defensePower, int price, bool isEquipped = false, bool isPurchased = false) : base(name, desc, price, isEquipped, isPurchased)
{
DefensePower = defensePower;
}
public override string ItemType()
{
return "방어구";
}
public override string ItemStats()
{
return $"방어력 +{DefensePower}";
}
}
public class Potion : Items
{
public int HealPower { get; }
public int PotionCount { get; set; }
public Potion(string name, string desc, int healPower, int price, int potionCount = 0, bool isEquipped = false, bool isPurchased = false) : base(name, desc, price, isEquipped, isPurchased)
{
HealPower = healPower;
PotionCount = potionCount;
}
public override string ItemType()
{
return "물약";
}
public override string ItemStats()
{
return $"체력 회복 +{HealPower}";
}
}
}
더보기
상점 클래스
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text_RPG_11
{
internal class Shop
{
private GameManager gameManager;
public Shop(GameManager manager)
{
gameManager = manager;
}
public void ShowShopDisplay()
{
Console.Clear();
Messages.TextTitleHlight("상점");
Console.WriteLine("필요한 아이템을 얻을 수 있는 상점입니다.\n\n[보유 골드]");
Messages.TextMagentaHlight($"{gameManager.Player.Gold}");
Console.WriteLine(" G\n\n[아이템 목록]");
for (int i = 0; i < gameManager.GameItems.Length; i++)
{
Items items = gameManager.GameItems[i];
Console.Write("- ");
Messages.TextMagentaHlight($"{i + 1} ");
Messages.Equipped(items.IsEquipped);
Console.WriteLine($" {items.Name}\t | {items.ItemStats()}\t | {items.Desc}");
}
Console.WriteLine();
Messages.TextMagentaHlight("1");
Console.WriteLine(". 아이템 구매");
Messages.TextMagentaHlight("2");
Console.WriteLine(". 아이템 판매");
Messages.TextMagentaHlight("0");
Console.Write(". 나가기\n\n원하시는 행동을 입력해주세요.\n>> ");
int intNumber = Messages.ReadInput(0, 2);
switch (intNumber)
{
case 0:
gameManager.GameMain();
break;
case 1:
ItemBuy();
break;
case 2:
ItemSell();
break;
}
}
public void ItemBuy()
{
Console.Clear();
Messages.TextTitleHlight("상점 - 아이템 구매");
Console.WriteLine("필요한 아이템을 얻을 수 있는 상점입니다.\n\n[보유 골드]");
Messages.TextMagentaHlight($"{gameManager.Player.Gold}");
Console.WriteLine(" G\n\n[아이템 목록]");
for (int i = 0; i < gameManager.GameItems.Length; i++)
{
Items items = gameManager.GameItems[i];
Console.Write("- ");
Messages.TextMagentaHlight($"{i + 1} ");
Messages.Equipped(items.IsEquipped);
if (items is Potion potion)
{
Console.WriteLine($" {items.Name} {(potion.PotionCount > 0 ? "x" + potion.PotionCount : "")}\t | {items.ItemStats()}\t | {items.Desc} | {items.Price}");
}
else
{
Console.WriteLine($" {items.Name}\t | {items.ItemStats()}\t | {items.Desc} | {(items.IsPurchased ? "구매 완료" : items.Price)}");
}
}
Console.WriteLine();
Messages.TextMagentaHlight("0");
Console.Write(". 나가기\n\n원하시는 행동을 입력해주세요.\n>> ");
int intNumber = Messages.ReadInput(0, gameManager.GameItems.Length);
switch (intNumber)
{
case 0:
ShowShopDisplay();
break;
default:
int itemIndex = intNumber - 1;
Items seletItem = gameManager.GameItems[itemIndex];
if(gameManager.Player.Gold >= seletItem.Price)
{
if(seletItem is Potion potion)
{
potion.PotionCount++;
potion.IsPurchased = true;
gameManager.Player.Gold -= seletItem.Price;
}
else if (!seletItem.IsPurchased)
{
seletItem.IsPurchased = true;
gameManager.Player.Gold -= seletItem.Price;
}
}
else if(gameManager.Player.Gold < seletItem.Price)
{
Console.WriteLine("소지금이 부족합니다.");
}
ItemBuy();
break;
}
}
public void ItemSell()
{
Console.Clear();
Messages.TextTitleHlight("상점 - 아이템 판매");
Console.WriteLine("필요한 아이템을 얻을 수 있는 상점입니다.\n\n[보유 골드]");
Messages.TextMagentaHlight($"{gameManager.Player.Gold}");
Console.WriteLine(" G\n\n[아이템 목록]");
for (int i = 0; i < gameManager.GameItems.Length; i++)
{
Items items = gameManager.GameItems[i];
if (items.IsPurchased == true)
{
Console.Write("- ");
Messages.TextMagentaHlight($"{i + 1} ");
Messages.Equipped(items.IsEquipped);
if (items is Potion potion)
{
Console.WriteLine($" {items.Name} {(potion.PotionCount > 0 ? "x" + potion.PotionCount : "")}\t | {items.ItemStats()}\t | {items.Desc} | {items.Price}");
}
else
{
Console.WriteLine($" {items.Name}\t | {items.ItemStats()}\t | {items.Desc} | {items.Price}");
}
}
}
Console.WriteLine();
Messages.TextMagentaHlight("0");
Console.Write(". 나가기\n\n원하시는 행동을 입력해주세요.\n>> ");
int intNumber = Messages.ReadInput(0, gameManager.GameItems.Length);
switch (intNumber)
{
case 0:
ShowShopDisplay();
break;
default:
int itemIndex = intNumber - 1;
Items seletItem = gameManager.GameItems[itemIndex];
if(seletItem is Potion potion)
{
if(potion.PotionCount > 0)
{
potion.PotionCount--;
gameManager.Player.Gold += seletItem.Price;
}
if(potion.PotionCount == 0)
{
potion.IsPurchased = false;
}
}
else if(seletItem.IsPurchased)
{
seletItem.IsPurchased = false;
gameManager.Player.Gold += seletItem.Price;
seletItem.IsEquipped = false;
}
ItemSell();
break;
}
}
}
}
더보기
인벤토리 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text_RPG_11
{
internal class Inventory
{
private GameManager gameManager;
public Inventory(GameManager manager)
{
gameManager = manager;
}
public void ShowInventoryDisplay()
{
Console.Clear();
Messages.TextTitleHlight("인벤토리");
Console.WriteLine("보유 중인 아이템을 관리할 수 있습니다.\n\n[아이템 목록]");
for (int i = 0; i < gameManager.GameItems.Length; i++)
{
Items items = gameManager.GameItems[i];
if (items.IsPurchased == true)
{
Console.Write("- ");
Messages.TextMagentaHlight($"{i + 1} ");
Messages.Equipped(items.IsEquipped);
if(items is Potion potion && potion.PotionCount > 0)
{
Console.WriteLine($"{items.Name} x{potion.PotionCount}\t | {items.ItemStats()}\t | {items.Desc}");
}
else
{
Console.WriteLine($" {items.Name}\t | {items.ItemStats()}\t | {items.Desc}");
}
}
}
Console.WriteLine();
Messages.TextMagentaHlight("1");
Console.WriteLine(". 장착 관리");
Messages.TextMagentaHlight("2");
Console.WriteLine(". 아이템 정렬");
Messages.TextMagentaHlight("0");
Console.WriteLine(". 나가기\n\n원하시는 행동을 입력해주세요.\n>> ");
int intNumber = Messages.ReadInput(0, 2);
switch (intNumber)
{
case 0:
gameManager.GameMain();
break;
case 1:
ItemEquipped(); //장착관리
break;
case 2:
//아이템 정렬
break;
}
}
public void ItemEquipped()
{
Console.Clear();
Messages.TextTitleHlight("인벤토리 - 장착 관리");
Console.WriteLine("보유 중인 아이템을 관리할 수 있습니다.\n\n[아이템 목록]");
for (int i = 0; i < gameManager.GameItems.Length; i++)
{
Items items = gameManager.GameItems[i];
if (items.IsPurchased == true && items.ItemType() != "물약")
{
Console.Write("- ");
Messages.TextMagentaHlight($"{i + 1} ");
Messages.Equipped(items.IsEquipped);
Console.WriteLine($" {items.Name}\t | {items.ItemStats()}\t | {items.Desc}");
}
}
Console.WriteLine();
Messages.TextMagentaHlight("0");
Console.Write(". 나가기\n\n원하시는 행동을 입력해주세요.\n>> ");
int intNumber = Messages.ReadInput(0, gameManager.GameItems.Length);
switch (intNumber)
{
case 0:
ShowInventoryDisplay();
return;
default:
int itemIndex = intNumber - 1;
Items selectItem = gameManager.GameItems[itemIndex];
if (!selectItem.IsEquipped)
{
foreach(var items in gameManager.GameItems)
{
if(items.ItemType() == selectItem.ItemType())
{
items.IsEquipped = false;
}
}
selectItem.IsEquipped = true;
}
else
{
selectItem.IsEquipped = false;
}
ItemEquipped();
break;
}
}
}
}
회고
기본적인 기능은 구현이 완료됐기에, 추가적으로 상점에서 구매 가능한 아이템 외에도 던전에서 획득 가능한 아이템을 추가하고, 장비 강화나 합성 기능을 구현해볼까 고민중이다. 구글 스프레드 시트와 연동하여 DB를 관리하기 쉽게 연동도 해보고 싶다.
'내일배움캠프 Unity > 본 캠프' 카테고리의 다른 글
| [내일배움캠프][TIL] 13일차, 팀 과제 3일차 (2025-10-16) (0) | 2025.10.16 |
|---|---|
| [내일배움캠프][TIL] 12일차, 팀 과제 2일차 (2025-10-15) (0) | 2025.10.15 |
| [내일배움캠프][TIL] 10일차, 문법 공부 (2025-10-13) (0) | 2025.10.13 |
| 2주차 개인 과제 마무리 (2025-10-12) (0) | 2025.10.12 |
| C# 문법 정리 (2025-10-06~12, 추석 연휴) (0) | 2025.10.06 |