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::Online::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::Online::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::Online::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::Online::EnemyBrain::setDimension(const std::pair<float, float> &dim)
66{
67 _collision.setDimension(dim);
68 _sprite.setDimension(dim);
69};
70
71void GUI::ECS::Online::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::Online::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
128{
129 PRETTY_DEBUG << "In enemy brain tick" << std::endl;
130 _sprite.checkTick();
131
132};
133
135{
136 GUI::ECS::Online::Bullet shot(id, _bullet);
137 shot.tick();
138 shot.setDamage(1);
139 shot.setPosition(_collision.getPosition());
140 PRETTY_DEBUG << "User Position: " << _collision << std::endl;
141 PRETTY_DEBUG << "bullet: " << _bullet << ", shot: " << shot << std::endl;
142 PRETTY_DEBUG << "Shot has been sent" << std::endl;
143 return shot;
144}
145
150
157{
158 _health = copy.getHealth();
159 _bullet = copy.getBullet();
160 _sprite = copy.getSprite();
161 _visible = copy.getVisible();
162 _collision = copy.getCollision();
163}
164
177
179{
180 return _visible;
181}
182
184{
185 return _health;
186}
187
189{
190 return _bullet;
191}
192
197
202
203
204const int GUI::ECS::Online::EnemyBrain::_randInt(int min, int max)
205{
206 static std::random_device rd;
207 static std::mt19937 gen(rd());
208 std::uniform_int_distribution<> dist(min, max);
209 return dist(gen);
210}
211
212
213const std::string GUI::ECS::Online::EnemyBrain::getInfo(const unsigned int indent) const
214{
215
216 std::string indentation = "";
217 for (unsigned int i = 0; i < indent; ++i) {
218 indentation += "\t";
219 }
220 std::string result = indentation + "Enemy brain:\n";
221 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
222 result += indentation + "- Visible: '" + Recoded::myToString(_visible) + "'\n";
223 result += indentation + "- Health: '" + Recoded::myToString(_health) + "'\n";
224 result += indentation + "- Delay before shot: '" + Recoded::myToString(_delayBeforeShot) + "'\n";
225 result += indentation + "- Bullet: {\n" + _bullet.getInfo(indent + 1) + indentation + "}\n";
226 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
227 result += indentation + "- Sprite: {\n" + _sprite.getInfo(indent + 1) + indentation + "}\n";
228
229 return result;
230}
231
232std::ostream &GUI::ECS::Online::operator<<(std::ostream &os, const GUI::ECS::Online::EnemyBrain &item)
233{
234 os << item.getInfo();
235 return os;
236}
#define PRETTY_DEBUG
Debug log with details and colour.
Declaration of the EnemyBrain class and its related functionality.
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 setDamage(const int damage)
Sets the damage dealt by the bullet.
Definition Bullet.cpp:84
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
The EnemyBrain class represents the logic and behavior of an enemy entity.
const GUI::ECS::Components::SpriteComponent render()
Renders the enemy's sprite.
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 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 ...
void setVisible(const bool visible)
Sets the visibility of the enemy.
const bool isVisible() const
Checks if the enemy is visible.
const bool isColliding(const GUI::ECS::Systems::Collision &second) const
Checks if the enemy is colliding with another entity.
void setPosition(const std::pair< float, float > &pos)
Sets the position of the enemy.
EnemyBrain & operator=(const GUI::ECS::Online::EnemyBrain &copy)
Overloads the assignment operator to copy from another EnemyBrain.
EnemyBrain(const std::uint32_t entityId=0)
Default constructor.
void setDimension(const std::pair< float, float > &size)
Sets the dimensions of the enemy.
const long int getHealth() const
Gets the health of the enemy.
const GUI::ECS::Online::Bullet shoot(const std::uint32_t id)
Make the enemy shoot a projectile with a defined id.
const bool getVisible() const
Gets the visibility status of the enemy.
const GUI::ECS::Components::SpriteComponent getSprite() const
Gets the sprite data for the enemy.
void setBulletSize(const std::pair< float, float > &size)
Sets the size of the bullet.
const GUI::ECS::Systems::Collision getCollision() const
Gets the collision data for the enemy.
void update(const GUI::ECS::Online::EnemyBrain &copy)
Updates the sprite by copying another EnemyBrain.
void tick()
Updates the enemy's state and determines if it should fire a bullet.
const GUI::ECS::Online::Bullet getBullet() const
Gets the bullet data for the enemy.
void setHealth(const long int health)
Sets the health of the enemy.
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:260
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223