언리얼

C++) TRPG #02 : 플레이어 클래스 생성

eclipse2 2026. 3. 26. 22:31

오늘은 본격적으로 기본 틀이 되는 코드를 작성하기 시작했다. 나는 캐릭터 생성을 맡아 player.h 와 player.cpp 파일의 작성을 했다.

심해 탐사라는 컨셉으로 기본 레벨, 체력, 공격력을 제외하고 산소, 압력, 배터리 등의 요소들을 player 클래스에 추가했다.

#pragma once

#include <iostream>
#include <string>


class Player
{
private:
	std::string name;
	int level;
	int hp;
	int maxHp;
	int attack;
	int tempAttack;
	int speed;
	int baseSpeed;
	int exp;
	int maxExp;
	int gold;
	int oxygen;
	int pressure;
	int battery;
	int artifactCount;

public:
	Player(std::string n);

	//상태 출력
	void showStatus() const;
	//경험치, 레벨업
	void gainExp(int amount);
	void levelUp();
	//아이템
	void addAttack(int amout);
	void addTempAttack(int amount);
	void resetTempStats();

	//전투 결과
	void takeDamage(int damage);
	void recoverDamage(int amount);
	void recoverOxygen(int amount);
	void useOxygen(int amount);
	void addArtifact();
	void spendBattery(int amount);
	void addGold(int amount);
	void recoverPressure(int amount);
	void takePressure(int amount);
	void debuffSpeed(int reduction);
	void resetSpeed();
	void useItem(std::string itemName);
	//정보 참조
	std::string getName() const { return name; }
	int getLevel() const { return level; }
	int getHp() const { return hp; }
	int getAttack() const { return attack+tempAttack; }
	int getGold() const { return gold; }
	int getArtifactCount() const { return artifactCount; }
	int getOxygen() const { return oxygen; }
	int getPressure() const { return pressure; }
	int getBattery() const { return battery; }
	int getMaxHp() const { return maxHp; }
	int getSpeed() const { return speed; }
};
#include "Player.h"

using namespace std;

Player::Player(string n)
	: name(n)
	, level(1)
	, hp(200)
	, maxHp(200)
	, attack(30)
	, exp(0)
	, maxExp(100)
	, gold(0)
	, oxygen(100)
	, speed(100)
	, baseSpeed(100)
	, pressure(0)
	, battery(100)
	, tempAttack(0)
	, artifactCount(0) {
	cout << "심해 탐사대원: " << name << " 이(가) 등록되었습니다!" << endl;
}

//상태 출력
void Player::showStatus() const {
	cout << "===============================" << endl;
	cout << "대원 정보 이름: " << name << " - level. " << level << endl;
	cout << "HP      :" << hp << " / " << maxHp << endl;
	cout << "ATK     :" << attack << endl;
	cout << "EXP     :" << exp << " / " << maxExp << endl;
	cout << "GOLD    :" << gold << " G" << endl;
	cout << "===============================" << endl;
	cout << "O2      :" << oxygen << " %" << endl;
	cout << "Battery :" << battery << " %" << endl;
	cout << "Pressure:" << pressure << " %" << endl;
	cout << "Artifact:" << artifactCount << " / 5" << endl;
}

//데미지
void Player::takeDamage(int damage) {
	hp -= damage;
	cout << name << " 대원이 " << damage << " 의 피해를 입었습니다." << endl;

	if (hp <= 0) {
		hp = 0;
		cout << name << " 대원이 쓰러졌습니다. 게임 오버" << endl;
	}
}

//채력 회복
void Player::recoverDamage(int amount) {
	int heal = amount;
	if (hp + amount > maxHp) heal = maxHp - hp;
	hp += heal;
	cout << name << " 대원의 체력이 " << heal << "만큼 회복 됐습니다. (현재 HP: " << hp << " / " << maxHp << ")" << endl;
}

//산소 회복
void Player::recoverOxygen(int amount) {
	int heal = amount;
	if (oxygen > 100) 
	{	cout << "산소가 충분합니다." << endl;
		return;
	}
	if (oxygen + amount > 100) { heal = 100 - oxygen; }
	oxygen += heal;
	cout << name << " 대원의 산소가 " << heal << "만큼 회복 됐습니다. (현재 산소량: " << oxygen << " / 100 )" << endl;
}

//산소 소모
void Player::useOxygen(int amount) {
	oxygen -= amount;
	if (oxygen < 0) oxygen = 0;

	if (oxygen > 0 && oxygen <= 10) cout << "산소가 부족합니다. 산소회복 혹은 지상으로 복귀하십쇼" << endl;
	
	cout << "산소를 " << amount << " % 소모했습니다. (남은 산소: " << oxygen << " %)" << endl;

	if (oxygen <= 0) {
		cout << "산소가 고갈됐습니다. 체력이 감소합니다." << endl;
		takeDamage(20);
	}
}
//배터리 소모(무기 사용 시)
void Player::spendBattery(int amount) {
	battery -= amount;
	if (battery < 0) battery = 0;
	cout << "배터리 " << amount << " % 소모했습니다. (현재 배터리: " << battery << " %)" << endl;
}

