R-Type  2
Doom but in better
Loading...
Searching...
No Matches
EnemyBrain.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** EnemyBrain.cpp
6*/
7
15
16GUI::ECS::Demo::EnemyBrain::EnemyBrain(const std::uint32_t entityId)
17 : EntityNode(entityId)
18{
19
20};
21
26
28 : EntityNode(entityId)
29{
30 update(copy);
31}
32
33void GUI::ECS::Demo::EnemyBrain::setSprite(const std::shared_ptr<GUI::ECS::Components::SpriteComponent> &sprite, const std::shared_ptr<GUI::ECS::Components::SpriteComponent> &bullet)
34{
35 PRETTY_DEBUG << "Setting the sprite content" << std::endl;
36 _sprite = *sprite;
37 PRETTY_DEBUG << "The sprite entity is set" << std::endl;
38 _bullet.setSprite(*bullet);
39 PRETTY_DEBUG << "The bullet entity is set" << std::endl;
40 _bullet.setVisible(true);
41 _bullet.setEnemy(true);
42 _bullet.setDamage(5);
43 _bullet.setSpeed(5);
44 _bullet.setPosition({ 1,1 });
45 _bullet.setDirection({ -1,0 });
46 _bullet.setSize({ 0.5, 0.5 });
47 _bullet.tick();
48 PRETTY_DEBUG << "Bullet info has been set" << std::endl;
49 _collision.update(_sprite.getCollision());
50 _collision.setPosition(_bullet.getPosition());
51 PRETTY_DEBUG << "Sprite info: " << _sprite << std::endl;
52 PRETTY_DEBUG << "Bullet info: " << *bullet << std::endl;
53 PRETTY_DEBUG << "Collision info: " << _collision << std::endl;
54};
55
56void GUI::ECS::Demo::EnemyBrain::setPosition(const std::pair<float, float> &pos)
57{
58 PRETTY_DEBUG << "In setPosition" << std::endl;
59 _collision.setPosition(pos);
60 _sprite.setPosition(pos);
61 _bullet.setPosition(pos);
62 PRETTY_DEBUG << "collision: " << _collision << "sprite: " << _sprite << "bullet:" << _bullet << std::endl;
63};
64
65void GUI::ECS::Demo::EnemyBrain::setDimension(const std::pair<float, float> &dim)
66{
67 _collision.setDimension(dim);
68 _sprite.setDimension(dim);
69};
70
71void GUI::ECS::Demo::EnemyBrain::setHealth(const long int health)
72{
73 _health = health;
74 if (health <= 0) {
75 _health = 0;
76 }
77}
78
80{
81 _visible = visible;
82 _sprite.setVisible(visible);
83};
84
85void GUI::ECS::Demo::EnemyBrain::setBulletSize(const std::pair<float, float> &size)
86{
87 _bullet.setSize(size);
88}
89
91{
92 PRETTY_DEBUG << "Enemy Position: X = " << _collision.getPositionX()
93 << ", Y = " << _collision.getPositionY()
94 << ", Width = " << _collision.getWidth()
95 << ", Height = " << _collision.getHeight() << std::endl;
96
97 PRETTY_DEBUG << "External entity Position: X = " << second.getPositionX()
98 << ", Y = " << second.getPositionY()
99 << ", Width = " << second.getWidth()
100 << ", Height = " << second.getHeight() << std::endl;
101
102
103 PRETTY_DEBUG << "Setting the tolerance for the collision" << std::endl;
104 const float tolerance = 30.0f;
105 PRETTY_DEBUG << "The tolerance is set to: " << Recoded::myToString(tolerance) << std::endl;
106
107 PRETTY_DEBUG << "Calculating the overlap of X" << std::endl;
108 const bool xOverlap = (std::abs(_collision.getPositionX() - second.getPositionX()) <= (tolerance + (_collision.getWidth() + second.getWidth())));
109 PRETTY_DEBUG << "The X overlap was calculated at: " << Recoded::myToString(xOverlap) << std::endl;
110
111 PRETTY_DEBUG << "Calculating the overlap of Y" << std::endl;
112 const bool yOverlap = (std::abs(_collision.getPositionY() - second.getPositionY()) <= (tolerance + (_collision.getHeight() + second.getHeight())));
113 PRETTY_DEBUG << "The Y overlap was calculated at: " << Recoded::myToString(yOverlap) << std::endl;
114
115 PRETTY_DEBUG << "X Overlap: " << Recoded::myToString(xOverlap) << ", Y Overlap: " << Recoded::myToString(yOverlap) << std::endl;
116 PRETTY_DEBUG << "Checking if x and y are overlapping" << std::endl;
117 const bool overlapping = xOverlap && yOverlap;
118 PRETTY_DEBUG << "The result of if x and y are overlapping is: " << Recoded::myToString(overlapping) << std::endl;
119 return overlapping;
120};
121
123{
124 return _visible;
125};
126
127const std::optional<GUI::ECS::Demo::Bullet> GUI::ECS::Demo::EnemyBrain::tick()
128{
129 PRETTY_DEBUG << "In enemy brain tick" << std::endl;
130 _sprite.checkTick();
131 if (_delayBeforeShot == 0) {
132 _delayBeforeShot = _randInt(20, 80);
133 GUI::ECS::Demo::Bullet shot(_bullet);
134 shot.tick();
135 shot.setDamage(_randInt(1, 20));
136 shot.setPosition(_collision.getPosition());
137 PRETTY_DEBUG << "User Position: " << _collision << std::endl;
138 PRETTY_DEBUG << "bullet: " << _bullet << ", shot: " << shot << std::endl;
139 PRETTY_DEBUG << "Shot has been sent" << std::endl;
140 return std::optional(shot);
141 }
142 _delayBeforeShot--;
143 PRETTY_DEBUG << "No shot has been sent" << std::endl;
144 return std::nullopt;
145};
146
151
158{
159 _health = copy.getHealth();
160 _bullet = copy.getBullet();
161 _sprite = copy.getSprite();
162 _visible = copy.getVisible();
163 _collision = copy.getCollision();
164}
165
174{
175 update(copy);
176 return *this;
177}
178
180{
181 return _visible;
182}
183
185{
186 return _health;
187}
188
190{
191 return _bullet;
192}
193
198
200{
201 return _collision;
202}
203
204
205const int GUI::ECS::Demo::EnemyBrain::_randInt(int min, int max)
206{
207 static std::random_device rd;
208 static std::mt19937 gen(rd());
209 std::uniform_int_distribution<> dist(min, max);
210 return dist(gen);
211}
212
213
214const std::string GUI::ECS::Demo::EnemyBrain::getInfo(const unsigned int indent) const
215{
216
217 std::string indentation = "";
218 for (unsigned int i = 0; i < indent; ++i) {
219 indentation += "\t";
220 }
221 std::string result = indentation + "Enemy brain:\n";
222 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
223 result += indentation + "- Visible: '" + Recoded::myToString(_visible) + "'\n";
224 result += indentation + "- Health: '" + Recoded::myToString(_health) + "'\n";
225 result += indentation + "- Delay before shot: '" + Recoded::myToString(_delayBeforeShot) + "'\n";
226 result += indentation + "- Bullet: {\n" + _bullet.getInfo(indent + 1) + indentation + "}\n";
227 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
228 result += indentation + "- Sprite: {\n" + _sprite.getInfo(indent + 1) + indentation + "}\n";
229
230 return result;
231}
232
233std::ostream &GUI::ECS::Demo::operator<<(std::ostream &os, const GUI::ECS::Demo::EnemyBrain &item)
234{
235 os << item.getInfo();
236 return os;
237}
Declaration of the EnemyBrain class and its related functionality.
#define PRETTY_DEBUG
Debug log with details and colour.
std::random_device rd
Definition Random.cpp:4
Represents a drawable and interactive sprite in the ECS system.
Represents a bullet entity in the game.
Definition Bullet.hpp:31
void setPosition(const std::pair< float, float > &pos)
Sets the position of the bullet.
Definition Bullet.cpp:52
void tick()
Advances the bullet's state by one tick.
Definition Bullet.cpp:89
void setDamage(const int damage)
Sets the damage dealt by the bullet.
Definition Bullet.cpp:84
The EnemyBrain class represents the logic and behavior of an enemy entity.
const bool getVisible() const
Gets the visibility status of the enemy.
const std::optional< GUI::ECS::Demo::Bullet > tick()
Updates the enemy's state and determines if it should fire a bullet.
void update(const GUI::ECS::Demo::EnemyBrain &copy)
Updates the sprite by copying another EnemyBrain.
void setVisible(const bool visible)
Sets the visibility of the enemy.
const GUI::ECS::Systems::Collision getCollision() const
Gets the collision data for the enemy.
void setPosition(const std::pair< float, float > &pos)
Sets the position of the enemy.
const bool isColliding(const GUI::ECS::Systems::Collision &second) const
Checks if the enemy is colliding with another entity.
const GUI::ECS::Demo::Bullet getBullet() const
Gets the bullet data for the enemy.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
const GUI::ECS::Components::SpriteComponent render()
Renders the enemy's sprite.
EnemyBrain & operator=(const GUI::ECS::Demo::EnemyBrain &copy)
Overloads the assignment operator to copy from another EnemyBrain.
void setHealth(const long int health)
Sets the health of the enemy.
void setSprite(const std::shared_ptr< GUI::ECS::Components::SpriteComponent > &sprite, const std::shared_ptr< GUI::ECS::Components::SpriteComponent > &bullet)
Sets the sprite components for the enemy and its bullet.
const bool isVisible() const
Checks if the enemy is visible.
const long int getHealth() const
Gets the health of the enemy.
void setDimension(const std::pair< float, float > &size)
Sets the dimensions of the enemy.
const GUI::ECS::Components::SpriteComponent getSprite() const
Gets the sprite data for the enemy.
EnemyBrain(const std::uint32_t entityId=0)
Default constructor.
void setBulletSize(const std::pair< float, float > &size)
Sets the size of the bullet.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
const float getWidth() const
Gets the width of the component.
const float getHeight() const
Gets the height of the component.
const float getPositionY() const
Gets the Y-coordinate of the component's position.
const float getPositionX() const
Gets the X-coordinate of the component's position.
std::ostream & operator<<(std::ostream &os, const Bullet &item)
Outputs the sprite's info to a stream.
Definition Bullet.cpp:259
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223