-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemy2.cpp
99 lines (86 loc) · 1.98 KB
/
Enemy2.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
#include "Enemy2.h"
// private functions
// constructor and distructor
Enemy2::Enemy2(int offset, sf::Texture n_texture, sf::Texture f_texture)
{
normal_texture = n_texture;
freezed_texture = f_texture;
srand((unsigned)(time(NULL) + offset));
state = rand() % 300;
stabled = false;
speed = 1.f;
direction = RIGHT;
}
Enemy2::~Enemy2()
{
}
// FUNCS
void Enemy2::set_texture(sf::Sprite new_enemy)
{
enemy = new_enemy;
}
bool Enemy2::is_change_time()
{
if (state % CHANGE_MODE_TIME == 0)
{
if (stabled)
enemy.setTexture(normal_texture);
else
enemy.setTexture(freezed_texture);
return true;
}
return false;
}
void Enemy2::move()
{
if (is_change_time())
{
state %= CHANGE_MODE_TIME;
stabled = !stabled;
}
if (!stabled)
enemy.move(speed * direction, 0);
state++;
}
bool Enemy2::is_on_ground(sf::Sprite ground)
{
bool on_ground = false;
enemy.move(50 * direction, 50 * speed);
on_ground = collided(ground);
enemy.move(-50 * direction, -50 * speed);
return on_ground;
}
void Enemy2::go_back()
{
direction *= -1;
enemy.scale(-1.f, 1.f);
}
void Enemy2::render(sf::RenderTarget &target)
{
target.draw(enemy);
move();
}
int Enemy2::get_dir()
{
return direction;
}
bool Enemy2::collided(sf::Sprite target)
{
if (enemy.getGlobalBounds().intersects(target.getGlobalBounds()))
{
return true;
}
return false;
}
bool Enemy2::is_in_world(sf::Sprite world)
{
if (enemy.getGlobalBounds().left >= world.getGlobalBounds().left &&
enemy.getGlobalBounds().top >= world.getGlobalBounds().top &&
enemy.getGlobalBounds().left + enemy.getGlobalBounds().width <=
world.getGlobalBounds().left + world.getGlobalBounds().width &&
enemy.getGlobalBounds().top + enemy.getGlobalBounds().height <= world.getGlobalBounds().top + world.getGlobalBounds().height)
{
return true;
}
return false;
}