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/>.
21 #include "audio/sound_manager.hpp"
22 #include "util/writer.hpp"
23 #include "supertux/globals.hpp"
24 #include "supertux/player_status.hpp"
25 #include "supertux/resources.hpp"
26 #include "supertux/timer.hpp"
27 #include "util/reader.hpp"
28 #include "video/drawing_context.hpp"
30 static const int START_COINS = 100;
31 static const int MAX_COINS = 9999;
33 static const int DISPLAYED_COINS_UNSET = -1;
35 PlayerStatus* player_status = 0;
37 PlayerStatus::PlayerStatus() :
38 /* Do we really want -Weffc++ to bully us into duplicating code from "reset" here? */
43 displayed_coins(DISPLAYED_COINS_UNSET),
44 displayed_coins_frame(0),
49 coin_surface = Surface::create("images/engine/hud/coins-0.png");
50 sound_manager->preload("sounds/coin.wav");
51 sound_manager->preload("sounds/lifeup.wav");
54 PlayerStatus::~PlayerStatus()
58 void PlayerStatus::reset()
62 displayed_coins = DISPLAYED_COINS_UNSET;
66 PlayerStatus::add_coins(int count, bool play_sound)
68 static float sound_played_time = 0;
69 coins = std::min(coins + count, MAX_COINS);
72 sound_manager->play("sounds/lifeup.wav");
73 else if (real_time > sound_played_time + 0.010) {
74 sound_manager->play("sounds/coin.wav");
75 sound_played_time = real_time;
81 PlayerStatus::write(lisp::Writer& writer)
85 writer.write("bonus", "none");
88 writer.write("bonus", "growup");
91 writer.write("bonus", "fireflower");
94 writer.write("bonus", "iceflower");
97 log_warning << "Unknown bonus type." << std::endl;
98 writer.write("bonus", "none");
100 writer.write("fireflowers", max_fire_bullets);
101 writer.write("iceflowers", max_ice_bullets);
103 writer.write("coins", coins);
107 PlayerStatus::read(const Reader& lisp)
111 std::string bonusname;
112 if(lisp.get("bonus", bonusname)) {
113 if(bonusname == "none") {
115 } else if(bonusname == "growup") {
116 bonus = GROWUP_BONUS;
117 } else if(bonusname == "fireflower") {
119 } else if(bonusname == "iceflower") {
122 log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
126 lisp.get("fireflowers", max_fire_bullets);
127 lisp.get("iceflowers", max_ice_bullets);
129 lisp.get("coins", coins);
133 PlayerStatus::draw(DrawingContext& context)
137 if ((displayed_coins == DISPLAYED_COINS_UNSET) ||
138 (fabsf(displayed_coins - coins) > 100)) {
139 displayed_coins = coins;
140 displayed_coins_frame = 0;
142 if (++displayed_coins_frame > 2) {
143 displayed_coins_frame = 0;
144 if (displayed_coins < coins) displayed_coins++;
145 if (displayed_coins > coins) displayed_coins--;
147 displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
149 std::stringstream ss;
150 ss << displayed_coins;
151 std::string coins_text = ss.str();
153 context.push_transform();
154 context.set_translation(Vector(0, 0));
158 context.draw_surface(coin_surface,
159 Vector(SCREEN_WIDTH - BORDER_X - coin_surface->get_width() - Resources::fixed_font->get_text_width(coins_text),
160 BORDER_Y + 1 + (Resources::fixed_font->get_text_height(coins_text) + 5) * player_id),
163 context.draw_text(Resources::fixed_font,
165 Vector(SCREEN_WIDTH - BORDER_X - Resources::fixed_font->get_text_width(coins_text),
166 BORDER_Y + (Resources::fixed_font->get_text_height(coins_text) + 5) * player_id),
169 PlayerStatus::text_color);
171 context.pop_transform();