84f92d317f53ee3dad447f4b62bca0a0c9294bf4
[supertux.git] / src / levelintro.cpp
1 //  $Id$
2 //
3 //  SuperTux -- LevelIntro screen
4 //  Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "levelintro.hpp"
23
24 #include <stdexcept>
25 #include "log.hpp"
26 #include "mainloop.hpp"
27 #include "gettext.hpp"
28 #include "resources.hpp"
29 #include "video/font.hpp"
30 #include "video/drawing_context.hpp"
31 #include "gui/menu.hpp"
32 #include "main.hpp"
33 #include "fadeout.hpp"
34 #include "control/joystickkeyboardcontroller.hpp"
35 #include "sprite/sprite_manager.hpp"
36 #include "random_generator.hpp"
37
38
39 LevelIntro::LevelIntro(const Level* level, const Statistics* best_level_statistics)
40         : level(level), best_level_statistics(best_level_statistics), player_sprite_py(0), player_sprite_vy(0)
41 {
42   player_sprite.reset(sprite_manager->create("images/creatures/tux/tux.sprite"));
43   player_sprite->set_action("small-walk-right");
44   player_sprite_jump_timer.start(systemRandom.randf(5,10));
45 }
46
47 LevelIntro::~LevelIntro()
48 {
49 }
50
51 void
52 LevelIntro::setup()
53 {
54   Menu::set_current(NULL);
55 }
56
57 void
58 LevelIntro::update(float elapsed_time)
59 {
60
61   // Check if it's time to exit the screen
62   if(main_controller->pressed(Controller::JUMP)
63       || main_controller->pressed(Controller::ACTION)
64       || main_controller->pressed(Controller::MENU_SELECT)
65       || main_controller->pressed(Controller::PAUSE_MENU)) {
66     main_loop->exit_screen(new FadeOut(0.1));
67   }
68
69   player_sprite_py += player_sprite_vy * elapsed_time;
70   player_sprite_vy += 1000 * elapsed_time;
71   if (player_sprite_py >= 0) {
72     player_sprite_py = 0;
73     player_sprite_vy = 0;
74   }
75   if (player_sprite_jump_timer.check()) {
76     player_sprite_vy = -300;
77     player_sprite_jump_timer.start(systemRandom.randf(2,3));
78   }
79   
80 }
81
82 void
83 LevelIntro::draw(DrawingContext& context)
84 {
85   const Statistics& stats = level->stats;
86   int py = SCREEN_HEIGHT / 2 - gold_text->get_height() / 2;
87
88   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), Color(0.0f, 0.0f, 0.0f, 1.0f), 0);
89
90   {
91     context.draw_center_text(gold_text, level->get_name(), Vector(0, py), LAYER_FOREGROUND1);
92     py += gold_text->get_height();
93   }
94
95   std::string author = level->get_author();
96   if ((author != "") && (author != "SuperTux Team")) {
97     std::string author_text = std::string(_("contributed by ")) + author;
98     context.draw_center_text(white_small_text, author_text, Vector(0, py), LAYER_FOREGROUND1);
99     py += white_small_text->get_height();
100   }
101
102   py += 32;
103
104   {
105     player_sprite->draw(context, Vector((SCREEN_WIDTH - player_sprite->get_current_hitbox_width()) / 2, py + player_sprite_py), LAYER_FOREGROUND1);
106     py += player_sprite->get_current_hitbox_height();
107   }
108
109   py += 32;
110
111   {
112     context.draw_center_text(blue_text, std::string("- ") + _("Best Level Statistics") + std::string(" -"), Vector(0, py), LAYER_FOREGROUND1);
113     py += blue_text->get_height();
114   }
115
116   {
117     std::stringstream ss;
118     ss << _("Coins") << ": " << Statistics::coins_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->coins : 0, stats.total_coins);
119     context.draw_center_text(white_text, ss.str(), Vector(0, py), LAYER_FOREGROUND1);
120     py += white_text->get_height();
121   }
122   
123   {
124     std::stringstream ss;
125     ss << _("Secrets") << ": " << Statistics::secrets_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->secrets : 0, stats.total_secrets);
126     context.draw_center_text(white_text, ss.str(), Vector(0, py), LAYER_FOREGROUND1);
127     py += white_text->get_height();
128   }
129
130   {
131     std::stringstream ss;
132     ss << _("Time") << ": " << Statistics::time_to_string((best_level_statistics && (best_level_statistics->coins >= 0)) ? best_level_statistics->time : 0);
133     context.draw_center_text(white_text, ss.str(), Vector(0, py), LAYER_FOREGROUND1);
134     py += white_text->get_height();
135   }
136
137 }