-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.cpp
101 lines (88 loc) · 2.88 KB
/
Item.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
#include "Item.h"
Item::Item() {
velocity.x = 0;
velocity.y = 0;
}
void Item::CheckCollisions(const sf::FloatRect* arg)
{
FloatRect itemBounds = getGlobalBounds();
FloatRect argBounds = *arg;
nextPosition = itemBounds;
nextPosition.left += velocity.x;
nextPosition.top += velocity.y;
if (argBounds.intersects(nextPosition))
{
// Bottom collision
if (itemBounds.top < argBounds.top
and itemBounds.top + itemBounds.height < argBounds.top + argBounds.height
and itemBounds.left < argBounds.left + argBounds.width
and itemBounds.left + itemBounds.width > argBounds.left)
{
velocity.y = 0;
setPosition(itemBounds.left + getOrigin().x, argBounds.top - itemBounds.height + getOrigin().y);
}
// Top collision
else if (itemBounds.top > argBounds.top
and itemBounds.top + itemBounds.height > argBounds.top + argBounds.height
and itemBounds.left < argBounds.left + argBounds.width
and itemBounds.left + itemBounds.width > argBounds.left)
{
velocity.y = 0;
setPosition(itemBounds.left + getOrigin().x, argBounds.top + argBounds.height + getOrigin().y);
}
// Right collision
if (itemBounds.left < argBounds.left
and itemBounds.left + itemBounds.width < argBounds.left + argBounds.width
and itemBounds.top < argBounds.top + argBounds.height
and itemBounds.top + itemBounds.height > argBounds.top)
{
velocity.x = 0;
setPosition(argBounds.left - itemBounds.width + getOrigin().x, itemBounds.top + getOrigin().y);
}
// Left collision
else if (itemBounds.left > argBounds.left
and itemBounds.left + itemBounds.width > argBounds.left + argBounds.width
and itemBounds.top < argBounds.top + argBounds.height
and itemBounds.top + itemBounds.height > argBounds.top)
{
velocity.x = 0;
setPosition(argBounds.left + argBounds.width + getOrigin().x, itemBounds.top + getOrigin().y);
}
}
}
void Item::goToPlayer(Vector2f playerPosition)
{
velocity.x = (playerPosition.x - getPosition().x)/5;
velocity.y = (playerPosition.y - getPosition().y)/5;
move(velocity);
}
vector<pair<IDs, int>> Item::getItemsRequiredToCraft()
{
return itmsRequiredToCraft;
}
int Item::getCraftedQuantity()
{
return craftedQuantity;
}
int Item::getBlockDamage()
{
return blockDamage;
}
void Item::GravityUpdate(float elapsed, float gravity)
{
if (velocity.y < 6)
{
velocity.y += gravity * elapsed;
}
move(velocity);
}
int Item::getStackingQuantity()
{
return stackingQuantity;
}
bool Item::getIsCraftingTableRequired() {
return isCraftingTableRequired;
}
bool Item::getIsFurnaceRequired() {
return isFurnaceRequired;
}