-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShootingEnemy.cpp
131 lines (124 loc) · 5.98 KB
/
ShootingEnemy.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
#include "ShootingEnemy.h"
#include "Globals.h"
#include "Player.h"
#include "Map.h"
#include "Game.h"
#include "Gun.h"
#include <cmath>
#include <allegro5/allegro_primitives.h>
ShootingEnemy::ShootingEnemy(int health, double xPos, double yPos):
Enemy(health, xPos, yPos, health/10+1, _Gun)
{
InitializeWeaponProperties();
xVel = std::max(1.0,75.0/(1.0*health));
yVel = std::max(1.0,75.0/(1.0*health));
}
void ShootingEnemy::InitializeWeaponProperties()
{
weaponProperties[_None] = WeaponProperties(_None, 0);
weaponProperties[_Gun] = WeaponProperties(_Gun, 1);
weaponProperties[_Gun].SetRange(std::max(health,60));
weaponProperties[_Gun].SetDamage(std::min(2*(health/10+1),weaponProperties[_Gun].GetDamage()));
weaponProperties[_WideShot] = WeaponProperties(_WideShot, 0);
weaponProperties[_ExplodingShot] = WeaponProperties(_ExplodingShot, 0);
weaponProperties[_Grenade] = WeaponProperties(_Grenade, 0);
weaponProperties[_Mine] = WeaponProperties(_Mine, 0);
weaponProperties[_Nuke] = WeaponProperties(_Nuke, 0);
weaponProperties[_WallBreaker] = WeaponProperties(_WallBreaker, 0);
weaponProperties[_Enemy] = WeaponProperties(_Enemy, -1);
}
void ShootingEnemy::Attack ()
{
attackDelay--;
Player *p = Game::GetInstance()->GetPlayer();
if (attackDelay <= 0 && (abs(p->GetX()-GetX()) <= GetWeaponProperties(activeWeapon).GetRange() ||
abs(p->GetY()-GetY()) <= GetWeaponProperties(activeWeapon).GetRange()))
{
int xDiff = p->GetX()-GetX();
int yDiff = p->GetY()-GetY();
if (xDiff <= GetWeaponProperties(activeWeapon).GetRange() && xDiff >= 0 && abs(yDiff) < 10)
weapons.push_back(new Gun(GetX(), GetY(), GetWeaponProperties(activeWeapon), 1000, this));
else if (xDiff >= -GetWeaponProperties(activeWeapon).GetRange() && xDiff < 0 && abs(yDiff) < 10)
weapons.push_back(new Gun(GetX(), GetY(), GetWeaponProperties(activeWeapon), 100, this));
else if (yDiff <= GetWeaponProperties(activeWeapon).GetRange() && yDiff >= 0 && abs(xDiff) < 10)
weapons.push_back(new Gun(GetX(), GetY(), GetWeaponProperties(activeWeapon), 10, this));
else if (yDiff >= -GetWeaponProperties(activeWeapon).GetRange() && yDiff < 0 && abs(xDiff) < 10)
weapons.push_back(new Gun(GetX(), GetY(), GetWeaponProperties(activeWeapon), 1, this));
attackDelay = GetWeaponProperties(activeWeapon).GetFireRate();
}
}
void ShootingEnemy::Move ()
{
int **check = Map::GetInstance()->GetDistFromPlayer();
Player &p = *Game::GetInstance()->GetPlayer();
if (!(p.GetX()/BOX_PIXEL_WIDTH==GetX()/BOX_PIXEL_WIDTH&&p.GetY()/BOX_PIXEL_WIDTH==GetY()/BOX_PIXEL_WIDTH))
{
if (check[GetX()/BOX_PIXEL_WIDTH][GetY()/BOX_PIXEL_WIDTH] > 0)
{
int i = GetX()/BOX_PIXEL_WIDTH, j = GetY()/BOX_PIXEL_WIDTH;
if (check[i-1][j] == check[i][j]-1)
{
if (abs(GetY() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) < BOX_PIXEL_WIDTH/2 - radius)
xPos-=xVel;
else
yPos += (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)*yVel;
}
else if (check[i+1][j] == check[i][j]-1)
{
if (abs(GetY() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) < BOX_PIXEL_WIDTH/2-radius)
xPos+=xVel;
else
yPos += (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)*yVel;
}
else if (check[i][j-1] == check[i][j]-1)
{
if (abs(GetX() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) < BOX_PIXEL_WIDTH/2-radius)
yPos-=yVel;
else
xPos += (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)*xVel;
}
else if (check[i][j+1] == check[i][j]-1)
{
if (abs(GetX() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) < BOX_PIXEL_WIDTH/2-radius)
yPos+=yVel;
else
xPos += (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)*xVel;
}
}
}
else
{
int x = -GetX()+p.GetX(), y = -GetY()+p.GetY();
if (x != 0)
{
if (abs(GetX() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) >= BOX_PIXEL_WIDTH/2-radius)
xPos += (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)*xVel;
else
{
xPos += x/abs(x)*std::min(xVel,(double)abs(x));
if (abs(GetX() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) >= BOX_PIXEL_WIDTH/2-radius)
xPos += (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetX() % BOX_PIXEL_WIDTH)*xVel;
}
}
if (y != 0)
{
if (abs(GetY() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) >= BOX_PIXEL_WIDTH/2-radius)
yPos += (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)*yVel;
else
{
yPos += y/abs(y)*std::min(yVel,(double)abs(y));
if (abs(GetY() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) >= BOX_PIXEL_WIDTH-radius)
yPos += (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)/abs (BOX_PIXEL_WIDTH/2 - GetY() % BOX_PIXEL_WIDTH)*yVel;
}
}
}
}
void ShootingEnemy::Draw(ALLEGRO_BITMAP *buffer, int midX, int midY)
{
for (unsigned int i = 0; i < weapons.size(); i++)
weapons[i]->Draw(buffer, midX, midY);
al_set_target_bitmap(buffer);
al_draw_filled_circle (midX+GetX(), midY+GetY(), radius, al_map_rgb (255,150,150));
al_draw_filled_circle (midX+GetX(), midY+GetY(), radius-GetHealth()/10-1, al_map_rgb (255,0,0));
al_draw_circle (midX+GetX(), midY+GetY(), radius, al_map_rgb (255,0,0), 0);
}