-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHitboxComponent.cpp
80 lines (62 loc) · 2.03 KB
/
HitboxComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "stdafx.h"
#include "HitboxComponent.h"
//Constructors and Destructor:
//Creates a hitbox component. Inisible by default.
HitboxComponent::HitboxComponent(sf::Sprite& sprite, const float offset_x, const float offset_y, const float width, const float height)
: sprite(sprite), offsetX(offset_x), offsetY(offset_y)
{
this->nextPosition.left = 0.f;
this->nextPosition.top = 0.f;
this->nextPosition.width = width;
this->nextPosition.height = height;
this->hitbox.setPosition(this->sprite.getPosition().x + offset_x, this->sprite.getPosition().y + offset_y);
this->hitbox.setSize(sf::Vector2f(width, height));
this->hitbox.setFillColor(sf::Color::Transparent);
this->hitbox.setOutlineThickness(-1.f);
this->hitbox.setOutlineColor(sf::Color::Green);
}
HitboxComponent::~HitboxComponent()
{
}
//Accessors:
const sf::Vector2f& HitboxComponent::getPosition() const
{
return this->hitbox.getPosition();
}
const sf::FloatRect HitboxComponent::getGlobalBounds() const
{
return this->hitbox.getGlobalBounds();
}
const sf::FloatRect& HitboxComponent::getNextPosition(const sf::Vector2f& velocity)
{
this->nextPosition.left = this->hitbox.getPosition().x + velocity.x;
this->nextPosition.top = this->hitbox.getPosition().y + velocity.y;
return this->nextPosition;
}
//Modifiers:
void HitboxComponent::setPosition(const sf::Vector2f& position)
{
this->hitbox.setPosition(position);
this->sprite.setPosition(position.x - this->offsetX, position.y - this->offsetY);
}
void HitboxComponent::setPosition(const float x, const float y)
{
this->hitbox.setPosition(x, y);
this->sprite.setPosition(x - this->offsetX, y - this->offsetY);
}
//Functions:
bool HitboxComponent::intersects(const sf::FloatRect& frect)
{
return this->hitbox.getGlobalBounds().intersects(frect);
}
void HitboxComponent::update()
{
this->hitbox.setPosition(this->sprite.getPosition().x + this->offsetX, this->sprite.getPosition().y + this->offsetY);
}
void HitboxComponent::render(sf::RenderTarget& target, const bool visible)
{
if (visible)
{
target.draw(this->hitbox);
}
}