Player.cpp
 1  #include "Player.h"
 2  #include <iostream>
 3  using namespace std;
 4  
 5  Player::Player() : _hp(100), _maxHp(100), _attack(10)
 6  {
 7  
 8  }
 9  
10  
11  Player::Player(int hp) : _hp(hp), _maxHp(hp), _attack(10)
12  {
13  	
14  }
15  
16  Player::~Player()
17  {
18  	
19  }
20  
21  
22  void Player::AddHp(int value)
23  {
24  	_hp += value;
25  	if (_hp < 0)
26  		_hp = 0;
27  	if (_hp > _maxHp)
28  		_hp = _maxHp;
29  }
30  
31  bool Player::IsDead()
32  {
33  	return (_hp <= 0);
34  }
35  
36  int Player::GetAttackDamage()
37  {
38  	int damage = _attack;
39  
40  	if (_maxHp > 0)
41  	{
42  		int percentage = (100 * _hp) / _maxHp;
43  		if (percentage <= 50)
44  			damage *= 2;
45  	}
46  
47  	return damage;
48  }
49  
50  void Player::PrintInfo()
51  {
52  	cout << "------ PlayerInfo ------" << endl;
53  	cout << "HP: " << _hp << endl;
54  	cout << "ATT: " << _attack << endl;
55  }
56  
57  void Player::OnDamaged(Player* attacker)
58  {
59  	if (attacker == nullptr)
60  		return;
61  	if (IsDead())
62  		return;
63  
64  	// �� ü�� ��´�
65  	int damage = attacker->GetAttackDamage();
66  	AddHp(-damage);
67  
68  	// �ݰ�!
69  	attacker->OnDamaged(this);
70  }