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 << "HP: " << _hp << endl;
53  	cout << "ATT: " << _attack << endl;
54  }
55  
56  void Player::OnDamaged(Player* attacker)
57  {
58  	if (attacker == nullptr)
59  		return;
60  	if (IsDead())
61  		return;
62  
63  	// �� ü�� ��´�
64  	int damage = attacker->GetAttackDamage();
65  	AddHp(-damage);
66  
67  	// �ݰ�!
68  	attacker->OnDamaged(this);
69  }