1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "worldmap/sprite_change.hpp"
18
19#include "sprite/sprite.hpp"
20#include "sprite/sprite_manager.hpp"
21#include "util/reader_mapping.hpp"
22#include "video/drawing_context.hpp"
23
24namespace worldmap {
25
26std::list<SpriteChange*> SpriteChange::s_all_sprite_changes;
27
28SpriteChange::SpriteChange(const ReaderMapping& mapping) :
29 m_pos(),
30 m_change_on_touch(false),
31 m_sprite(),
32 m_sprite_name(),
33 m_stay_action(),
34 m_stay_group(),
35 m_in_stay_action(false)
36{
37 mapping.get("x", m_pos.x);
38 mapping.get("y", m_pos.y);
39 mapping.get("change-on-touch", m_change_on_touch);
40
41 if (!mapping.get("sprite", m_sprite_name)) m_sprite_name = "";
42 m_sprite = SpriteManager::current()->create(m_sprite_name);
43
44 mapping.get("stay-action", m_stay_action);
45 mapping.get("initial-stay-action", m_in_stay_action);
46
47 mapping.get("stay-group", m_stay_group);
48
49 s_all_sprite_changes.push_back(this);
50}
51
52SpriteChange::~SpriteChange()
53{
54 s_all_sprite_changes.remove(this);
55}
56
57void
58SpriteChange::draw(DrawingContext& context)
59{
60 if (m_in_stay_action && !m_stay_action.empty()) {
61 m_sprite->set_action(m_stay_action);
62 m_sprite->draw(context.color(), m_pos * 32, LAYER_OBJECTS-1);
63 }
64}
65
66void
67SpriteChange::update(float )
68{
69}
70
71bool
72SpriteChange::show_stay_action() const
73{
74 return m_in_stay_action;
75}
76
77void
78SpriteChange::set_stay_action()
79{
80 m_in_stay_action = true;
81}
82
83void
84SpriteChange::clear_stay_action(bool propagate)
85{
86 m_in_stay_action = false;
87
88 // if we are in a stay_group, also clear all stay actions in this group
89 if (!m_stay_group.empty() && propagate) {
90 for (auto& sc : s_all_sprite_changes) {
91 if (sc->m_stay_group != m_stay_group) continue;
92 sc->m_in_stay_action = false;
93 }
94 }
95}
96
97} // namespace worldmap
98
99/* EOF */
100