C++

[C++] TRPG(TextRPG) Character제작

programmer-faust 2025. 6. 19. 22:04
  • 순서
    1. Character class 생성
    2. 기능 분류
    3. 변수와 함수 선언
  • Character class 생성
    • Character.h파일 작성
#pragma once

#include <vector>
#include <string>

class Item;

class Character {
private:
	static Character* charInstance;

	std::string name; //플레이어 이름
	int level = 1; //레벨
	int maxLevel = 10;
	int health = 0; //현재 체력
	int maxHealth = 0; //최대 체력
	int attack = 0; //공격력
	int experience = 0; //경험치
	int needExperience = 100;
	int gold = 0; //소지 골드

	Item* equipWeapon;
	Item* equipArmor;

	void UpgradeStatus(); //레벨업 했을 때 스탯 업그레이드
	void LevelUp(); //레벨업

public:
	Character();
	static Character* Get(std::string playerName = " "); //싱글턴 인스턴스

	//플레이어 정보
	void DisplayStatus() const; //플레이어의 현재 스탯 확인
	std::string GetName() const; //플레이어 이름

	//전투 관련
	int Attack() const;
	void TakeDamage(int damage); //피격시
	void RecoveryHP(int health); //체력 회복
	void GetExperience(int experience); //경험치 획득 -> 몬스터 처치시 몬스터의 사망로직에서 호출하여 사용
	void Die();

	//아이템 관리
	void GetItem(std::vector<Item*> item) const;
	void UseItem(int index) const; //전투에서 아이템 사용
	Item* GetEquipWeapon() const; //현재 장착 무기 반환
	Item* GetEquipArmor() const; //현재 장착 방어구 반환
	void SetEquipWeapon(Item* weapon); //무기 장착
	void SetEquipArmor(Item* armor); //방어구 장착
	void EquipStatus(int getAttack, int getHealth); //장비 장착시 스탯 변환
	void UnEquipStatus(int getAttack, int getHealth); //장비 해제시 스탯 변환

	//골드 관련
	void BorrowGold(int getGold);
	void ConsumeGold(int consumeGold);
	void VisitShop(); //Shop과 상호작용

	~Character();
};

 

 

  • Character.cpp 작성
#include <iostream>

#include "Character.h"
#include "Inventory.h"

using namespace std;

Character* Character::charInstance = nullptr;

#pragma region PlayerInformation

Character::Character() {
    name = name;
    level = 1;
    maxLevel = 10;
    maxHealth = 200;
    health = maxHealth;
    attack = 30;
    experience = 0;
    needExperience = 100;
    gold = 0;

    equipWeapon = nullptr;
    equipArmor = nullptr;
}

Character* Character::Get(string playerName)
{
    //만약 instance가 생성되지 않았다면
    if (charInstance == nullptr)
    {
        charInstance = new Character(); //캐릭터 생성해주기
    }

    charInstance->name = playerName;

    return charInstance;
}

void Character::DisplayStatus() const
{
    cout << "플레이어 캐릭터 이름: " << name << endl;
    cout << "플레이어 레벨: " << level << endl;
    cout << "플레이어 현재체력/최대체력: " << health << "/" << maxHealth << endl;
    cout << "플레이어 공격력: " << attack << endl;
    cout << "플레이어 현재 경험치/레벨업에 필요한 경험치: " << experience << "/" << needExperience << endl;
    cout << "플레이어 소지 골드: " << gold << endl;
}

string Character::GetName() const
{
    return name;
}
#pragma endregion

#pragma region Battle

int Character::Attack() const
{
    return attack;
}

void Character::TakeDamage(int damage)
{
    health -= damage;
    cout << "몬스터에게" << damage << "의 피해를 입었습니다!";

    if (health <= 0) {
        health = 0;

        Die();
    }
    else {
        cout << "플레이어 남은 체력: " << health << "/" << maxHealth << endl;
    }
}

void Character::RecoveryHP(int recoveryHp)
{
    health += recoveryHp; //체력 회복

    //만약 회복한 후 체력이 최대 체력을 넘는다면
    if (health > maxHealth) {
        health = maxHealth; //체력을 최대체력으로 조정
    }
}

void Character::Die()
{
    cout << "체력이 0이되어 플레이어 캐릭터가 사망하였습니다." << endl;
}
#pragma endregion

#pragma region Experience(Level)
void Character::GetExperience(int getExperience)
{
    experience += getExperience;

    while (experience >= needExperience)
    {
        experience -= needExperience; //레벨업에 필요한 경험치만큼 제거
        LevelUp();
    }
}

void Character::LevelUp()
{
    if (level < maxLevel) {
        level += 1;
    }
    UpgradeStatus();
}

void Character::UpgradeStatus()
{
    maxHealth += level * 20; //최대 체력 증가
    attack = attack + level * 5; //공격력 증가

    RecoveryHP(maxHealth); //체력 회복
}
#pragma endregion

#pragma region Equipment
Item* Character::GetEquipWeapon() const
{
    return equipWeapon;
}
Item* Character::GetEquipArmor() const
{
    return equipArmor;
}

void Character::SetEquipWeapon(Item* weapon)
{
    equipWeapon = weapon;
}
void Character::SetEquipArmor(Item* armor)
{
    equipArmor = armor;
}

void Character::EquipStatus(int getAttack, int getHealth)
{
    attack += getAttack;
    maxHealth += getHealth;
}

void Character::UnEquipStatus(int getAttack, int getHealth)
{
    attack -= getAttack;
    maxHealth -= getHealth;
}


#pragma endregion

#pragma region Item & Shop

void Character::GetItem(vector<Item*> getItems) const
{
    Inventory::Get()->ClassificationItem(getItems);
}
void Character::UseItem(int index) const
{
    Inventory::Get()->DisplayConsumeItem();
}

void Character::BorrowGold(int getGold)
{
    gold += getGold;
}

void Character::ConsumeGold(int consumeGold)
{
    gold -= consumeGold;
}

void Character::VisitShop()
{

}
#pragma endregion

Character::~Character()
{
    delete charInstance;
    charInstance = nullptr;
}

 

'C++' 카테고리의 다른 글

[C++] toupper, tolower  (0) 2025.07.02
[C++]TRPG(TextRPG) Inventory제작  (0) 2025.06.23
[C++]FMath  (0) 2025.06.18
[C++]객체지향적 설계  (0) 2025.06.11
[C++]STL(Standard Template Library)  (1) 2025.06.11