R-Type  2
Doom but in better
Loading...
Searching...
No Matches
PlayerBrain.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** PlayerBrain.cpp
6*/
7
15
16GUI::ECS::Online::PlayerBrain::PlayerBrain(const std::uint32_t entityId)
17 : EntityNode(entityId)
18{
19
20};
21
26
28 : EntityNode(entityId)
29{
30 update(copy);
31}
32
33
34void GUI::ECS::Online::PlayerBrain::setSprite(const std::shared_ptr<GUI::ECS::Components::SpriteComponent> &sprite, const std::shared_ptr<GUI::ECS::Components::SpriteComponent> &bullet)
35{
36 PRETTY_DEBUG << "Setting the sprite content" << std::endl;
37 _sprite = *sprite;
38 PRETTY_DEBUG << "The sprite entity is set" << std::endl;
39 _bullet.setSprite(*bullet);
40 PRETTY_DEBUG << "The bullet entity is set" << std::endl;
41 _bullet.setVisible(true);
42 _bullet.setEnemy(false);
43 _bullet.setDamage(5);
44 _bullet.setSpeed(5);
45 _bullet.setPosition({ 1,1 });
46 _bullet.setDirection({ 1,0 });
47 _bullet.setSize({ 0.5, 0.5 });
48 _bullet.tick();
49 PRETTY_DEBUG << "Bullet info has been set" << std::endl;
50 _collision.update(_sprite.getCollision());
51 _collision.setPosition(_bullet.getPosition());
52 PRETTY_DEBUG << "Sprite info: " << _sprite << std::endl;
53 PRETTY_DEBUG << "Bullet info: " << *bullet << std::endl;
54 PRETTY_DEBUG << "Collision info: " << _collision << std::endl;
55};
56
57void GUI::ECS::Online::PlayerBrain::setPosition(const std::pair<float, float> &pos)
58{
59 PRETTY_DEBUG << "In setPosition" << std::endl;
60 _collision.setPosition(pos);
61 _sprite.setPosition(pos);
62 _bullet.setPosition(pos);
63 PRETTY_DEBUG << "collision: " << _collision << "sprite: " << _sprite << "bullet:" << _bullet << std::endl;
64};
65
66void GUI::ECS::Online::PlayerBrain::setDimension(const std::pair<float, float> &dim)
67{
68 _collision.setDimension(dim);
69 _sprite.setDimension(dim);
70};
71
73{
74 _health = health;
75 if (health <= 0) {
76 _health = 0;
77 }
78}
79
81{
82 _visible = visible;
83 _sprite.setVisible(visible);
84};
85
86void GUI::ECS::Online::PlayerBrain::setBulletSize(const std::pair<float, float> &size)
87{
88 _bullet.setSize(size);
89}
90
92{
93 PRETTY_DEBUG << "Enemy Position: X = " << _collision.getPositionX()
94 << ", Y = " << _collision.getPositionY()
95 << ", Width = " << _collision.getWidth()
96 << ", Height = " << _collision.getHeight() << std::endl;
97
98 PRETTY_DEBUG << "External entity Position: X = " << second.getPositionX()
99 << ", Y = " << second.getPositionY()
100 << ", Width = " << second.getWidth()
101 << ", Height = " << second.getHeight() << std::endl;
102
103
104 PRETTY_DEBUG << "Setting the tolerance for the collision" << std::endl;
105 const float tolerance = 30.0f;
106 PRETTY_DEBUG << "The tolerance is set to: " << Recoded::myToString(tolerance) << std::endl;
107
108 PRETTY_DEBUG << "Calculating the overlap of X" << std::endl;
109 const bool xOverlap = (std::abs(_collision.getPositionX() - second.getPositionX()) <= (tolerance + (_collision.getWidth() + second.getWidth())));
110 PRETTY_DEBUG << "The X overlap was calculated at: " << Recoded::myToString(xOverlap) << std::endl;
111
112 PRETTY_DEBUG << "Calculating the overlap of Y" << std::endl;
113 const bool yOverlap = (std::abs(_collision.getPositionY() - second.getPositionY()) <= (tolerance + (_collision.getHeight() + second.getHeight())));
114 PRETTY_DEBUG << "The Y overlap was calculated at: " << Recoded::myToString(yOverlap) << std::endl;
115
116 PRETTY_DEBUG << "X Overlap: " << Recoded::myToString(xOverlap) << ", Y Overlap: " << Recoded::myToString(yOverlap) << std::endl;
117 PRETTY_DEBUG << "Checking if x and y are overlapping" << std::endl;
118 const bool overlapping = xOverlap && yOverlap;
119 PRETTY_DEBUG << "The result of if x and y are overlapping is: " << Recoded::myToString(overlapping) << std::endl;
120 return overlapping;
121};
122
124{
125 return _visible;
126};
127
128
130{
131 _sprite.checkTick();
132 _bullet.tick();
133};
134
136{
137 GUI::ECS::Online::Bullet shot(id, _bullet);
138 shot.setPosition(_collision.getPosition());
139 shot.setSize({ 0.5,0.5 });
140 shot.tick();
141 PRETTY_DEBUG << "User Position: " << _collision << std::endl;
142 PRETTY_DEBUG << "bullet: " << _bullet << ", shot: " << shot << 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
203const std::string GUI::ECS::Online::PlayerBrain::getInfo(const unsigned int indent) const
204{
205
206 std::string indentation = "";
207 for (unsigned int i = 0; i < indent; ++i) {
208 indentation += "\t";
209 }
210 std::string result = indentation + "Player brain:\n";
211 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
212 result += indentation + "- Visible: '" + Recoded::myToString(_visible) + "'\n";
213 result += indentation + "- Health: '" + Recoded::myToString(_health) + "'\n";
214 result += indentation + "- Bullet: {\n" + _bullet.getInfo(indent + 1) + indentation + "}\n";
215 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
216 result += indentation + "- Sprite: {\n" + _sprite.getInfo(indent + 1) + indentation + "}\n";
217
218 return result;
219}
220
221std::ostream &GUI::ECS::Online::operator<<(std::ostream &os, const GUI::ECS::Online::PlayerBrain &item)
222{
223 os << item.getInfo();
224 return os;
225}
#define PRETTY_DEBUG
Debug log with details and colour.
Declaration of the PlayerBrain class and related functionality.
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 setSize(const std::pair< float, float > &dimension)
Sets the size of the bullet.
Definition Bullet.cpp:78
void tick()
Advances the bullet's state by one tick.
Definition Bullet.cpp:89
Forward declaration of the Bullet class.
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 setDimension(const std::pair< float, float > &size)
Sets the player's dimensions.
const GUI::ECS::Online::Bullet shoot(const std::uint32_t id) const
Simulates the player shooting a bullet.
PlayerBrain & operator=(const GUI::ECS::Online::PlayerBrain &copy)
Overloads the assignment operator to copy from another PlayerBrain.
PlayerBrain(const std::uint32_t entityId=0)
Default constructor.
const bool isVisible() const
Checks if the player is currently visible.
void tick()
Updates the player's state for the current frame.
void update(const GUI::ECS::Online::PlayerBrain &copy)
Updates the player's state by copying another PlayerBrain.
void setVisible(const bool visible)
Sets the player's visibility state.
const GUI::ECS::Components::SpriteComponent render()
Renders the player's sprite to the screen.
const bool getVisible() const
Gets the player's visibility state.
const GUI::ECS::Systems::Collision getCollision() const
Gets the player's collision component.
const GUI::ECS::Online::Bullet getBullet() const
Gets the player's bullet instance.
void setPosition(const std::pair< float, float > &pos)
Sets the player's position.
const bool isColliding(const GUI::ECS::Systems::Collision &second) const
Checks if the player is colliding with another entity.
const long int getHealth() const
Gets the player's health value.
void setBulletSize(const std::pair< float, float > &size)
Sets the bullet dimensions.
void setSprite(const std::shared_ptr< GUI::ECS::Components::SpriteComponent > &sprite, const std::shared_ptr< GUI::ECS::Components::SpriteComponent > &bullet)
Sets the sprite and bullet sprite components for the player.
const GUI::ECS::Components::SpriteComponent getSprite() const
Gets the player's sprite component.
void setHealth(const long int health)
Sets the player's health.
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 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 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