1// SuperTux -- LevelIntro screen
2// Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.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 "supertux/levelintro.hpp"
18
19#include "control/input_manager.hpp"
20#include "math/random.hpp"
21#include "sprite/sprite.hpp"
22#include "sprite/sprite_manager.hpp"
23#include "supertux/fadetoblack.hpp"
24#include "supertux/gameconfig.hpp"
25#include "supertux/level.hpp"
26#include "supertux/player_status.hpp"
27#include "supertux/resources.hpp"
28#include "supertux/screen_manager.hpp"
29#include "supertux/sector.hpp"
30#include "util/gettext.hpp"
31#include "video/compositor.hpp"
32
33#include <boost/format.hpp>
34
35LevelIntro::LevelIntro(const Level& level, const Statistics* best_level_statistics, const PlayerStatus& player_status) :
36 m_level(level),
37 m_best_level_statistics(best_level_statistics),
38 m_player_sprite(SpriteManager::current()->create("images/creatures/tux/tux.sprite")),
39 m_power_sprite(SpriteManager::current()->create("images/creatures/tux/powerups.sprite")),
40 m_player_sprite_py(0),
41 m_player_sprite_vy(0),
42 m_player_sprite_jump_timer(),
43 m_player_status(player_status)
44{
45 //Show appropriate tux animation for player status.
46 if (m_player_status.bonus == FIRE_BONUS && g_config->christmas_mode)
47 {
48 m_player_sprite->set_action("big-walk-right");
49 m_power_sprite->set_action("santa-walk-right");
50 }
51 else
52 {
53 m_player_sprite->set_action(m_player_status.get_bonus_prefix() + "-walk-right");
54 }
55 m_player_sprite_jump_timer.start(graphicsRandom.randf(5,10));
56
57 /* Set Tux powerup sprite action */
58 if (m_player_status.bonus == EARTH_BONUS || m_player_status.bonus == AIR_BONUS)
59 {
60 m_power_sprite->set_action(m_player_sprite->get_action());
61 }
62}
63
64LevelIntro::~LevelIntro()
65{
66}
67
68void
69LevelIntro::setup()
70{
71}
72
73void
74LevelIntro::update(float dt_sec, const Controller& controller)
75{
76 auto bonus_prefix = m_player_status.get_bonus_prefix();
77 if (m_player_status.bonus == FIRE_BONUS && g_config->christmas_mode)
78 {
79 bonus_prefix = "big";
80 }
81
82 // Check if it's time to exit the screen
83 if (controller.pressed(Control::JUMP) ||
84 controller.pressed(Control::ACTION) ||
85 controller.pressed(Control::MENU_SELECT) ||
86 controller.pressed(Control::START) ||
87 controller.pressed(Control::ESCAPE)) {
88 ScreenManager::current()->pop_screen(std::make_unique<FadeToBlack>(FadeToBlack::FADEOUT, 0.1f));
89 }
90
91 m_player_sprite_py += m_player_sprite_vy * dt_sec;
92 m_player_sprite_vy += 100 * dt_sec * Sector::get().get_gravity();
93 if (m_player_sprite_py >= 0) {
94 m_player_sprite_py = 0;
95 m_player_sprite_vy = 0;
96 m_player_sprite->set_action(bonus_prefix + "-walk-right");
97 } else {
98
99 m_player_sprite->set_action(bonus_prefix + "-jump-right");
100 }
101 if (m_player_sprite_jump_timer.check()) {
102 m_player_sprite_vy = -300;
103 m_player_sprite_jump_timer.start(graphicsRandom.randf(2,3));
104 }
105
106}
107
108void LevelIntro::draw_stats_line(DrawingContext& context, int& py, const std::string& name, const std::string& stat)
109{
110 std::stringstream ss;
111 ss << name << ": " << stat;
112 context.color().draw_center_text(Resources::normal_font, ss.str(), Vector(0, static_cast<float>(py)),
113 LAYER_FOREGROUND1, s_stat_color);
114 py += static_cast<int>(Resources::normal_font->get_height());
115}
116
117void
118LevelIntro::draw(Compositor& compositor)
119{
120 auto& context = compositor.make_context();
121
122 const Statistics& stats = m_level.m_stats;
123 int py = static_cast<int>(static_cast<float>(context.get_height()) / 2.0f - Resources::normal_font->get_height() / 2.0f);
124
125 context.set_ambient_color(Color(1.0f, 1.0f, 1.0f, 1.0f));
126 context.color().draw_filled_rect(Rectf(0, 0,
127 static_cast<float>(context.get_width()),
128 static_cast<float>(context.get_height())),
129 Color(0.0f, 0.0f, 0.0f, 1.0f), 0);
130
131 {
132 context.color().draw_center_text(Resources::normal_font, m_level.get_name(), Vector(0, static_cast<float>(py)), LAYER_FOREGROUND1, s_header_color);
133 py += static_cast<int>(Resources::normal_font->get_height());
134 }
135
136 std::string author = m_level.get_author();
137 if ((!author.empty()) && (author != "SuperTux Team")) {
138 std::string author_text = str(boost::format(_("contributed by %s")) % author);
139 context.color().draw_center_text(Resources::small_font, author_text, Vector(0, static_cast<float>(py)), LAYER_FOREGROUND1, s_author_color);
140 py += static_cast<int>(Resources::small_font->get_height());
141 }
142
143 py += 32;
144
145 {
146 m_player_sprite->draw(context.color(), Vector((static_cast<float>(context.get_width()) - m_player_sprite->get_current_hitbox_width()) / 2,
147 static_cast<float>(py) + m_player_sprite_py), LAYER_FOREGROUND1);
148
149 if (m_player_status.bonus == EARTH_BONUS
150 || m_player_status.bonus == AIR_BONUS
151 || (m_player_status.bonus == FIRE_BONUS && g_config->christmas_mode))
152 {
153 m_power_sprite->draw(context.color(), Vector((static_cast<float>(context.get_width()) - m_player_sprite->get_current_hitbox_width()) / 2,
154 static_cast<float>(py) + m_player_sprite_py), LAYER_FOREGROUND1);
155 }
156 py += static_cast<int>(m_player_sprite->get_current_hitbox_height());
157 }
158
159 py += 32;
160
161 if (m_best_level_statistics)
162 {
163 context.color().draw_center_text(Resources::normal_font,
164 std::string("- ") + _("Best Level Statistics") + std::string(" -"),
165 Vector(0, static_cast<float>(py)),
166 LAYER_FOREGROUND1, s_stat_hdr_color);
167
168 py += static_cast<int>(Resources::normal_font->get_height());
169
170 draw_stats_line(context, py, _("Coins"),
171 Statistics::coins_to_string(m_best_level_statistics->m_coins, stats.m_total_coins));
172 draw_stats_line(context, py, _("Badguys killed"),
173 Statistics::frags_to_string(m_best_level_statistics->m_badguys, stats.m_total_badguys));
174 draw_stats_line(context, py, _("Secrets"),
175 Statistics::secrets_to_string(m_best_level_statistics->m_secrets, stats.m_total_secrets));
176 draw_stats_line(context, py, _("Best time"),
177 Statistics::time_to_string(m_best_level_statistics->get_time()));
178
179 if (m_level.m_target_time != 0.0f) {
180 draw_stats_line(context, py, _("Level target time"),
181 Statistics::time_to_string(m_level.m_target_time));
182 }
183 }
184}
185
186/* EOF */
187