C#/텍스트 게임 "Shelter"
5일차. 상점 시스템 구현
홍삼맛
2023. 11. 10. 19:23
📌 상점 시스템 구현
- 상품 리스트를 생성하고, 해당 상품을 플레이어 인벤토리에 다시 관리할 수 있는 기능 구현
🔧 Buy
// 상품 리스트 복사
public static List<IItem> productLists = Game.ShopProducts.ToList();
// 상품 구매
static void Buy()
{
var item = productLists[currentItemIdx];
if (item == null || item.IsEmptyItem()) return;
// Bool 체크를 통해 상품 구매
if (Game.player.IsTrade(item.Price))
{
Game.player.Inventory.Add(item);
productLists.Remove(item);
}
}
- Game.cs에서 선언한 ShopProducts 리스트를 복사한다.
- IsTrade에서 bool 체크를 통해 상품을 구매할지 판정, 상품가격이 보유 재화보다 작을 경우 false, 클 경우 true
- Inventory에서 item 추가를 하고, 추가한 상품은 상품 리스트에서 제거한다.
🔧 Sell
// 인벤토리 복사
public static List<IItem> Inventory = Game.Player.Inventory;
// 상품 판매
static void Sell()
{
var item = Game.Player.Inventory[currentItemIdx];
if (item == null || item.IsEmptyItem()) return;
Game.Player.Cash += item.ToPrice();
Game.Player.Inventory.Remove(item);
}
- Game.cs에서 선언한 Inventory 리스트를 복사한다.
- 복사된 인벤토리 리스트에서 선택된 인덱스 값을 불러서, 해당 아이템 판매
- ToPrice로 판매된 가격으로 재화를 얻고, 해당 아이템은 인벤토리에서 제거