-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlien.cpp
184 lines (165 loc) · 3.56 KB
/
Alien.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <math.h>
using namespace std;
#include "Alien.h"
Alien::Alien():Game_Object('X')
{
attack_strength = 2;
range = 2.0;
target = NULL;
speed = 5;
cout << "Alien default constructed." << endl;
}
Alien::Alien(int in_id, Cart_Point in_loc):Game_Object(in_loc, in_id, 'X')
{
attack_strength = 2;
range = 2.0;
target = NULL;
speed = 5;
cout << "Alien constructed." << endl;
}
void Alien::start_attack(Person* in_target)
{
double target_dist = cart_distance(destination, in_target->get_location());
if(target_dist <= range)
{
Game_Object::show_status();
cout << ": Smash!" << endl;
target = in_target;
state = 'a';
}
else
{
Game_Object::show_status();
cout << ": Target out of range" << endl;
Game_Object::show_status();
cout << ": Chaaaaarge." << endl;
}
}
bool Alien::update()
{
switch(state){
case 's':
{
return false;
}
case 'm':
{
if (update_location() == true)
{
state = 's';
return true;
}
else
{
return false;
}
}
case 'a':
{
double target_dist = cart_distance(destination, target->get_location());
//target is no longer in range
if(target_dist > range)
{
state = 's';
Game_Object::show_status();
cout << ": Target out of range" << endl;
Game_Object::show_status();
cout << ": Chaaaaarge." << endl;
return true;
}
//target is in range
else
{
//target is dead
if(target->get_state() == 'x')
{
Game_Object::show_status();
cout << ": I win." << endl;
state = 's';
return true;
}
//target is still alive
else
{
Game_Object::show_status();
cout << ": Take that!" << endl;
target->take_hit(attack_strength);
return false;
}
}
}
}
}
void Alien::show_status()
{
Game_Object::show_status();
cout << " at " << location;
if (state == 's')
cout << " is stopped" << endl;
else if (state == 'a')
{
cout << " attacking astronaut A" << target->get_id() << endl;
}
else
cout << " moving at speed of " << speed << " towards " << destination << " at each step of " << delta << endl;
}
void Alien::start_moving(Cart_Point dest)
{
setup_destination(dest);
//If the alien is not at the location yet
if((delta.x != 0 && delta.y != 0) ||(delta.x == 0 && delta.y != 0) || (delta.x != 0 && delta.y == 0))
{
state = 'm';
cout << "Moving " << id_num;
cout << " to " << dest << endl;
Game_Object::show_status();
cout << ": On my way." << endl;
}
}
void Alien::stop()
{
state = 's';
Game_Object::show_status();
cout << ": Stopped." << endl;
}
bool Alien::update_location()
{
if (fabs(destination.x - location.x) <= fabs(delta.x) && fabs(destination.y - location.y) <= fabs(delta.y))
{
location = destination;
Game_Object::show_status();
cout << ": I'm there!" << endl;
return true;
}
else
{
location = location + delta;
Game_Object::show_status();
cout << ": step..." << endl;
return false;
}
}
void Alien::setup_destination(Cart_Point dest)
{
destination = dest;
if(destination.x == location.x && destination.y == location.y)
{
Game_Object::show_status();
cout << ": I'm already there. see?" << endl;
delta = Cart_Vector();
}
else
{
delta = (destination-location)*(speed/cart_distance(destination, location));
}
}
Alien::~Alien()
{
cout << "Alien destructed." << endl;
}
//changing the speed of alien
void Alien::setspeed(double new_speed)
{
speed = new_speed;
}