f4b77acbfd2149f022133e6cc3eec0100d5a2ea8
[supertux.git] / src / supertux / levelintro.cpp
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_generator.hpp"
21 #include "sprite/sprite_manager.hpp"
22 #include "supertux/fadeout.hpp"
23 #include "supertux/globals.hpp"
24 #include "supertux/screen_manager.hpp"
25 #include "supertux/resources.hpp"
26 #include "util/gettext.hpp"
27
28 #include <sstream>
29 #include <boost/format.hpp>
30
31 LevelIntro::LevelIntro(const Level* level_, const Statistics* best_level_statistics_) :
32   level(level_),
33   best_level_statistics(best_level_statistics_),
34   player_sprite(),
35   player_sprite_py(0),
36   player_sprite_vy(0),
37   player_sprite_jump_timer()
38 {
39   player_sprite = SpriteManager::current()->create("images/creatures/tux/tux.sprite");
40   player_sprite->set_action("small-walk-right");
41   player_sprite_jump_timer.start(graphicsRandom.randf(5,10));
42 }
43
44 LevelIntro::~LevelIntro()
45 {
46 }
47
48 void
49 LevelIntro::setup()
50 {
51 }
52
53 void
54 LevelIntro::update(float elapsed_time)
55 {
56   Controller *controller = InputManager::current()->get_controller();
57
58   // Check if it's time to exit the screen
59   if(controller->pressed(Controller::JUMP)
60      || controller->pressed(Controller::ACTION)
61      || controller->pressed(Controller::MENU_SELECT)
62      || controller->pressed(Controller::START)
63      || controller->pressed(Controller::ESCAPE)) {
64     ScreenManager::current()->pop_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.1)));
65   }
66
67   player_sprite_py += player_sprite_vy * elapsed_time;
68   player_sprite_vy += 1000 * elapsed_time;
69   if (player_sprite_py >= 0) {
70     player_sprite_py = 0;
71     player_sprite_vy = 0;
72   }
73   if (player_sprite_jump_timer.check()) {
74     player_sprite_vy = -300;
75     player_sprite_jump_timer.start(graphicsRandom.randf(2,3));
76   }
77
78 }
79
80 void
81 LevelIntro::draw(DrawingContext& context)
82 {
83   const Statistics& stats = level->stats;
84   int py = static_cast<int>(SCREEN_HEIGHT / 2 - Resources::normal_font->get_height() / 2);
85
86   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(0.0f, 0.0f, 0.0f, 1.0f), 0);
87
88   {
89     context.draw_center_text(Resources::normal_font, level->get_name(), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::header_color);
90     py += static_cast<int>(Resources::normal_font->get_height());
91   }
92
93   std::string author = level->get_author();
94   if ((author != "") && (author != "SuperTux Team")) {
95     std::string author_text = str(boost::format(_("contributed by %s")) % author);
96     context.draw_center_text(Resources::small_font, author_text, Vector(0, py), LAYER_FOREGROUND1, LevelIntro::author_color);
97     py += static_cast<int>(Resources::small_font->get_height());
98   }
99
100   py += 32;
101
102   {
103     player_sprite->draw(context, Vector((SCREEN_WIDTH - player_sprite->get_current_hitbox_width()) / 2, py + player_sprite_py), LAYER_FOREGROUND1);
104     py += static_cast<int>(player_sprite->get_current_hitbox_height());
105   }
106
107   py += 32;
108
109   {
110     context.draw_center_text(Resources::normal_font, std::string("- ") + _("Best Level Statistics") + std::string(" -"), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::stat_hdr_color);
111     py += static_cast<int>(Resources::normal_font->get_height());
112   }
113
114   {
115     std::stringstream ss;
116     ss << _("Coins") << ": " << Statistics::coins_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->coins : 0, stats.total_coins);
117     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::stat_color);
118     py += static_cast<int>(Resources::normal_font->get_height());
119   }
120         
121   {
122     std::stringstream ss;
123     ss << _("Badguys killed") << ": " << Statistics::frags_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->badguys : 0, stats.total_badguys);
124     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
125     py += static_cast<int>(Resources::normal_font->get_height());
126   }
127
128   {
129     std::stringstream ss;
130     ss << _("Secrets") << ": " << Statistics::secrets_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->secrets : 0, stats.total_secrets);
131     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
132     py += static_cast<int>(Resources::normal_font->get_height());
133   }
134
135   {
136     std::stringstream ss;
137     ss << _("Best time") << ": " << Statistics::time_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->time : 0);
138     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
139     py += static_cast<int>(Resources::normal_font->get_height());
140   }
141
142   if(level->target_time){
143     std::stringstream ss;
144     ss << _("Level target time") << ": " << Statistics::time_to_string(level->target_time);
145     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
146     py += static_cast<int>(Resources::normal_font->get_height());
147   }
148
149 }
150
151 /* EOF */