-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDialogComponent.cpp
113 lines (90 loc) · 2.98 KB
/
DialogComponent.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
/*
Copyright (c) 2017 InversePalindrome
Nihil - DialogComponent.cpp
InversePalindrome.com
*/
#include "DialogComponent.hpp"
#include "FilePaths.hpp"
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <fstream>
#include <streambuf>
DialogComponent::DialogComponent(ResourceManager& resourceManager, float dialogueTime, const std::string& dialogFile,
const std::string& textStyleFile, const std::string& spriteFile, const sf::Vector2f& textOffset, const sf::Vector2f& positionOffset) :
Component("Dialog"),
dialogueTime(dialogueTime),
dialogFile(dialogFile),
textStyleFile(textStyleFile),
spriteFile(spriteFile),
textOffset(textOffset),
visibilityStatus(false),
dialogueCount(0u),
text(resourceManager, "", textStyleFile),
sprite(resourceManager, spriteFile)
{
std::ifstream inFile(Path::dialogues / dialogFile);
dialogue.assign((std::istreambuf_iterator<char>(inFile)),
std::istreambuf_iterator<char>());
boost::split(subDialogues, dialogue, boost::is_any_of("-"));
for (auto& dialo : subDialogues)
{
boost::trim(dialo);
}
text.move(textOffset);
setOffset(positionOffset);
}
std::ostream& operator<<(std::ostream& os, const DialogComponent& component)
{
os << component.getEntityID() << ' ' << component.getName() << ' ' << component.dialogueTime << ' '
<< component.dialogFile << ' ' << component.textStyleFile << ' ' << component.spriteFile << ' '
<< component.textOffset.x << ' ' << component.textOffset.y << ' ' << component.getOffset().x << ' ' << component.getOffset().y;
return os;
}
void DialogComponent::nextDialogue()
{
if (!this->subDialogues.empty() && this->dialogueCount < this->subDialogues.size())
{
this->text.setText(this->subDialogues[this->dialogueCount]);
++this->dialogueCount;
}
else
{
this->dialogueCount = 0u;
this->visibilityStatus = false;
}
}
float DialogComponent::getDialogueTime() const
{
return this->dialogueTime;
}
sf::FloatRect DialogComponent::getGlobalBounds() const
{
return this->sprite.getGlobalBounds();
}
void DialogComponent::setVisibilityStatus(bool visibilityStatus)
{
this->visibilityStatus = visibilityStatus;
this->dialogueCount = 0u;
}
void DialogComponent::setDialogueTime(float dialogueTime)
{
this->dialogueTime = dialogueTime;
}
bool DialogComponent::isVisible() const
{
return this->visibilityStatus;
}
bool DialogComponent::hasDialogueFinished() const
{
return this->dialogueCount == this->subDialogues.size();
}
void DialogComponent::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (this->visibilityStatus)
{
states.transform *= this->getTransform();
target.draw(sprite, states);
target.draw(text, states);
}
}