/
Enemy.cpp
116 lines (103 loc) · 2.06 KB
/
Enemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "Enemy.h"
Enemy::Enemy()
{
SetHitpoints(ENEMY_DEFAULT_HITPOINTS) ;
Aggro = false;
CanMove = true;
CanAct = false;
Inactive = false;
}
Enemy::~Enemy()
{
}
bool
Enemy::IsAlive()
{
return (GetHitpoints()>0);
}
void
Enemy::Attack( GameObject *pObject )
{
int hp = pObject->GetHitpoints();
if ( (rand() % 10) < 3 )
{
// TODO: convert this into weapon
// with damage property. Can you do it? Is so, explain in your learning diary how.
pObject->SetHitpoints(hp-1);
cout << GetName() << " hits!\n";
}
else
cout << GetName() << " misses!\n";
}
void
Enemy::Update()
{
if(IsAlive() && Inactive==false)
{
CheckRoom();
if(Aggro == false && CanMove== true)
{
Move(South);
}
else if(Aggro ==true&&CanAct == true )
{
std::ostringstream s;
Player & player = GetGame()->GetPlayer();
s << GetName() << " attacks " << player.GetName() << "\n";
GetGame()->GetRenderer()->Render(s.str());
Attack(&player);
}
else if(CanAct == false && Aggro==true)
{
CanAct = true;
}
}
else if(IsAlive()==false&&Inactive == false)
{
GetGame()->GetRenderer()->Render("The enemy dies! You are victorious!\n");
//GetGame()->GetCurrentRoom()->SetDescription(GetGame()->GetPlayer().GetDesc());
Inactive = true;
}
}
Room*
Enemy::GetRoom()
{
return mRoom;
}
void
Enemy::SetRoom(Room* MRoom)
{
mRoom = MRoom;
}
void
Enemy::Move(Direction direction)
{
Room* isnow = GetRoom();
Room* next = isnow->GetNextRoom(direction);
if(next)
{
SetRoom(next);
cout << " Patrol moves south ";
}
}
void
Enemy::SetGame( Game * pGame ){m_pGame=pGame;}
////////////////////
Game *
Enemy::GetGame() {return m_pGame;}
//////////////
void
Enemy::CheckRoom()
{
if(GetGame()->GetCurrentRoom() == GetRoom())
{
if(Aggro == false)
{
GetGame()->GetRenderer()->Render("A wild enemy appears before you! it looks angry!\n");
GetGame()->GetPlayer().SetDesc(GetGame()->GetCurrentRoom()->GetDescription());
}
Aggro = true;
}
else
Aggro = false;
}