-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
179 lines (157 loc) · 4.14 KB
/
Copy pathEnemy.cpp
File metadata and controls
179 lines (157 loc) · 4.14 KB
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
#include "src/Enemy.h"
namespace physics{
Enemy::Enemy() : _health(100), _alive(true), _point(0),
_alert(false), alertRadius(20.0f)
{
_position = Vector3f(42.0f, 0.5f, 42.0f); //Arbitrary position
Graphics::createCircle(_verts, _norms);
createStrongPatrol();
}
Enemy::Enemy(Vector3f pos) : _health(100), _alive(true), _point(0),
_alert(false), alertRadius(20.0f)
{
_position = pos;
Graphics::createCircle(_verts,_norms);
createStrongPatrol();
}
bool Enemy::isLiving()
{
if(!_alive)
{
return false;
}
else if(_health <= 0)
{
die();
return false;
}
return _alive;
}
void Enemy::attack(Vector3f target)
{
}
void Enemy::createSimplePatrol()
{
std::default_random_engine generator;
std::uniform_int_distribution<unsigned int> distribution(1, 42);
unsigned int rangeX = distribution(generator);
unsigned int rangeZ = distribution(generator);
Vector3f pos = _position;
float floating = (float)(util::randomRange(0,50)%50)/100.0;
floating *= _position.y<0,5 ? 1 : -1;
pos.x += rangeX;
_patrolPath.push_back(pos);
pos.z += rangeX;
_patrolPath.push_back(pos);
pos.x -= rangeX;
_patrolPath.push_back(pos);
pos.z -= rangeZ;
_patrolPath.push_back(pos);
}
void Enemy::decreaseHealth()
{
_health -= 10;
}
void Enemy::decreaseHealth(int damage)
{
switch(damage)
{
case 0:
_health -= 100;
break;
case 1:
_health -= 50;
break;
case 2:
_health -= 25;
break;
default:
_health -= 10;
break;
}
}
void Enemy::die()
{
_alive = false;
}
void Enemy::patrol(Vector3f target)
{
if(physics::PhysicsEngine::spheresphere(_position, alertRadius, target, 0.5f) )
{
//Fire at player
//_radius = 2.0f;
if(_radius <= MAX_SIZE)
_radius += GROWRATE;
_weapon.fire(_position, target, true);
/*_velocity = target - _position;
_velocity.normalize();
_velocity = _velocity * 3.0f;
_force = _velocity * 1.5f;
update();
*/
}
else
{
//_radius = 0.5f;
if(_radius >= MIN_SIZE)
_radius -= GROWRATE;
if(!_patrolPath.empty())
{
if(!(_position.x - _patrolPath[_point].x < 0.25f && _position.x - _patrolPath[_point].x > -0.25f &&
_position.y - _patrolPath[_point].y < 0.25f && _position.y - _patrolPath[_point].y > - 0.25f &&
_position.z - _patrolPath[_point].z < 0.25f && _position.z - _patrolPath[_point].z > -0.25f ))
{
_velocity = _patrolPath[_point] - _position;//_velocity is a physics entity attribute
_velocity.normalize();
_position = _position + _velocity*0.125f;
}
else
{
++_point;
if(_point >= _patrolPath.size() && !_searching)
_point = 0;
else if(_point >= _patrolPath.size() && _searching)
{
searchNDestroy();
_point = 0;
}
}
}
}
}
void Enemy::createStrongPatrol()
{
Vector3f player = AIManager::getInstance().getPlayer();
Vector3f dest; //destination
Node* path = AIManager::getInstance().astar(_position, player);
if(path != NULL)
{
path->traverse(_patrolPath);
}
dest = AIManager::getInstance().randVec3f();
path = AIManager::getInstance().astar(player, dest);
if(path != NULL)
{
path->traverse(_patrolPath);
}
path = AIManager::getInstance().astar(dest, _position);
if(path != NULL)
{
path->traverse(_patrolPath);
}
}
void Enemy::searchNDestroy()
{
_patrolPath.clear();
_searching = true;
Node *path = AIManager::getInstance().astar(_position,
AIManager::getInstance().getPlayer());
if(path != NULL)
{
path->traverse(_patrolPath);
}
}
void Enemy::target()
{
}
}//namespace physics