//압력 감소
void Player::recoverPressure(int amount) {
	pressure -= amount;
	cout << "압력이 " << amount << "% 감소했습니다. (현재 압력 " << pressure << " %)" << endl;
	
}
//압력 증가
void Player::takePressure(int amount) {
	pressure += amount;
	if (pressure > 100) pressure = 100;
	cout << "압력이 " << amount << " % 증가했습니다. (현재 압력 " << pressure << " %)" << endl;
	if (pressure >= 100) {
		cout << "압력이 너무 셉니다. 체력이 감소합니다." << endl;
		//takeDamage(20);
		debuffSpeed(50);
	}
}
//압력 증가로 속도 디버프
void Player::debuffSpeed(int reduction) {
	speed -= reduction;
	if (speed < 10) speed = 10;
	cout << "[디버프] 과도한 수압으로 몸이 무거워집니다! (현재 속도: " << speed << ")" << endl;
}
void Player::resetSpeed() {
	speed = baseSpeed;
	cout << "수압이 해소되어 몸이 가벼워졌습니다!" << endl;
}
//골드 획드
void Player::addGold(int amount) {
	gold += amount;
	cout << amount << "G를 획득했습니다. (보유 골드: " << gold << "G)" << endl;

}
//유적 발견
void Player::addArtifact() {
	artifactCount++;
	cout << "고대 유적을 발견했습니다. (현재 유적 개수: " << artifactCount << "개)" << endl;
	if (artifactCount >= 3) {
		cout << "모든 유적을 모았습니다! 심해의 비밀이 드러납니다.\n";
		cout << "(대충왕국과 심해어들의 비밀)" << endl;
	}
}
//레벨 로직
void Player::gainExp(int amount) {
	if (level >= 10) return;
	exp += amount;
	cout << amount << "의 경험치를 획득했습니다. (현재: " << exp << "/" << maxExp << ")" << endl;
	while (exp >= maxExp && level < 10) {
		levelUp();
	}
}
void Player::levelUp() {
	level++;
	int hpBonus = level * 20;
	int atkBonus = level * 5;

	maxHp += hpBonus;
	attack += atkBonus;
	hp = maxHp;
	exp -= maxExp;
	if (exp < 0) exp = 0;
	
	cout << "=============================" << endl;
	cout << "Level UP!!! 현재 레벨: " << level << endl;
	cout << "최대 체력 " << hpBonus << " 증가 / 공격력 " << atkBonus << " 증가" << endl;
	cout << "=============================" << endl;
}
//아이템 사용
void Player::useItem(string itemName) {
	cout << itemName << "을(를) 사용합니다." << endl;
	//아이템별 로직
}
//공격력 상승
void Player::addAttack(int amount) {
	attack += amount;
	cout << "공격력이 " << amount << "만큼 증가했습니다. (현재 ATK: " << attack << ")" << endl;
}

//해당 전투에만 공격력 상승
void Player::addTempAttack(int amount) {
	tempAttack += amount;
	cout << "임시 공격력이 " << amount << "만큼 증가했습니다. (현재 ATK: "
		<< getAttack() << ")" << endl;
}

//전투 종료시 tempAttack 초기화
void Player::resetTempStats() {
tempAttack = 0;
}

초기 값과 상태 출력, 전투 시 로직 아이템 및 레벨업을 구현했다.

처음으로 팀 단위에 프로젝트로 코드를 짜다 보니 깃허브 관련 문제가 있었다. 본의 아니게 깃을 관리하게 된 나는 깃허브에 branch ruleset을 추가하여 팀원들을 초대했지만 소통 문제와 복잡성으로 인해 메인 branch에 올리는 거로 되돌아갔다. 나중에 다시 ruleset은 추가 하지 않았지만 각자 branch를 따로 관리하고 내가 머지하는 형태로 정착하여 해결했다.

플레이어 클래스 구현을 생각보다 빨리 끝낸 뒤에 아이디어 구성과 정리를 팀원과 같이 했다.

마지막으로 파일 전체의 포멧이 다 달라 모두 utf-8 with signature로 바꾸는데 다른 팀원들도 수정중에 있어서 충돌이 내 branch에서 나타났고 버리고 다시 수정 하는 과정을 반복하여 애를 먹었다.....

또한, 이런 상황에서 .editorconfig 파일을 만들어서 통일할 수 있다는 걸 배웠다. 

# .editorconfig - Project coding style and file encoding
# Root .editorconfig for Restless_Ocean project
root = true

[*]
# Ensure files are saved with BOM (UTF-8 with signature) to avoid C4819 in MSVC
charset = utf-8-bom
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 4
tab_width = 4
max_line_length = 120

[*.{cpp,cxx,c,cc,h,hpp,hh}]
# C/C++ specific overrides
charset = utf-8-bom
indent_style = tab
indent_size = 4
tab_width = 4
trim_trailing_whitespace = true
end_of_line = crlf

[*.{cs,csproj,sln}]
charset = utf-8-bom
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
end_of_line = crlf

[*.{md,txt,json,xml,yml,yaml}]
charset = utf-8-bom
trim_trailing_whitespace = false
insert_final_newline = true
end_of_line = crlf

[*.{png,jpg,jpeg,gif,ico,zip,exe}]
# Binary files - don't touch whitespace rules
trim_trailing_whitespace = false
insert_final_newline = false

 

'언리얼' 카테고리의 다른 글

C++) TRPG#04 : 패치 및 수정  (0) 2026.03.30
C++) TRPG #03: 무기 및 스킬 클래스 생성  (0) 2026.03.27
C++) TRPG #01 : 기획  (0) 2026.03.25
TRPG C++  (0) 2026.03.23
C++ 알고리즘) 그리디 알고리즘  (0) 2026.03.20