-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
121 lines (117 loc) · 3.56 KB
/
enemy.cpp
File metadata and controls
121 lines (117 loc) · 3.56 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
#include "enemy.hpp"
//Private functions
void Enemy::initVariables(){
this->movementSpeed = 1.f;
this->posX = rand()%(this->window->getSize().x);
this->isActive = false;
}
void Enemy::initShape(){
this->shape.setSize(sf::Vector2f(100.f, 30.f));
this->shape.setFillColor(sf::Color::Red);
if((float)this->posX > (float)((float)this->window->getSize().x-(1.5f)*(float)this->shape.getSize().x))
this->posX-=(1.5f)*this->shape.getSize().x;
this->shape.setPosition(sf::Vector2f((float)this->posX, 0));
}
void Enemy::initText(){
this->enemyText.setFont(*this->font);
std::string text;
if(this->difficulty < 5){
int i = (rand()%(this->difficulty+1))+3;
int j = rand()%this->WORDS[i].size();// (int)(*this->WORDS)[i].size();
while((*this->usedKey)[this->WORDS[i][j][0]]==1){
i = (rand()%(this->difficulty+1))+3;
j = rand()%(int)this->WORDS[i].size();
}
(*this->usedKey)[this->WORDS[i][j][0]]=1;
text = this->WORDS[i][j];
}
else
if(this->difficulty < 10){
int i = (rand()%std::min(this->difficulty+2, 11))+3;
if(i > 12)
i = 12;
int j = rand()%(int)this->WORDS[i].size();
while((*this->usedKey)[this->WORDS[i][j][0]]==1){
i = (rand()%std::min(this->difficulty+2, 11))+3;
if(i > 12)
i = 12;
j = rand()%(int)this->WORDS[i].size();
}
(*this->usedKey)[this->WORDS[i][j][0]]=1;
text = this->WORDS[i][j];
}
else
if(this->difficulty >=10){
int i = (rand()%std::min(this->difficulty+2, 11))+4;
if(i > 12)
i = 12;
int j = rand()%(int)this->WORDS[i].size();
while((*this->usedKey)[this->WORDS[i][j][0]]==1){
i = (rand()%std::min(this->difficulty+2, 11))+4;
if(i > 12)
i = 12;
j = rand()%(int)this->WORDS[i].size();
}
(*this->usedKey)[this->WORDS[i][j][0]]=1;
text = this->WORDS[i][j];
}
this->enemyText.setString(text);
this->enemyText.setCharacterSize(18);
this->enemyText.setOutlineThickness(0.8f);
this->enemyText.setOutlineColor(sf::Color::White);
this->enemyText.setPosition(this->shape.getPosition().x+this->shape.getGlobalBounds().width/2-this->enemyText.getGlobalBounds().width/2, this->shape.getPosition().y+3);
}
//Constructors
Enemy::Enemy(sf::RenderWindow* window, std::vector<std::string>* WORDS, std::unordered_map<char, int>* usedKey, int difficulty, sf::Font* font){
this->window = window;
this->font = font;
this->difficulty = difficulty;
this->WORDS = WORDS;
this->usedKey = usedKey;
this->initVariables();
this->initShape();
this->initText();
}
Enemy::~Enemy(){
}
//Accessors
sf::Vector2f Enemy::getShapePos(){
return this->shape.getPosition();
}
sf::Vector2f Enemy::getShapeSize(){
return this->shape.getSize();
}
std::string Enemy::getEnemyText(){
return this->enemyText.getString();
}
void Enemy::setEnemyText(std::string updating){
this->enemyText.setString(updating);
}
void Enemy::activate(){
this->isActive = true;
}
//Functions
void Enemy::updateTextPos(){
this->enemyText.setPosition(this->shape.getPosition().x+this->shape.getGlobalBounds().width/2-this->enemyText.getGlobalBounds().width/2, this->shape.getPosition().y+3);
}
void Enemy::updateEnemyPos(){
this->shape.move(0, this->movementSpeed);
this->enemyText.move(0, this->movementSpeed);
}
void Enemy::updateTextColor(){
if(this->isActive){
this->enemyText.setOutlineThickness(1.f);
this->enemyText.setOutlineColor(sf::Color::Cyan);
this->enemyText.setFillColor(sf::Color::Green);
this->isActive = false;
}
}
void Enemy::update(){
this->updateEnemyPos();
this->updateTextPos();
this->updateTextColor();
}
void Enemy::render(sf::RenderTarget* target){
target->draw(this->shape);
target->draw(this->enemyText);
}