-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.cpp
146 lines (121 loc) · 3.48 KB
/
GameObject.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "GameObject.h"
GameObject::GameObject()
{
}
void GameObject::Start()
{
}
void GameObject::Update(float const& time)
{
}
void GameObject::Draw(sf::RenderWindow& window) const
{
window.draw(sprite);
/*auto hitboxDebug = sf::RectangleShape(sf::Vector2f(GetHitbox().width, GetHitbox().height));
hitboxDebug.setFillColor(sf::Color(0,255,0,100));
hitboxDebug.setPosition(GetHitbox().left, GetHitbox().top);
window.draw(hitboxDebug);*/
}
sf::FloatRect GameObject::GetHitbox() const
{
auto pos = sf::Vector2f(position.x - sprite.getGlobalBounds().width / 2, position.y - sprite.getGlobalBounds().height / 2);
auto size = sf::Vector2f(sprite.getGlobalBounds().width, sprite.getGlobalBounds().height);
return sf::FloatRect(pos, size);
}
void GameObject::TriggerCollision(GameObject const& other) {
if (isOnGround) {
velocity.y = 0;
}
else {
velocity = sf::Vector2f(0, 0);
}
}
void GameObject::GravityUpdate(float const& time, float const& gravityFactor)
{
if (useGravity && !isOnGround)
velocity.y += gravityFactor;
position += velocity * (speed * time);
}
void GameObject::AnimationUpdate(float time)
{
if (!animations.empty() && !currentAnimation.empty()) {
auto& anim = animations.find(currentAnimation)->second;
sprite.setTextureRect(anim.textureRects.at(currentAnimationIndex));
anim.totalTime += time;
if (anim.totalTime >= anim.switchTime) {
anim.totalTime -= anim.switchTime;
currentAnimationIndex = (currentAnimationIndex + 1) % anim.textureRects.size();
}
}
UpdateSprite();
}
void GameObject::SetCurrentAnimation(std::string_view const & animationName)
{
auto iter = animations.find(animationName.data());
if (iter != animations.end() && animationName != currentAnimation) {
currentAnimation = animationName;
currentAnimationIndex = 0;
}
}
void GameObject::SetAnimation(std::string_view const& animationName, float switchTime, int rowIndex, int nbrOfFrame, int spriteWidth, int spriteHeight)
{
auto _textureRects = std::vector<sf::IntRect>{};
for (auto i{ 0 }; i < nbrOfFrame; i++)
{
_textureRects.push_back(sf::IntRect{ i*spriteWidth, rowIndex*spriteHeight, spriteWidth, spriteHeight });
}
animations[animationName] = Animation(animationName, _textureRects, switchTime);
}
void GameObject::SetTexture(sf::Texture const& p_texture)
{
sprite.setTexture(p_texture);
UpdateSprite();
}
void GameObject::OnCollision(GameObject const& other)
{
}
void GameObject::OnTrigger(GameObject const& other)
{
}
void GameObject::UpdateSprite()
{
sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2);
sprite.setPosition(position.x, position.y);
}
bool GameObject::CheckCollision(GameObject const & other)
{
auto futurePosition = sf::FloatRect(sf::Vector2f(GetHitbox().left, GetHitbox().top) + (velocity), sf::Vector2f(GetHitbox().width, GetHitbox().height));
auto intersect = bool{ futurePosition.intersects(other.GetHitbox()) };
isOnGround = false;
if (intersect) {
isOnGround = (GetHitbox().top + GetHitbox().height) - other.GetHitbox().top < 0.5f;
if (isColliding && other.isColliding) {
OnCollision(other);
}
else if (isTrigger) {
OnTrigger(other);
}
return true;
}
return false;
}
bool GameObject::GetIsColliding()
{
return isColliding;
}
bool GameObject::GetIsTrigger()
{
return isTrigger;
}
void GameObject::setIsTrigger(bool const & value)
{
isTrigger = value;
if (isTrigger)
isColliding = false;
}
void GameObject::setIsColliding(bool const & value)
{
isColliding = value;
if (isColliding)
isTrigger = false;
}