-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtankEnemy.cpp
125 lines (100 loc) · 2.05 KB
/
tankEnemy.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
#include "tankEnemy.h"
#include "utils.h"
#include "wall.h"
#include "bullet.h"
TankEnemy::TankEnemy()
{
m_type = GameObjectType_TankEnemy;
m_analizeTime = kEnemyAIAnalizeTime;
m_analizeTimer = GetRandomFloat( 0.0, m_analizeTime);
m_lastAnalizeX = 0.0;
m_lastAnalizeY = 0.0;
m_health = kEnemyHealth;
m_speed = kEnemySpeed;
setColor( ConsoleColor_DarkCyan, ConsoleColor_Cyan );
}
TankEnemy::~TankEnemy()
{
m_game->increaseDiedEnemiesCount();
}
void TankEnemy::update( float dt )
{
Tank::update( dt );
m_analizeTimer += dt;
if( m_analizeTimer >= m_analizeTime )
{
m_analizeTimer = 0;
analize();
}
}
void TankEnemy::analize()
{
float x = 0.0;
float y = 0.0;
calculateFrontCellPosition( &x, &y );
float xDelta = 0.0;
float yDelta = 0.0;
switch( m_direction )
{
case Direction_Left: xDelta = -1.0; break;
case Direction_Right: xDelta = 1.0; break;
case Direction_Up: yDelta = -1.0; break;
case Direction_Down: yDelta = 1.0; break;
}
GameObject* object = 0;
int distance = 0;
do
{
object = m_game->checkIntersects( x, y, 1, 1, 0 );
x += xDelta;
y += yDelta;
++distance;
}
while( object == 0 );
if( object->getType() == GameObjectType_Bullet )
{
if( ((Bullet*)object)->getOwnerType() == GameObjectType_TankPlayer )
fire();
return;
}
if( object->getType() == GameObjectType_TankPlayer
|| object->getType() == GameObjectType_Base )
{
fire();
return;
}
if( 0 == rand()%5 )
{
moveRandomDirection();
return;
}
switch( object->getType() )
{
case GameObjectType_Wall:
if( ((Wall*)object)->getInvulnerable() == false )
{
fire();
return;
}
break;
}
if( distance == 1
|| ((int)m_lastAnalizeX == (int)getX() && (int)m_lastAnalizeY == (int)getY()) )
{
moveRandomDirection();
return;
}
m_lastAnalizeX = getX();
m_lastAnalizeY = getY();
}
void TankEnemy::moveRandomDirection()
{
Direction prevDirection = getDirection();
Direction newDirection;
do
{
newDirection = (Direction)( rand()%((int)Direction_MAX) );
}
while( prevDirection == newDirection );
move( newDirection );
}