Skip to content

Commit b2c4633

Browse files
committed
Add ability to show object portrait during convo
1 parent b343821 commit b2c4633

File tree

9 files changed

+64
-21
lines changed

9 files changed

+64
-21
lines changed

sample_game/spiced.chai

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ var game_creator = fun(game) {
384384

385385
fairview.set_collision_action("Deborah", collision_action);
386386
fairview.set_action_generator("Deborah", Deborah_actions);
387+
fairview.set_portrait("Deborah", "resources/Deborah-portrait.png");
387388

388389
game.add_map("fairview", fairview);
389390

src/chaiscript_bindings.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ namespace spiced {
3030
ADD_FUN(Game, add_start_action);
3131
ADD_FUN(Game, add_queued_action);
3232
ADD_FUN(Game, show_message_box);
33+
module->add(
34+
chaiscript::fun([](Game &t_game, const std::string &t_msg)
35+
{
36+
t_game.show_message_box(t_msg);
37+
}), "show_message_box");
3338
ADD_FUN(Game, show_object_interaction_menu);
3439
ADD_FUN(Game, show_selection_menu);
3540
module->add(
3641
chaiscript::fun([](Game &t_game, const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections)
37-
{
38-
t_game.show_selection_menu(t_game_time, t_simulation_time, t_selections);
39-
}), "show_selection_menu");
42+
{
43+
t_game.show_selection_menu(t_game_time, t_simulation_time, t_selections);
44+
}), "show_selection_menu");
4045
ADD_FUN(Game, show_conversation);
4146
ADD_FUN(Game, has_pending_events);
4247
ADD_FUN(Game, get_current_event);
@@ -123,6 +128,7 @@ namespace spiced {
123128
ADD_FUN(Tile_Map, update);
124129
ADD_FUN(Tile_Map, set_collision_action);
125130
ADD_FUN(Tile_Map, set_action_generator);
131+
ADD_FUN(Tile_Map, set_portrait);
126132

127133
module->add(chaiscript::type_conversion<std::string, sf::String>());
128134

src/game.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ namespace spiced {
9494
m_game_events.emplace_back(new Queued_Action(t_action));
9595
}
9696

97-
void Game::show_message_box(const sf::String &t_msg)
97+
void Game::show_message_box(const sf::String &t_msg, const sf::Texture *t_texture)
9898
{
99-
m_game_events.emplace_back(new Message_Box(t_msg, get_font("resources/FreeMonoBold.ttf"), 17, sf::Color(255, 255, 255, 255), sf::Color(0, 0, 0, 128), sf::Color(255, 255, 255, 200), 3, Location::Bottom));
99+
m_game_events.emplace_back(new Message_Box(t_msg, get_font("resources/FreeMonoBold.ttf"), 17, sf::Color(255, 255, 255, 255), sf::Color(0, 0, 0, 128), sf::Color(255, 255, 255, 200), 3, Location::Bottom, t_texture));
100100
}
101101

102102
void Game::show_conversation(const float t_game_time, const float t_simulation_time, Object &t_obj, const Conversation &t_conversation)
@@ -110,7 +110,12 @@ namespace spiced {
110110
[q, t_game_time, t_simulation_time, &t_obj, t_conversation](const float, const float, Game &t_game, Object &obj)
111111
{
112112
for (const auto &answer : q.answers) {
113-
t_game.show_message_box(answer.speaker + ":\n\n" + answer.answer);
113+
const std::string portrait = obj.get_portrait();
114+
const sf::Texture *texture = nullptr;
115+
if (!portrait.empty()) {
116+
texture = &t_game.get_texture(portrait);
117+
}
118+
t_game.show_message_box(answer.speaker + ":\n\n" + answer.answer, texture);
114119
}
115120
if (q.action) {
116121
using namespace std::placeholders;

src/game.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace spiced {
3737

3838
void add_queued_action(const std::function<void(const float t_game_time, const float t_simulation_time, Game &)> &t_action);
3939

40-
void show_message_box(const sf::String &t_msg);
40+
void show_message_box(const sf::String &t_msg, const sf::Texture *t_texture = nullptr);
4141

4242
void show_selection_menu(const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections, const size_t t_selection = 0);
4343
void show_object_interaction_menu(const float t_game_time, const float t_simulation_time, Object &t_obj);

src/game_event.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ namespace spiced {
3232

3333

3434
Message_Box::Message_Box(sf::String t_string, sf::Font t_font, int t_font_size,
35-
sf::Color t_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness, Location t_loc)
35+
sf::Color t_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness, Location t_loc,
36+
const sf::Texture *t_texture)
3637
: Game_Event(),
37-
m_string(std::move(t_string)), m_font(std::move(t_font)), m_font_color(std::move(t_font_color)),
38-
m_fill_color(std::move(t_fill_color)), m_outline_color(std::move(t_outline_color)),
39-
m_outline_thickness(t_outlineThickness),
40-
m_text(t_string, m_font, t_font_size),
41-
m_location(std::move(t_loc))
38+
m_string(std::move(t_string)), m_font(std::move(t_font)), m_font_color(std::move(t_font_color)),
39+
m_fill_color(std::move(t_fill_color)), m_outline_color(std::move(t_outline_color)),
40+
m_outline_thickness(t_outlineThickness),
41+
m_text(t_string, m_font, t_font_size),
42+
m_location(std::move(t_loc)),
43+
m_portrait(t_texture?new sf::Sprite(*t_texture):nullptr)
4244
{
45+
/// \todo un-hardcode this
46+
if (m_portrait) m_portrait->setScale(2,2);
4347
m_text.setColor(m_font_color);
4448
}
4549

@@ -75,6 +79,9 @@ namespace spiced {
7579

7680
target.draw(rect, states);
7781
target.draw(m_text, states);
82+
if (m_portrait) {
83+
target.draw(*m_portrait);
84+
}
7885
}
7986

8087

src/game_event.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <SFML/Graphics.hpp>
55
#include <SFML/Window.hpp>
66
#include <functional>
7+
#include <memory>
78

89
namespace spiced
910
{
@@ -154,7 +155,7 @@ namespace spiced
154155
{
155156
public:
156157
Message_Box(sf::String t_string, sf::Font t_font, int t_font_size,
157-
sf::Color t_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness, Location t_location);
158+
sf::Color t_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness, Location t_location, const sf::Texture *t_texture);
158159

159160
virtual ~Message_Box() = default;
160161

@@ -174,6 +175,7 @@ namespace spiced
174175
float m_outline_thickness;
175176
sf::Text m_text;
176177
Location m_location;
178+
std::unique_ptr<sf::Sprite> m_portrait;
177179

178180
float m_start_time = 0;
179181
bool m_is_done = false;

src/map.cpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@ namespace spiced {
1919
std::function<void(const float, const float, Game &, Object &, sf::Sprite &)> t_collision_action,
2020
std::function<std::vector<Object_Action>(const float, const float, Game &, Object &)> t_action_generator)
2121
: m_name(std::move(t_name)),
22-
m_tileset(std::move(t_tileset)),
23-
m_tile_id(t_tile_id),
24-
m_visible(t_visible),
25-
m_collision_action(std::move(t_collision_action)),
26-
m_action_generator(std::move(t_action_generator))
22+
m_tileset(std::move(t_tileset)),
23+
m_tile_id(t_tile_id),
24+
m_visible(t_visible),
25+
m_collision_action(std::move(t_collision_action)),
26+
m_action_generator(std::move(t_action_generator))
2727
{
2828
setTexture(m_tileset.texture.get());
2929
setTextureRect(m_tileset.get_rect(m_tile_id, 0));
3030
}
3131

32+
void Object::set_portrait(const std::string &t_portrait)
33+
{
34+
m_portrait = t_portrait;
35+
}
36+
37+
std::string Object::get_portrait() const
38+
{
39+
return m_portrait;
40+
}
3241

3342
void Object::update(const float t_game_time, const float /*t_simulation_time*/, Game &t_game)
3443
{
@@ -516,12 +525,19 @@ namespace spiced {
516525
void Tile_Map::set_collision_action(const std::string &t_obj_name,
517526
std::function<void(const float, const float, Game &, Object &, sf::Sprite &)> t_collision_action)
518527
{
519-
520528
const auto obj = std::find_if(m_objects.begin(), m_objects.end(), [&](const Object &t_obj) { return t_obj.name() == t_obj_name; });
521529
if (obj == m_objects.end()) throw std::logic_error("Attempt to set collision action on non-existent object: " + t_obj_name);
522530
obj->set_collision_action(t_collision_action);
523531
}
524532

533+
534+
void Tile_Map::set_portrait(const std::string &t_obj_name, const std::string &t_portrait_path)
535+
{
536+
const auto obj = std::find_if(m_objects.begin(), m_objects.end(), [&](const Object &t_obj) { return t_obj.name() == t_obj_name; });
537+
if (obj == m_objects.end()) throw std::logic_error("Attempt to set portrait path on non-existent object: " + t_obj_name);
538+
obj->set_portrait(t_portrait_path);
539+
}
540+
525541
void Tile_Map::set_action_generator(const std::string &t_obj_name,
526542
std::function<std::vector<Object_Action>(const float, const float, Game &, Object &)> t_action_generator)
527543
{
@@ -741,4 +757,4 @@ namespace spiced {
741757

742758
return verts;
743759
}
744-
}
760+
}

src/map.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ namespace spiced
8989

9090
std::string name() const;
9191

92+
void set_portrait(const std::string &t_portrait);
93+
std::string get_portrait() const;
94+
9295
private:
9396
std::string m_name;
97+
std::string m_portrait;
9498
Tileset m_tileset;
9599
int m_tile_id;
96100
bool m_visible;
@@ -198,6 +202,8 @@ namespace spiced
198202
void set_action_generator(const std::string &t_obj_name,
199203
std::function<std::vector<Object_Action>(const float, const float, Game &, Object &)> t_action_generator);
200204

205+
void set_portrait(const std::string &t_obj_name, const std::string &t_portrait_path);
206+
201207

202208
static sf::FloatRect get_bounding_box(const sf::Sprite &t_s, const sf::Vector2f &t_distance);
203209

0 commit comments

Comments
 (0)