R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Bullet.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Bullet.cpp
6*/
7
15
16GUI::ECS::Online::Bullet::Bullet(const std::uint32_t EntityId)
17 : EntityNode(EntityId)
18{
19};
20
21GUI::ECS::Online::Bullet::Bullet(const GUI::ECS::Components::SpriteComponent &sprite, const bool fromEnemy, const std::pair<int, int> &positionInitial, const unsigned int speed, const std::pair<int, int> &direction, const int damage)
22 : EntityNode(0), _sprite(sprite), _fromEnemy(fromEnemy), _speed(speed), _direction(direction), _positionInitial(positionInitial), _damage(damage)
23{
24 _createBullet(positionInitial);
25};
26
27GUI::ECS::Online::Bullet::Bullet(const std::uint32_t EntityId, const GUI::ECS::Components::SpriteComponent &sprite, const bool fromEnemy, const std::pair<int, int> &positionInitial, const unsigned int speed, const std::pair<int, int> &direction, const int damage)
28 : EntityNode(EntityId), _sprite(sprite), _fromEnemy(fromEnemy), _speed(speed), _direction(direction), _positionInitial(positionInitial), _damage(damage)
29{
30 _createBullet(positionInitial);
31};
32
38
39GUI::ECS::Online::Bullet::Bullet(const std::uint32_t EntityId, const GUI::ECS::Online::Bullet &bullet)
40 : EntityNode(EntityId)
41{
42 update(bullet);
43};
44
45
47{
48 _visible = visible;
49 _sprite.setVisible(visible);
50}
51
52void GUI::ECS::Online::Bullet::setPosition(const std::pair<float, float> &pos)
53{
54 _collision.setPosition(pos);
55 _sprite.setPosition(pos);
56};
57
59{
60 _sprite.update(sprite);
61}
62
64{
65 _fromEnemy = enemy;
66}
67
68void GUI::ECS::Online::Bullet::setSpeed(const unsigned int speed)
69{
70 _speed = speed;
71}
72
73void GUI::ECS::Online::Bullet::setDirection(const std::pair<int, int> &direction)
74{
75 _direction = direction;
76}
77
78void GUI::ECS::Online::Bullet::setSize(const std::pair<float, float> &dimension)
79{
80 _collision.setDimension(dimension);
81 _sprite.setDimension(dimension);
82}
83
85{
86 _damage = damage;
87}
88
90{
91 PRETTY_DEBUG << "In the bullet tick function" << std::endl;
92 if (!_visible) {
93 PRETTY_DEBUG << "The bullet isn't visible" << std::endl;
94 return;
95 }
96 PRETTY_DEBUG << "Checking sprite tick" << std::endl;
97 _sprite.checkTick();
98 _sprite.forceTick();
99 PRETTY_DEBUG << "Sprite has ticked: " << Recoded::myToString(_sprite.hasTicked()) << std::endl;
100 PRETTY_DEBUG << "Calculating the new position" << std::endl;
101 PRETTY_DEBUG << "Current collision: " << _collision.getPosition() << ", direction: " << _direction << ", speed :" << Recoded::myToString(_speed) << std::endl;
102 const float offsetX = (_direction.first * static_cast<int>(_speed));
103 const float offsetY = (_direction.second * static_cast<int>(_speed));
104 PRETTY_DEBUG << "OffsetX = " << Recoded::myToString(offsetX) << ", offsetY = " << Recoded::myToString(offsetY) << std::endl;
105 const float finalPositionX = _collision.getPositionX() + offsetX;
106 const float finalPositionY = _collision.getPositionY() + offsetY;
107 PRETTY_DEBUG << "finalPositionX = " << finalPositionX << ", finalPositionY = " << finalPositionY << std::endl;
108 std::pair<float, float> opt = { finalPositionX,finalPositionY };
109 PRETTY_DEBUG << "Bullet tick: initial position: " << getPosition() << ", updated position: " << opt << std::endl;
110 setPosition(opt);
111 PRETTY_DEBUG << "Position updated" << std::endl;
112}
113
118
120{
121 return _visible;
122}
123
125{
126 return _fromEnemy;
127}
128
129const std::pair<float, float> GUI::ECS::Online::Bullet::getPosition() const
130{
131 return _collision.getPosition();
132}
133
134
135/*
136If needed, check the opposite condition, as if<object> was the target and target the object
137( = check isColliding as target with bullet as parameter)
138*/
140{
141 PRETTY_DEBUG << "Enemy Position: X = " << _collision.getPositionX()
142 << ", Y = " << _collision.getPositionY()
143 << ", Width = " << _collision.getWidth()
144 << ", Height = " << _collision.getHeight() << std::endl;
145
146 PRETTY_DEBUG << "External entity Position: X = " << second.getPositionX()
147 << ", Y = " << second.getPositionY()
148 << ", Width = " << second.getWidth()
149 << ", Height = " << second.getHeight() << std::endl;
150
151
152 PRETTY_DEBUG << "Setting the tolerance for the collision" << std::endl;
153 const float tolerance = 30.0f;
154 PRETTY_DEBUG << "The tolerance is set to: " << Recoded::myToString(tolerance) << std::endl;
155
156 PRETTY_DEBUG << "Calculating the overlap of X" << std::endl;
157 const bool xOverlap = (std::abs(_collision.getPositionX() - second.getPositionX()) <= (tolerance + (_collision.getWidth() + second.getWidth())));
158 PRETTY_DEBUG << "The X overlap was calculated at: " << Recoded::myToString(xOverlap) << std::endl;
159
160 PRETTY_DEBUG << "Calculating the overlap of Y" << std::endl;
161 const bool yOverlap = (std::abs(_collision.getPositionY() - second.getPositionY()) <= (tolerance + (_collision.getHeight() + second.getHeight())));
162 PRETTY_DEBUG << "The Y overlap was calculated at: " << Recoded::myToString(yOverlap) << std::endl;
163
164 PRETTY_DEBUG << "X Overlap: " << Recoded::myToString(xOverlap) << ", Y Overlap: " << Recoded::myToString(yOverlap) << std::endl;
165 PRETTY_DEBUG << "Checking if x and y are overlapping" << std::endl;
166 const bool overlapping = xOverlap && yOverlap;
167 PRETTY_DEBUG << "The result of if x and y are overlapping is: " << Recoded::myToString(overlapping) << std::endl;
168 return overlapping;
169}
170
177{
178 _entityID = copy._entityID;
179 _speed = copy.getSpeed();
180 _sprite = copy.getSprite();
181 _damage = copy.getDamage();
182 _visible = copy.isVisible();
183 _fromEnemy = copy.isEnemy();
184 _direction = copy.getDirection();
185 _collision = copy.getCollision();
186 _positionInitial = copy.getPositionInitial();
187}
188
197{
198 update(copy);
199 return *this;
200}
201
202const unsigned int GUI::ECS::Online::Bullet::getSpeed() const
203{
204 return _speed;
205}
206
208{
209 return _collision;
210}
211
216
217const std::pair<int, int> GUI::ECS::Online::Bullet::getPositionInitial() const
218{
219 return _positionInitial;
220}
221
223{
224 return _damage;
225}
226
227const std::pair<int, int> GUI::ECS::Online::Bullet::getDirection() const
228{
229 return _direction;
230}
231
232const std::string GUI::ECS::Online::Bullet::getInfo(const unsigned int indent) const
233{
234
235 std::string indentation = "";
236 for (unsigned int i = 0; i < indent; ++i) {
237 indentation += "\t";
238 }
239 std::string result = indentation + "Bullet:\n";
240 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
241 result += indentation + "- Damage: '" + Recoded::myToString(_damage) + "'\n";
242 result += indentation + "- Visible: '" + Recoded::myToString(_visible) + "'\n";
243 result += indentation + "- From Enemy: '" + Recoded::myToString(_fromEnemy) + "'\n";
244 result += indentation + "- Speed: '" + Recoded::myToString(_speed) + "'\n";
245 result += indentation + "- Direction: '" + Recoded::myToString(_direction) + "'\n";
246 result += indentation + "- Position Initial: '" + Recoded::myToString(_positionInitial) + "'\n";
247 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
248 result += indentation + "- Sprite: {\n" + _sprite.getInfo(indent + 1) + indentation + "}\n";
249
250 return result;
251}
252
253void GUI::ECS::Online::Bullet::_createBullet(const std::pair<int, int> &positionInitial)
254{
255 _sprite.setPosition(positionInitial);
256 _collision.setPosition(positionInitial);
257 _collision.setDimension(_sprite.getSpritesheet().getCollisionInfo().getDimension());
258}
259
260std::ostream &GUI::ECS::Online::operator<<(std::ostream &os, const GUI::ECS::Online::Bullet &item)
261{
262 os << item.getInfo();
263 return os;
264}
#define PRETTY_DEBUG
Debug log with details and colour.
File in charge of containing the logic for the bullet class.
Represents a drawable and interactive sprite in the ECS system.
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 ...
Represents a bullet entity in the game.
Definition Bullet.hpp:31
void setDirection(const std::pair< int, int > &direction)
Sets the direction of the bullet.
Definition Bullet.cpp:73
const GUI::ECS::Components::SpriteComponent render()
Renders the bullet.
Definition Bullet.cpp:114
void setVisible(const bool visible)
Sets the visibility of the bullet.
Definition Bullet.cpp:46
const std::pair< int, int > getPositionInitial() const
Get the initial position that was set for the object.
Definition Bullet.cpp:217
const int getDamage() const
Get the Damage the bullet will inflict upon impact.
Definition Bullet.cpp:222
const GUI::ECS::Systems::Collision getCollision() const
Get the Collision component.
Definition Bullet.cpp:207
void setSprite(const GUI::ECS::Components::SpriteComponent &sprite)
Sets the sprite representing the bullet.
Definition Bullet.cpp:58
const bool isEnemy() const
Definition Bullet.cpp:124
void setDamage(const int damage)
Sets the damage dealt by the bullet.
Definition Bullet.cpp:84
Bullet & operator=(const GUI::ECS::Online::Bullet &copy)
Overloads the assignment operator to copy from another Bullet.
Definition Bullet.cpp:196
Bullet(const std::uint32_t EntityId=0)
Default constructor for the Bullet class.
Definition Bullet.cpp:16
void setEnemy(const bool enemy)
Marks the bullet as fired by an enemy or not.
Definition Bullet.cpp:63
void setPosition(const std::pair< float, float > &pos)
Sets the position of the bullet.
Definition Bullet.cpp:52
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 ...
Definition Bullet.cpp:232
const GUI::ECS::Components::SpriteComponent getSprite() const
Get the Sprite component.
Definition Bullet.cpp:212
void setSize(const std::pair< float, float > &dimension)
Sets the size of the bullet.
Definition Bullet.cpp:78
const bool isVisible() const
Definition Bullet.cpp:119
const unsigned int getSpeed() const
Get the Speed at which the bullet is traveling.
Definition Bullet.cpp:202
const bool isColliding(const GUI::ECS::Systems::Collision &second) const
Definition Bullet.cpp:139
const std::pair< float, float > getPosition() const
Definition Bullet.cpp:129
const std::pair< int, int > getDirection() const
Get the Direction in which the bullet is travelling.
Definition Bullet.cpp:227
void update(const GUI::ECS::Online::Bullet &copy)
Updates the sprite by copying another Bullet.
Definition Bullet.cpp:176
void setSpeed(const unsigned int speed)
Sets the speed of the bullet.
Definition Bullet.cpp:68
void tick()
Advances the bullet's state by one tick.
Definition Bullet.cpp:89
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