-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
130 lines (112 loc) · 3.92 KB
/
object.cpp
File metadata and controls
130 lines (112 loc) · 3.92 KB
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
#include "object.hpp"
namespace tol {
const std::map<std::string, std::string> Object::DESCRIPTIONS{
{ "lemon", "A sour fruit. Can be used to gain 30 health." },
{ "strawberry", "Very sweet and delicious. Also adds 400 experience" },
{ "orange", "Makes a perfect juice. Increases strength by 1." },
{ "melon", "A tremendous watermelon. Eating it recovers 50 health." },
{ "pear", "Looks just like a regular fruit, but think twice before eating. Not everyone can handle the speed. +20 "
"on walking speed" },
{ "cherry", "Probably edible." },
{ "pistol", "Watch out! This kills people. Use it to get a new attack." },
{ "tools", "I should probably bring these back to Detlef. Just drop them in front of his feet." },
{ "baguette", "Perfect for stabbing someone." }
};
const std::string Object::UNDEFINED_DESCRIPTION = "Undefined object.";
const std::map<std::string, std::function<std::optional<std::string>(Protagonist& player)>> Object::EFFECTS{
{ "lemon",
[](auto& player) {
player.stats().health().increase(30);
return player.stats().get();
} },
{ "strawberry",
[](auto& player) {
player.stats().experience().increase(400);
return player.stats().get();
} },
{ "orange",
[](auto& player) {
player.stats().strength().increase(1);
return player.stats().get();
} },
{ "melon",
[](auto& player) {
player.stats().health().increase(50);
return player.stats().get();
} },
{ "pear",
[](auto& player) {
player.stats().speed().increase(20);
return player.stats().get();
} },
{ "pistol",
[](auto& player) {
player.add_attack(Attack("pistol", 25));
return "Pistol available for fight.";
} },
{ "baguette",
[](auto& player) {
player.add_attack(Attack("baguette", 18));
return "Baguette available for fight.";
} }
};
Object::Object(tson::Object& object, tson::Tile& tile, std::shared_ptr<AssetCache> asset_cache):
Tile(
tile,
// Y for tile objects is on bottom, so go up one tile.
// See https://github.com/mapeditor/tiled/issues/91.
{ static_cast<float>(object.getPosition().x), static_cast<float>(object.getPosition().y - tile.getTileSize().y) },
asset_cache),
object(object) {}
bool Object::intersects(const sf::FloatRect& rect) const {
return rect.intersects({ getPosition().x, getPosition().y, static_cast<float>(tile().getTileSize().y),
static_cast<float>(tile().getTileSize().y) });
}
bool Object::collides_with(const sf::FloatRect& rect) const {
auto object_group = tile().getObjectgroup();
for (auto& obj: object_group.getObjects()) {
sf::FloatRect object_rect = {
static_cast<float>(getPosition().x + obj.getPosition().x),
static_cast<float>(getPosition().y + obj.getPosition().y),
static_cast<float>(obj.getSize().x),
static_cast<float>(obj.getSize().y),
};
if (object_rect.intersects(rect)) {
return true;
}
}
return false;
}
const std::string& Object::name() const {
return object.get().getName();
}
const std::string& Object::description() const {
auto found = DESCRIPTIONS.find(name());
if (found != DESCRIPTIONS.end()) {
return found->second;
} else {
return UNDEFINED_DESCRIPTION;
}
}
std::optional<std::function<std::optional<std::string>(Protagonist& player)>> Object::effect() const {
auto found = Object::EFFECTS.find(name());
if (found != Object::EFFECTS.end()) {
return found->second;
}
return std::nullopt;
}
std::optional<float> Object::z_index() const {
auto prop = object.get().getProp("always_on_top");
if (prop && std::any_cast<const bool&>(prop->getValue())) {
return std::numeric_limits<float>::infinity();
}
return getPosition().y + tile().getTileSize().y;
}
bool Object::usable() const {
auto prop = object.get().getProp("usable");
if (prop) {
return std::any_cast<const bool&>(prop->getValue());
}
return true;
}
} // namespace tol