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 "lisp/lisp.hpp"
22 #include "util/writer.hpp"
23 #include "supertux/main.hpp"
24 #include "supertux/player_status.hpp"
25 #include "supertux/resources.hpp"
26 #include "supertux/timer.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()
42 coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
43 sound_manager->preload("sounds/coin.wav");
44 sound_manager->preload("sounds/lifeup.wav");
47 PlayerStatus::~PlayerStatus()
51 void PlayerStatus::reset()
58 PlayerStatus::add_coins(int count, bool play_sound)
60 static float sound_played_time = 0;
61 coins = std::min(coins + count, MAX_COINS);
64 sound_manager->play("sounds/lifeup.wav");
65 else if (real_time > sound_played_time + 0.010) {
66 sound_manager->play("sounds/coin.wav");
67 sound_played_time = real_time;
73 PlayerStatus::write(lisp::Writer& writer)
77 writer.write("bonus", "none");
80 writer.write("bonus", "growup");
83 writer.write("bonus", "fireflower");
86 writer.write("bonus", "iceflower");
89 log_warning << "Unknown bonus type." << std::endl;
90 writer.write("bonus", "none");
92 writer.write("fireflowers", max_fire_bullets);
93 writer.write("iceflowers", max_ice_bullets);
95 writer.write("coins", coins);
99 PlayerStatus::read(const Reader& lisp)
103 std::string bonusname;
104 if(lisp.get("bonus", bonusname)) {
105 if(bonusname == "none") {
107 } else if(bonusname == "growup") {
108 bonus = GROWUP_BONUS;
109 } else if(bonusname == "fireflower") {
111 } else if(bonusname == "iceflower") {
114 log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
118 lisp.get("fireflowers", max_fire_bullets);
119 lisp.get("iceflowers", max_ice_bullets);
121 lisp.get("coins", coins);
125 PlayerStatus::draw(DrawingContext& context)
127 static int displayed_coins = -1;
128 static int next_count = 0;
130 if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
131 displayed_coins = coins;
133 if (++next_count > 2) {
135 if (displayed_coins < coins) displayed_coins++;
136 if (displayed_coins > coins) displayed_coins--;
138 displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
140 std::stringstream ss;
141 ss << displayed_coins;
142 std::string coins_text = ss.str();
144 context.push_transform();
145 context.set_translation(Vector(0, 0));
147 Surface* coin_surf = coin_surface.get();
149 context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - fixed_font->get_text_width(coins_text), BORDER_Y + 1), LAYER_HUD);
151 context.draw_text(fixed_font, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), ALIGN_RIGHT, LAYER_HUD, PlayerStatus::text_color);
153 context.pop_transform();
157 PlayerStatus::operator= (const PlayerStatus& other)