-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeleeEnemyRed.cpp
58 lines (50 loc) · 1.69 KB
/
MeleeEnemyRed.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
#include "MeleeEnemyRed.hpp"
#include "GameState.hpp"
#include <iostream>
MeleeEnemyRed::MeleeEnemyRed(glm::vec2 position, glm::vec2 velocity, Sprite* s, GameState* gs)
: Enemy(position, velocity, s, gs) {
this->time_step = 5.0f;
this->hp = 3.0f;
this->color = glm::u8vec4(255,100,100,255);
this->anim = enemy_sprites->sprites.at("melee_run");
};
void MeleeEnemyRed::update(float elapsed) {
// cout << time_step << endl;
time_step += elapsed;
anim_elapsed += elapsed;
}
void MeleeEnemyRed::move(float elapsed, const glm::vec2 &player_pos, Map &map) {
glm::vec2 old_pos = this->position;
glm::vec2 direction = glm::normalize(player_pos - this->position);
this->set_vel(direction);
this->position += direction * speed_val* 2.0f * elapsed;
if (map.ValueAtWorld(this->position.x, this->position.y) == 0) {
// this->position = old_pos;
if (map.ValueAtWorld(old_pos.x, this->position.y) != 0) {
this->position.x = old_pos.x;
} else if (map.ValueAtWorld(this->position.x, old_pos.y) != 0) {
this->position.y = old_pos.y;
} else {
this->position = old_pos;
}
}
//std::cout << "BasicEnemyGreen::move" << this->position.x << "&" << this->position.y << std::endl;
}
Bullet* MeleeEnemyRed::do_attack(const glm::vec2 &player_pos) {
if(time_step >= 2.0f && distance(player_pos) < 1.0f) {
Bullet* b = new Bullet(this->position,
glm::normalize(player_pos - this->position + 1.0f)
);
time_step = 0.0f;
return b;
}
return nullptr;
}
void MeleeEnemyRed::on_hit(Bullet* b) {
if(b->get_rgb().find(RGB::Blue) != b->get_rgb().end()) {
this->hp -= b->get_damage();
}
else {
this->hp -= 0.5f * b->get_damage();
}
}