f7cb7d8da134acdc3fbf9bd653ab623f13d2378b
[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/joystickkeyboardcontroller.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 LevelIntro::LevelIntro(const Level* level, const Statistics* best_level_statistics) :
29   level(level), 
30   best_level_statistics(best_level_statistics), 
31   player_sprite(),
32   player_sprite_py(0), 
33   player_sprite_vy(0),
34   player_sprite_jump_timer()
35 {
36   player_sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
37   player_sprite->set_action("small-walk-right");
38   player_sprite_jump_timer.start(systemRandom.randf(5,10));
39 }
40
41 LevelIntro::~LevelIntro()
42 {
43 }
44
45 void
46 LevelIntro::setup()
47 {
48 }
49
50 void
51 LevelIntro::update(float elapsed_time)
52 {
53
54   // Check if it's time to exit the screen
55   if(g_main_controller->pressed(Controller::JUMP)
56      || g_main_controller->pressed(Controller::ACTION)
57      || g_main_controller->pressed(Controller::MENU_SELECT)
58      || g_main_controller->pressed(Controller::PAUSE_MENU)) {
59     g_screen_manager->exit_screen(new FadeOut(0.1));
60   }
61
62   player_sprite_py += player_sprite_vy * elapsed_time;
63   player_sprite_vy += 1000 * elapsed_time;
64   if (player_sprite_py >= 0) {
65     player_sprite_py = 0;
66     player_sprite_vy = 0;
67   }
68   if (player_sprite_jump_timer.check()) {
69     player_sprite_vy = -300;
70     player_sprite_jump_timer.start(systemRandom.randf(2,3));
71   }
72   
73 }
74
75 void
76 LevelIntro::draw(DrawingContext& context)
77 {
78   const Statistics& stats = level->stats;
79   int py = static_cast<int>(SCREEN_HEIGHT / 2 - Resources::normal_font->get_height() / 2);
80
81   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(0.0f, 0.0f, 0.0f, 1.0f), 0);
82
83   {
84     context.draw_center_text(Resources::normal_font, level->get_name(), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::header_color);
85     py += static_cast<int>(Resources::normal_font->get_height());
86   }
87
88   std::string author = level->get_author();
89   if ((author != "") && (author != "SuperTux Team")) {
90     std::string author_text = std::string(_("contributed by ")) + author;
91     context.draw_center_text(Resources::small_font, author_text, Vector(0, py), LAYER_FOREGROUND1, LevelIntro::author_color);
92     py += static_cast<int>(Resources::small_font->get_height());
93   }
94
95   py += 32;
96
97   {
98     player_sprite->draw(context, Vector((SCREEN_WIDTH - player_sprite->get_current_hitbox_width()) / 2, py + player_sprite_py), LAYER_FOREGROUND1);
99     py += static_cast<int>(player_sprite->get_current_hitbox_height());
100   }
101
102   py += 32;
103
104   {
105     context.draw_center_text(Resources::normal_font, std::string("- ") + _("Best Level Statistics") + std::string(" -"), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::stat_hdr_color);
106     py += static_cast<int>(Resources::normal_font->get_height());
107   }
108
109   {
110     std::stringstream ss;
111     ss << _("Coins") << ": " << Statistics::coins_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->coins : 0, stats.total_coins);
112     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1, LevelIntro::stat_color);
113     py += static_cast<int>(Resources::normal_font->get_height());
114   }
115   
116   {
117     std::stringstream ss;
118     ss << _("Secrets") << ": " << Statistics::secrets_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->secrets : 0, stats.total_secrets);
119     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
120     py += static_cast<int>(Resources::normal_font->get_height());
121   }
122
123   {
124     std::stringstream ss;
125     ss << _("Time") << ": " << Statistics::time_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->time : 0);
126     context.draw_center_text(Resources::normal_font, ss.str(), Vector(0, py), LAYER_FOREGROUND1,LevelIntro::stat_color);
127     py += static_cast<int>(Resources::normal_font->get_height());
128   }
129
130 }
131
132 /* EOF */