2 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "audio/sound_manager.hpp"
21 #include "util/writer.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/player_status.hpp"
24 #include "supertux/resources.hpp"
25 #include "supertux/timer.hpp"
26 #include "util/reader.hpp"
27 #include "video/drawing_context.hpp"
29 static const int START_COINS = 100;
30 static const int MAX_COINS = 9999;
32 PlayerStatus* player_status = 0;
34 PlayerStatus::PlayerStatus() :
43 coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
44 sound_manager->preload("sounds/coin.wav");
45 sound_manager->preload("sounds/lifeup.wav");
48 PlayerStatus::~PlayerStatus()
52 void PlayerStatus::reset()
59 PlayerStatus::add_coins(int count, bool play_sound)
61 static float sound_played_time = 0;
62 coins = std::min(coins + count, MAX_COINS);
65 sound_manager->play("sounds/lifeup.wav");
66 else if (real_time > sound_played_time + 0.010) {
67 sound_manager->play("sounds/coin.wav");
68 sound_played_time = real_time;
74 PlayerStatus::write(lisp::Writer& writer)
78 writer.write("bonus", "none");
81 writer.write("bonus", "growup");
84 writer.write("bonus", "fireflower");
87 writer.write("bonus", "iceflower");
90 log_warning << "Unknown bonus type." << std::endl;
91 writer.write("bonus", "none");
93 writer.write("fireflowers", max_fire_bullets);
94 writer.write("iceflowers", max_ice_bullets);
96 writer.write("coins", coins);
100 PlayerStatus::read(const Reader& lisp)
104 std::string bonusname;
105 if(lisp.get("bonus", bonusname)) {
106 if(bonusname == "none") {
108 } else if(bonusname == "growup") {
109 bonus = GROWUP_BONUS;
110 } else if(bonusname == "fireflower") {
112 } else if(bonusname == "iceflower") {
115 log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
119 lisp.get("fireflowers", max_fire_bullets);
120 lisp.get("iceflowers", max_ice_bullets);
122 lisp.get("coins", coins);
126 PlayerStatus::draw(DrawingContext& context)
128 static int displayed_coins = -1;
129 static int next_count = 0;
131 if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
132 displayed_coins = coins;
134 if (++next_count > 2) {
136 if (displayed_coins < coins) displayed_coins++;
137 if (displayed_coins > coins) displayed_coins--;
139 displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
141 std::stringstream ss;
142 ss << displayed_coins;
143 std::string coins_text = ss.str();
145 context.push_transform();
146 context.set_translation(Vector(0, 0));
148 Surface* coin_surf = coin_surface.get();
150 context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - Resources::fixed_font->get_text_width(coins_text),
151 BORDER_Y + 1), LAYER_HUD);
153 context.draw_text(Resources::fixed_font, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), ALIGN_RIGHT, LAYER_HUD, PlayerStatus::text_color);
155 context.pop_transform();