R-Type  2
Doom but in better
Loading...
Searching...
No Matches
SpriteComponent.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** SpriteComponent.cpp
6*/
7
15
17 :EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
18 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
19{
20};
21
23 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
24 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
25{
26 setName(name);
27};
28
30 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
31 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
32{
33 setCollision(collision);
34 _processSprite();
35};
36
38 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
39 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
40{
41 setAnimation(animation);
42 _processSprite();
43};
44
46 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
47 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
48{
49 setSpritesheet(spritesheetTexture);
50 _processSprite();
51};
52
53GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::string &name, const std::string &spritesheetPath)
54 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
55 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
56{
57 setName(name);
58 setSpritesheet(spritesheetPath);
59 _processSprite();
60};
61
63 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
64 _hoverColor(0), _normalColor(0), _clickedColor(0)
65{
66 setName(name);
67 setCollision(collision);
68 _processSprite();
69};
70
72 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
73 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
74{
75 setName(name);
76 setAnimation(animation);
77 _processSprite();
78};
79
81 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
82 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
83{
84 setName(name);
85 setSpritesheet(spritesheetTexture);
86 _processSprite();
87};
88
90 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
91 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
92{
93 setName(name);
94 setCollision(collision);
95 setAnimation(animation);
96 _processSprite();
97};
98
100 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
101 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
102{
103 setName(name);
104 setCollision(collision);
105 setSpritesheet(spritesheetTexture);
106 _processSprite();
107};
108
110 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
111 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
112{
113 setName(name);
114 setHoverColor(hoverColor);
115 setNormalColor(normalColor);
116 setClickedColor(clickedColor);
117 _processSprite();
118};
119
121 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
122 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
123{
124 setName(name);
125 setCollision(collision);
126 setHoverColor(hoverColor);
127 setNormalColor(normalColor);
128 setClickedColor(clickedColor);
129 _processSprite();
130};
131
133 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
134 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
135{
136 setName(name);
137 setAnimation(animation);
138 setHoverColor(hoverColor);
139 setNormalColor(normalColor);
140 setClickedColor(clickedColor);
141 _processSprite();
142};
143
144GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::string &name, const GUI::ECS::Components::TextureComponent &spritesheetTexture, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
145 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
146 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
147{
148 setName(name);
149 setSpritesheet(spritesheetTexture);
150 setHoverColor(hoverColor);
151 setNormalColor(normalColor);
152 setClickedColor(clickedColor);
153 _processSprite();
154};
155
157 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
158 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
159{
160 setName(name);
161 setCollision(collision);
162 setAnimation(animation);
163 setHoverColor(hoverColor);
164 setNormalColor(normalColor);
165 setClickedColor(clickedColor);
166 _processSprite();
167};
168
169GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Components::TextureComponent &spritesheetTexture, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
170 : EntityNode(0), _spritesheet(0), _collision(0), _animation(0),
171 _hoverColor(0, GUI::ECS::Systems::Colour::White), _normalColor(0, GUI::ECS::Systems::Colour::White), _clickedColor(0, GUI::ECS::Systems::Colour::White)
172{
173 setName(name);
174 setCollision(collision);
175 setSpritesheet(spritesheetTexture);
176 setHoverColor(hoverColor);
177 setNormalColor(normalColor);
178 setClickedColor(clickedColor);
179 _processSprite();
180};
181
182
183
185 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
186 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
187{
188};
189
190GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name)
191 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
192 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
193{
194 setName(name);
195 _processSprite();
196};
197
199 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
200 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
201{
202 setCollision(collision);
203 _processSprite();
204};
205
207 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
208 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
209{
210 setAnimation(animation);
211 _processSprite();
212};
213
215 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
216 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
217{
218 setSpritesheet(spritesheetTexture);
219 _processSprite();
220};
221
222GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const std::string &spritesheetPath)
223 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
224 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
225{
226 setName(name);
227 setSpritesheet(spritesheetPath);
228 _processSprite();
229};
230
231GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision)
232 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
233 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
234{
235 setName(name);
236 setCollision(collision);
237 _processSprite();
238};
239
240GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Components::AnimationComponent &animation)
241 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
242 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
243{
244 PRETTY_INFO << "In sprite constructor taking an entity id, a name and an animation." << std::endl;
245 setName(name);
246 setAnimation(animation);
247 _processSprite();
248};
249
250GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Components::TextureComponent &spritesheetTexture)
251 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
252 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
253{
254 PRETTY_INFO << "In sprite constructor taking an entity id, a name and a spritesheet texture." << std::endl;
255 setName(name);
256 setSpritesheet(spritesheetTexture);
257 _processSprite();
258};
259
260GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Components::AnimationComponent &animation)
261 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
262 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
263{
264 setName(name);
265 setCollision(collision);
266 setAnimation(animation);
267 _processSprite();
268};
269
270GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Components::TextureComponent &spritesheetTexture)
271 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
272 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
273{
274 setName(name);
275 setCollision(collision);
276 setSpritesheet(spritesheetTexture);
277 _processSprite();
278};
279
280GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
281 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
282 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
283{
284 setName(name);
285 setHoverColor(hoverColor);
286 setNormalColor(normalColor);
287 setClickedColor(clickedColor);
288 _processSprite();
289};
290
291GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
292 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
293 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
294{
295 setName(name);
296 setCollision(collision);
297 setHoverColor(hoverColor);
298 setNormalColor(normalColor);
299 setClickedColor(clickedColor);
300 _processSprite();
301};
302
303GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Components::AnimationComponent &animation, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
304 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
305 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
306{
307 setName(name);
308 setAnimation(animation);
309 setHoverColor(hoverColor);
310 setNormalColor(normalColor);
311 setClickedColor(clickedColor);
312 _processSprite();
313};
314
315GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Components::TextureComponent &spritesheetTexture, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
316 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
317 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
318{
319 setName(name);
320 setSpritesheet(spritesheetTexture);
321 setHoverColor(hoverColor);
322 setNormalColor(normalColor);
323 setClickedColor(clickedColor);
324 _processSprite();
325};
326
327GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Components::AnimationComponent &animation, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
328 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
329 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
330{
331 setName(name);
332 setCollision(collision);
333 setAnimation(animation);
334 setHoverColor(hoverColor);
335 setNormalColor(normalColor);
336 setClickedColor(clickedColor);
337 _processSprite();
338};
339
340GUI::ECS::Components::SpriteComponent::SpriteComponent(const std::uint32_t entityId, const std::string &name, const GUI::ECS::Systems::Collision &collision, const GUI::ECS::Components::TextureComponent &spritesheetTexture, const GUI::ECS::Systems::Colour &normalColor, const GUI::ECS::Systems::Colour &hoverColor, const GUI::ECS::Systems::Colour &clickedColor)
341 : EntityNode(entityId), _spritesheet(entityId), _collision(entityId), _animation(entityId),
342 _hoverColor(entityId, GUI::ECS::Systems::Colour::White), _normalColor(entityId, GUI::ECS::Systems::Colour::White), _clickedColor(entityId, GUI::ECS::Systems::Colour::White)
343{
344 setName(name);
345 setCollision(collision);
346 setSpritesheet(spritesheetTexture);
347 setHoverColor(hoverColor);
348 setNormalColor(normalColor);
349 setClickedColor(clickedColor);
350 _processSprite();
351};
352
353
355
357{
358 PRETTY_INFO << "Setting sprite name to '" << name << "'" << std::endl;
359 _spriteName = name;
360 PRETTY_SUCCESS << "Sprite name set to '" << name << "'" << std::endl;
361};
362
364{
365 _application = application;
366}
367
369{
370 _collision.update(copy);
371 _processCollision();
372 _collisionSet = true;
373};
374
375void GUI::ECS::Components::SpriteComponent::setSpritesheet(const std::string &spritesheetPath)
376{
377 _spritesheet.setFilePath(spritesheetPath);
378 std::any textureCapsule = _spritesheet.getTexture();
379 if (!textureCapsule.has_value()) {
380 PRETTY_ERROR << "There is no texture to manage" << std::endl;
381 return;
382 }
383 _spritesheetSet = true;
384 setSpritesheet(_spritesheet);
385}
386
388{
389 _normalColor = color;
390}
391
393{
394 _hoverColor = color;
395}
396
398{
399 _clickedColor = color;
400}
401
403{
404 _spritesheet.update(spritesheetTexture);
405 _spritesheetSet = true;
406 if (!_sfSprite.has_value()) {
407 PRETTY_ERROR << "There is no sprite instance" << std::endl;
408 return;
409 }
410 std::any textureCapsule = _spritesheet.getTexture();
411 if (!textureCapsule.has_value()) {
412 PRETTY_ERROR << "There is not texture instance" << std::endl;
413 return;
414 }
415 std::shared_ptr<sf::Texture> texture = std::any_cast<std::shared_ptr<sf::Texture>>(textureCapsule);
416 _sfSprite->setTexture(*texture);
417 _spriteSet = true;
418}
419
421{
422 PRETTY_INFO << "Setting sprite animation." << std::endl;
423 PRETTY_INFO << "Updating the _animation node with the child version." << std::endl;
424 _animation.update(animation);
425 PRETTY_SUCCESS << "Animation updated" << std::endl;
426 PRETTY_INFO << "Updating the collision node with the child version. '" << _spriteName << "'" << std::endl;
427 _collision.setDimension(animation.getFrameDimensions());
428 PRETTY_SUCCESS << "Collision updated for sprite '" << _spriteName << "'" << std::endl;
429 _animationSet = true;
430 _collisionSet = true;
431 setSpritesheet(animation.getBaseTexture());
432 PRETTY_SUCCESS << "Spritesheet set" << std::endl;
433}
434
436{
437 _visible = visible;
438}
439
440void GUI::ECS::Components::SpriteComponent::setPosition(const std::pair<int, int> &pos)
441{
442 _collision.setPosition(pos);
443 _processCollision();
444}
445
446void GUI::ECS::Components::SpriteComponent::setDimension(const std::pair<float, float> &dim)
447{
448 _collision.setDimension(dim);
449 _processCollision();
450}
451
453{
454 if (_visible) {
455 _visible = false;
456 } else {
457 _visible = true;
458 }
459}
460
462{
463 if (_collisionSet) {
464 _collision.update(mouse);
465 _processCollision();
466 } else {
468 }
469}
470
472{
473 setAnimation(animation);
474}
475
477{
478 _spriteName = copy.getName();
479 setSpritesheet(copy.getSpritesheet());
480 setCollision(copy.getCollision());
481 setAnimation(copy.getAnimation());
482}
483
485{
486 if (!_visible || !_sfSprite.has_value()) {
487 PRETTY_DEBUG << "Instance is hidden or no sfImage instance found, not rendering" << std::endl;
488 return std::nullopt;
489 }
490 return std::make_any<sf::Sprite>(_sfSprite.value());
491};
492
494{
495 _animation.start();
496}
497
499{
500 _animation.pause();
501}
502
504{
505 _animation.resume();
506}
507
509{
510 _animation.stop();
511}
512
514{
515 return _collisionSet;
516}
517
519{
520 return _animationSet;
521}
522
524{
525 return _spritesheetSet;
526}
527
529{
530 return _spriteSet;
531}
532
534{
535 return _visible;
536}
537
539{
540 return _animation.isLooping();
541}
542
544{
545 return _animation.isPaused();
546}
547
549{
550 return _animation.isPlaying();
551}
552
554{
555 return _animation.isStopped();
556}
557
559{
560 return _animation.hasTicked();
561}
562
564{
565 return _animation.hasLooped();
566}
567
569{
570 PRETTY_DEBUG << "Checking sprite tick" << std::endl;
571 _spriteSet = false;
572 if (!_animationSet) {
573 PRETTY_ERROR << "There are no animations set, skipping tick" << std::endl;
574 return;
575 }
576 PRETTY_INFO << "Running animation.checkTick()" << std::endl;
577 _animation.checkTick();
578 PRETTY_INFO << "Animation checkTick ran, checking if frames need to be updated." << std::endl;
579 if (!_animation.hasTicked()) {
580 PRETTY_WARNING << "No tick has occurred yet, nothing to do, exiting function" << std::endl;
581 return;
582 }
583 PRETTY_DEBUG << "Animation has ticked, Getting the updated rectangle." << std::endl;
584 Recoded::IntRect RectComponent = _animation.getCurrentRectangle();
585 PRETTY_DEBUG << "Got the updated rectangle, updating sprite." << std::endl;
586 if (!_sfSprite.has_value()) {
587 PRETTY_WARNING << "There is no sprite value, initialising sprite." << std::endl;
588 _initialiseSprite();
589 PRETTY_SUCCESS << "Sprite initialised." << std::endl;
590 }
591 PRETTY_DEBUG << "Converting internal rectangle to sf::IntRect." << std::endl;
592 sf::IntRect transporter = {
593 {
594 RectComponent.position.first,
595 RectComponent.position.second
596 },
597 {
598 RectComponent.size.first,
599 RectComponent.size.second
600 }
601 };
602 PRETTY_DEBUG << "sf::IntRect initialised." << std::endl;
603 _sfSprite->setTextureRect(transporter);
604 PRETTY_DEBUG << "Texture rect set." << std::endl;
605 _spriteSet = true;
606}
607
609{
610 PRETTY_DEBUG << "Checking sprite tick" << std::endl;
611 _spriteSet = false;
612 if (!_animationSet) {
613 PRETTY_ERROR << "There are no animations set, skipping tick" << std::endl;
614 return;
615 }
616 PRETTY_INFO << "Running animation.forceTick()" << std::endl;
617 _animation.forceTick();
618 PRETTY_INFO << "Animation forceTick ran, updating the frame." << std::endl;
619 PRETTY_DEBUG << "Animation has ticked, Getting the updated rectangle." << std::endl;
620 Recoded::IntRect RectComponent = _animation.getCurrentRectangle();
621 PRETTY_DEBUG << "Got the updated rectangle, updating sprite." << std::endl;
622 if (!_sfSprite.has_value()) {
623 PRETTY_WARNING << "There is no sprite value, initialising sprite." << std::endl;
624 _initialiseSprite();
625 PRETTY_SUCCESS << "Sprite initialised." << std::endl;
626 }
627 PRETTY_DEBUG << "Converting internal rectangle to sf::IntRect." << std::endl;
628 sf::IntRect transporter = {
629 {
630 RectComponent.position.first,
631 RectComponent.position.second
632 },
633 {
634 RectComponent.size.first,
635 RectComponent.size.second
636 }
637 };
638 PRETTY_DEBUG << "sf::IntRect initialised." << std::endl;
639 _sfSprite->setTextureRect(transporter);
640 PRETTY_DEBUG << "Texture rect set." << std::endl;
641 _spriteSet = true;
642}
643
645{
646 return _visible;
647}
648
650{
651 return _spriteName;
652}
653
655{
656 return _application;
657}
658
660{
661 if (!_spritesheetSet) {
663 }
664 return _spritesheet;
665}
666
668{
669 if (!_collisionSet) {
671 }
672 return _collision;
673}
674
676{
677 if (!_animationSet) {
679 }
680 return _animation;
681}
682
687
692
697
698
699const std::string GUI::ECS::Components::SpriteComponent::getInfo(const unsigned int indent) const
700{
701
702 std::string indentation = "";
703 for (unsigned int i = 0; i < indent; ++i) {
704 indentation += "\t";
705 }
706 std::string result = indentation + "Sprite:\n";
707 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
708 result += indentation + "- Visible: " + Recoded::myToString(_visible) + "\n";
709 result += indentation + "- Name: '" + _spriteName + "'\n";
710 result += indentation + "- Application: '" + _application + "'\n";
711 result += indentation + "- Spritesheet Set: " + Recoded::myToString(_spritesheetSet) + "\n";
712 result += indentation + "- Spritesheet: {\n" + _spritesheet.getInfo(indent + 1) + indentation + "}\n";
713 result += indentation + "- Collision Set: " + Recoded::myToString(_collisionSet) + "\n";
714 result += indentation + "- Collision: {\n" + _collision.getInfo(indent + 1) + indentation + "}\n";
715 result += indentation + "- Animation Set: " + Recoded::myToString(_animationSet) + "\n";
716 result += indentation + "- Animation: {\n" + _animation.getInfo(indent + 1) + indentation + "}\n";
717 result += indentation + "- Spritesheet Set: " + Recoded::myToString(_spriteSet) + "\n";
718 result += indentation + "- (sfSprite) Has sprite: " + Recoded::myToString(_sfSprite.has_value()) + "\n";
719 result += indentation + "- Hover Color: {\n" + _hoverColor.getInfo(indent + 1) + indentation + "}\n";
720 result += indentation + "- Normal Color: {\n" + _normalColor.getInfo(indent + 1) + indentation + "}\n";
721 result += indentation + "- Clicked Color: {\n" + _clickedColor.getInfo(indent + 1) + indentation + "}\n";
722 return result;
723}
724
726{
727 if (this != &copy) {
728 update(copy);
729 }
730 return *this;
731};
732
733void GUI::ECS::Components::SpriteComponent::_initialiseSprite()
734{
735 if (_sfSprite.has_value()) {
736 return;
737 }
738 std::any textureCapsule = _spritesheet.getTexture();
739 if (textureCapsule.has_value()) {
740 std::optional<std::shared_ptr<sf::Texture>> texture = std::any_cast<std::shared_ptr<sf::Texture>>(textureCapsule);
741 if (!texture.has_value()) {
742 PRETTY_ERROR << "No Texture found" << std::endl;
743 return;
744 }
745 _sfSprite.emplace(*(texture.value()));
746 }
747}
748
749void GUI::ECS::Components::SpriteComponent::_processSpriteColor()
750{
751 if (!_sfSprite.has_value()) {
752 PRETTY_ERROR << "Sprite instance not set" << std::endl;
753 return;
754 }
755 if (!_collisionSet) {
756 PRETTY_ERROR << "Collision component not set" << std::endl;
757 return;
758 }
759 std::any systemColour;
760 if (_collision.isClicked()) {
761 PRETTY_DEBUG << "The colour being applied is : {\n"
762 << _clickedColor << "\n} and corresponds to the _clickedColour"
763 << std::endl;
764 systemColour = _clickedColor.getRenderColour();
765 } else if (_collision.isHovered()) {
766 PRETTY_DEBUG << "The colour being applied is : {\n"
767 << _hoverColor << "\n} and corresponds to the _hoverColour"
768 << std::endl;
769 systemColour = _hoverColor.getRenderColour();
770 } else {
771 PRETTY_DEBUG << "The colour being applied is : {\n"
772 << _normalColor << "\n} and corresponds to the _normalColour"
773 << std::endl;
774 systemColour = _normalColor.getRenderColour();
775 }
776 const std::string errMsg = "<The content returned by the getRenderColour function is not of type sf::Color>, system error: ";
777 const std::optional<sf::Color> color = Utilities::unCast<sf::Color, CustomExceptions::NoColour>(systemColour, true, errMsg);
778 if (!color.has_value()) {
779 PRETTY_CRITICAL << "BaseId: '" << Recoded::myToString(getEntityNodeId()) << "' "
780 << "No Color found, " << errMsg << std::endl;
781 throw CustomExceptions::NoColour(errMsg);
782 }
783 _sfSprite->setColor(color.value());
784 PRETTY_SUCCESS << "Sprite colour has been set" << std::endl;
785}
786
787void GUI::ECS::Components::SpriteComponent::_processCollision()
788{
789 _initialiseSprite();
790 if (!_sfSprite.has_value()) {
791 return;
792 }
793 std::pair<float, float> pos = _collision.getPosition();
794 std::pair<float, float> dim = _collision.getDimension();
795 _sfSprite->setPosition({ pos.first, pos.second });
796 _sfSprite->setScale({ dim.first, dim.second });
797 _processSpriteColor();
798}
799
800void GUI::ECS::Components::SpriteComponent::_processSprite()
801{
802 _initialiseSprite();
803 _processCollision();
804 _processSpriteColor();
805}
806
808{
809 os << item.getInfo();
810 return os;
811}
#define PRETTY_ERROR
Error log with details and colour.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_CRITICAL
Critical log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
#define PRETTY_SUCCESS
Success log with details and colour.
This is the file that contains the class in charge of managing a sprite.
This is the class in charge of informing the user that they tried to access a non-existant animation ...
Definition No.hpp:238
This is the class in charge of informing the user that they tried to access a non-existant collision ...
Definition No.hpp:207
This is the class in charge of informing the user that they tried to access a non-existant colour ins...
Definition No.hpp:388
This is the class in charge of informing the user that they tried to access a non-existant spriteshee...
Definition No.hpp:117
const GUI::ECS::Components::TextureComponent getBaseTexture() const
Get the Base Texture object.
const std::pair< int, int > getFrameDimensions() const
Get the dimension of the first frame under an std::pair<int, int> instance.
Represents a drawable and interactive sprite in the ECS system.
void setNormalColor(const GUI::ECS::Systems::Colour &color)
Set the default colour of the sprite.
const bool isSpriteSet() const
Checks if the sprite is initialized.
const GUI::ECS::Systems::Collision getCollision() const
Gets the collision component associated with the sprite.
void update(const GUI::ECS::Systems::MouseInfo &mouse)
Updates the sprite based on mouse input.
void checkTick()
Processes and updates sprite properties based on tick state of the clock internal to it's animation c...
void setSpritesheet(const std::string &spritesheetPath)
Sets the texture of the sprite using a file path.
void setAnimation(const GUI::ECS::Components::AnimationComponent &animation)
Associates an animation component with the sprite.
const std::string getApplication() const
Gets the application name associated with the sprite.
const bool isAnimationSet() const
Checks if animation data is set.
void setClickedColor(const GUI::ECS::Systems::Colour &color)
Set the Clicked Color of the sprite.
const bool hasTicked()
A function to check if the frame has changed.
const GUI::ECS::Systems::Colour getNormalColor() const
Get the colour of the default state for the class.
void toggleVisibility()
Change the visibility of the sprite, if visible becomes invisible and vise versa.
const GUI::ECS::Systems::Colour getHoverColor() const
Get the colour of the hover state for the class.
void setName(const std::string &name)
Sets the name of the sprite.
const bool isStopped() const
Get the information about the state of the animation (stopped)
SpriteComponent & operator=(const GUI::ECS::Components::SpriteComponent &copy)
Overloads the assignment operator to copy from another SpriteComponent.
void pause()
Pause the playing of the animation but does not reset the index to the default frame.
void setVisible(const bool visible)
Set the visible of the sprite.
const bool getVisible() const
Check if the sprite is set to be rendred or not.
void setApplication(const std::string &application)
Sets the application name (the name is based on the configuration file) associated with the sprite.
void setHoverColor(const GUI::ECS::Systems::Colour &color)
Set the Hover Color of the sprite.
void stop()
A function to stop the animation, and reset the index to the default frame.
const bool isSpritesheetSet() const
Checks if a spritesheet texture is set.
const std::string getName() const
Gets the name of the sprite.
void setCollision(const GUI::ECS::Systems::Collision &copy)
Associates a collision component with the sprite.
void setDimension(const std::pair< float, float > &dim)
Set the dimensions of the sprite.
const GUI::ECS::Systems::Colour getClickedColor() const
Get the colour of the clicked state for the class.
std::any render() const
Renders the sprite to a window.
void setPosition(const std::pair< int, int > &pos)
Set the Position of the sprite.
const bool isLooping() const
Get the information about if the component is set to loop the animation.
const bool isCollisionSet() const
Checks if collision data is set.
void start()
Start the playing of the animation from the current index in memory.
void forceTick()
Updates the animation frame regardless of the delay.
const bool isPlaying() const
Get the information about the state of the animation (playing)
const GUI::ECS::Components::AnimationComponent getAnimation() const
Gets the animation component associated with the sprite.
const GUI::ECS::Components::TextureComponent getSpritesheet() const
Gets the texture component associated with the sprite.
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 bool hasLooped() const
A function to check if the animation has looped around (valid for the 1rst frame of the new loop)
void resume()
Resume the playing of the animation (has no effect if already playing)
const bool isPaused() const
Get the information about the state of the animation (paused)
const bool isVisible() const
Check if the sprite is set to be rendred or not.
Represents a texture component used in an entity component system.
Represents a rectangular component that can detect collisions and mouse interactions,...
Definition Collision.hpp:39
A class for representing and manipulating colors using RGBA components. Inherits from EntityNode to a...
Definition Colour.hpp:37
void update(const Colour &copy)
Updates the color by copying from another Colour instance.
Definition Colour.cpp:171
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 Colour.cpp:226
A generic 2D rectangle class that holds position and size as pairs.
Definition Rect.hpp:38
std::pair< T, T > size
The size of the rectangle (width, height)
Definition Rect.hpp:98
std::pair< T, T > position
the position (top and left corner)
Definition Rect.hpp:97
std::ostream & operator<<(std::ostream &os, const AnimationComponent &item)
Outputs the animation's info to a stream.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223
std::optional< T > unCast(const std::any &classNode, const bool raiseOnError=true, const std::string customErrorMessage="")
Casts the content of a std::any back to its original type.
Definition UnCast.hpp:65