프로젝트 설계1)

근형·2025년 1월 16일
post-thumbnail

.

오늘 팀 프로젝트는 완료 하였고 마지막 팀 다이어그램 상태이고

완성된 게임에서의 아이템 마스터 코드이다

아이템 (수정)

아이템.h
#pragma once
#include < string>

// Item 클래스: 게임 내 아이템을 나타내는 추상 기본 클래스
class Item {
public:
virtual ~Item() = default;
virtual std::string GetName() const = 0;
virtual int GetHealthBoost() const = 0;
virtual int GetAttackBoost() const = 0;
virtual int GetBuyPrice() const = 0;
virtual int GetSellPrice() const = 0;
};

아이템 마스터.h
#pragma once
#include < vector>
#include < memory>
#include < iostream>
#include < string>
#include "Item.h"
#include "SingleTon.h"

// 체력 포션 클래스: Item 클래스를 상속받아 구현
class HealPotion : public Item {
private:
std::string name;
int healthBoost;
int buyPrice;
int sellPrice;

public:
HealPotion(std::string n, int h, int bp)
: healthBoost(h) {
name = n;
buyPrice = bp;
sellPrice = static_cast< int>(buyPrice * 0.6);
// 판매 가격은 구매 가격의 60%
}

std::string GetName() const override { return name; }
int GetHealthBoost() const override { return healthBoost; }
int GetAttackBoost() const override { return 0; }
int GetBuyPrice() const override { return buyPrice; }
int GetSellPrice() const override { return sellPrice; }

};

// 공격력 상승 포션 클래스: Item 클래스를 상속받아 구현
class AttackBoostPotion : public Item {
private:
std::string name;
int attackBoost;
int buyPrice;
int sellPrice;

public:
AttackBoostPotion(std::string n, int a, int bp)
: attackBoost(a) {
name = n;
buyPrice = bp;
sellPrice = static_cast< int>(buyPrice * 0.6);
}

std::string GetName() const override { return name; }
int GetHealthBoost() const override { return 0; }
int GetAttackBoost() const override { return attackBoost; }
int GetBuyPrice() const override { return buyPrice; }
int GetSellPrice() const override { return sellPrice; }

};

// 아이템 매니저 클래스: 싱글톤 패턴을 사용하여 구현
class ItemManager : public SingleTon< ItemManager> {
private:
std::vector< std::shared_ptr< Item>> allItems;
friend class SingleTon< ItemManager>;

public:
void initializeItems();
std::vector< std::shared_ptr< Item>> getAllItems() const;
std::shared_ptr< Item> getItemByIndex(int index);
void displayAllItems() const;
std::shared_ptr< Item> createItem(const std::string& itemType,
const std::string& name,
int value,
int price);
};

아이템 마스터 ccp
#include "ItemManager.h"

// 아이템 초기화 함수
void ItemManager::initializeItems() {
if (allItems.empty()) {
allItems.push_back(std::make_shared< HealPotion>("체력 포션", 50, 10));
allItems.push_back(std::make_shared< AttackBoostPotion>("공격력 버프 포션", 10, 15));
}
}
// 모든 아이템을 반환하는 함수
std::vector<std::shared_ptr< Item>> ItemManager::getAllItems() const {
return allItems;
}
// 인덱스로 특정 아이템을 가져오는 함수
std::shared_ptr< Item> ItemManager::getItemByIndex(int index) {
if (index < 1 || index > allItems.size()) {
return nullptr;
}
return allItems[index - 1];
}

// 모든 아이템을 화면에 표시하는 함수
void ItemManager::displayAllItems() const {
std::cout << "\n===== 아이템 목록 =====\n";
int index = 1;
for (const auto& item : allItems) {
std::cout << index++ << ". " << item->GetName()
<< " (체력 +" << item->GetHealthBoost()
<< ", 공격력 +" << item->GetAttackBoost()
<< ") - 구매가: " << item->GetBuyPrice()
<< " 골드, 판매가: " << item->GetSellPrice() << " 골드\n";
}
}

std::shared_ptr< Item> ItemManager::createItem(const std::string& itemType,
const std::string& name,
int value,
int price) {
if (itemType == "HealPotion") {
return std::make_shared< HealPotion>(name, value, price);
}
else if (itemType == "AttackBoostPotion") {
return std::make_shared< AttackBoostPotion>(name, value, price);
}
return nullptr;
}
게임 "택스트 RPG"를 완성을 했다.

0개의 댓글