-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hitbox.cpp
75 lines (61 loc) · 1.58 KB
/
Hitbox.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
#include "Hitbox.h"
#include "SFML/Graphics.hpp"
Hitbox::Hitbox(sf::Vector2f size, sf::Vector2f upperLeftOffset) :
mSize(size),
mUpperLeftOffset(upperLeftOffset),
mEntityPosition({-1000, -1000}),
mIsValid(true)
{
}
void Hitbox::setEntityPosition(const sf::Vector2f& newEntityPosition)
{
mEntityPosition = newEntityPosition;
}
bool Hitbox::collidesWith(const Hitbox& other) const
{
if (!mIsValid || !other.mIsValid)
{
return false;
}
const auto thisLeft = this->getLeft();
const auto thisRight = this->getRight();
const auto thisTop = this->getTop();
const auto thisBottom = this->getBottom();
const auto otherRight = other.getRight();
const auto otherLeft = other.getLeft();
const auto otherTop = other.getTop();
const auto otherBottom = other.getBottom();
return (thisLeft < otherRight && thisRight > otherLeft &&
thisTop < otherBottom && thisBottom > otherTop);
}
void Hitbox::invalidate()
{
mIsValid = false;
}
void Hitbox::makeValid()
{
mIsValid = true;
}
float Hitbox::getBottom() const
{
return getTop() + mSize.y;
}
float Hitbox::getTop() const
{
return mEntityPosition.y + mUpperLeftOffset.y;
}
float Hitbox::getLeft() const
{
return mEntityPosition.x + mUpperLeftOffset.x;
}
float Hitbox::getRight() const
{
return getLeft() + mSize.x;
}
void Hitbox::draw(sf::RenderWindow& window) const
{
sf::RectangleShape rectangle(sf::Vector2f(mSize.x, mSize.y));
rectangle.setFillColor(sf::Color(150, 50, 250));
rectangle.setPosition(getLeft(), getTop());
window.draw(rectangle);
}