-
Notifications
You must be signed in to change notification settings - Fork 0
/
Soldier.cpp
157 lines (135 loc) · 3.87 KB
/
Soldier.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include "Soldier.h"
#include "Person.h"
#include "Cart_Point.h"
#include "Model.h"
using namespace std;
Soldier::Soldier(): Person('S')
//Invokes Person('S'), initializes attack_strength to 2, range to 2.0, and outputs a message
{
attack_strength = 2;
range = 2.0;
cout << " Default Soldier constructed.\n";
}
Soldier::Soldier(int in_id, Cart_Point in_loc): Person('S', in_id, in_loc)
//Invokes Person('S', in_id, in_loc), initializes attack_strength to 2, range to 2.0, and outputs a message
{
attack_strength = 2;
range = 2.0;
cout << " Soldier constructed.\n";
}
Soldier::~Soldier()
//Prints a message when the Model class deletes a Soldier object
{
cout << " Soldier destructed.\n";
}
void Soldier::start_attack(Person * in_target)
//If the distance to the target is less than or equal to the range, outputs a message, saves the target pointer, and sets the state to 'a' (attack). if it is too far away, outputs a message
{
if (is_alive())
{
if (cart_distance(location, in_target->get_location()) <= range)
{
target = in_target;
state = 'a';
cout << 'S' << get_id() << ":Attacking.\n";
}
else
{
cout << "Target is out of range\n";
}
}
else
{
//If the Soldier is dead, print a message
cout << 'S' << get_id() << ": I am dead. You do not have the ability to summon the undead.\n";
}
}
bool Soldier::update()
//Updates the Soldier according to its state
{
switch (state)
{
//state = "dead": Soldier does nothing and stays in this state
case 'x': return false;
break;
//state = "stopped": Soldier does nothing and stays in this state
case 's': return false;
break;
//state = "moving to a destination": calls update_location to take a step
case 'm':
//If the object has arrived, sets the state to stopped and returns true
if (update_location())
{
state = 's';
return true;
}
break;
//state = "attack": If the target is within range and alive, the soldier attacks. If not, prints a message
case 'a':
//If the distance is out of range, prints a message, sets the state to stopped, and returns true
if (cart_distance(location, target->get_location()) > range)
{
cout << "Target is out of range\n";
state = 's';
return true;
}
//If the distange is in range, checks to see whether the target is alive
else
{
if (target->is_alive())
{
//If the target is alive, outputs a message and calls the target's take_hit function, stays in the state, and returns false
cout << display_code << get_id() << ":Take that!\n";
target->take_hit(attack_strength, this);
return false;
}
else
{
//If the target is dead, output a message, set the state to stopped, and return true
cout << display_code << get_id() << ":I win.\n";
state = 's';
return true;
}
}
break;
}
}
void Soldier::show_status()
//Outputs "Soldier status: ", calls Person::show_status(), and outputs whether the object is attacking
{
cout << "Soldier status: ";
Person::show_status();
if (state == 'a')
{
cout << "Attacking.\n";
}
}
void Soldier::save(ofstream& file)
//Calls the save function for Person, then writes to the file the member variables declared in this class
{
Person::save(file);
if (target)
{
file << target->get_id() << endl;
}
else
{
file << -1 << endl;
}
}
void Soldier::restore(ifstream& file, Model& model)
//Reads from the file into the member variables declared in this class
{
Person::restore(file, model);
//Sets target
int id;
file >> id;
target = model.get_Person_ptr(id);
}
void Soldier::take_hit(int attack_strength, Person* attacker_ptr)
//Tells the soldier to attack the attacker
{
Person::take_hit(attack_strength, attacker_ptr);
this->start_attack(attacker_ptr);
}