4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "lisp/writer.hpp"
24 #include "lisp/lisp.hpp"
25 #include "player_status.hpp"
26 #include "resources.hpp"
27 #include "gettext.hpp"
28 #include "video/drawing_context.hpp"
29 #include "audio/sound_manager.hpp"
30 #include "sprite/sprite_manager.hpp"
31 #include "math/vector.hpp"
35 static const int START_COINS = 100;
36 static const int MAX_COINS = 99999;
38 PlayerStatus* player_status = 0;
40 PlayerStatus::PlayerStatus()
46 max_score_multiplier(1)
50 coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
52 Console::instance->registerCommand("coins", this);
55 PlayerStatus::~PlayerStatus()
59 void PlayerStatus::reset()
64 max_score_multiplier = 1;
68 PlayerStatus::add_coins(int count, bool play_sound)
70 coins = std::min(coins + count, MAX_COINS);
73 sound_manager->play("sounds/lifeup.wav");
75 sound_manager->play("sounds/coin.wav");
80 PlayerStatus::write(lisp::Writer& writer)
84 writer.write_string("bonus", "none");
87 writer.write_string("bonus", "growup");
90 writer.write_string("bonus", "fireflower");
93 writer.write_string("bonus", "iceflower");
96 log_warning << "Unknown bonus type." << std::endl;
97 writer.write_string("bonus", "none");
99 writer.write_int("fireflowers", max_fire_bullets);
100 writer.write_int("iceflowers", max_ice_bullets);
102 writer.write_int("coins", coins);
103 writer.write_int("max-score-multiplier", max_score_multiplier);
107 PlayerStatus::read(const lisp::Lisp& 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);
130 lisp.get("max-score-multiplier", max_score_multiplier);
134 PlayerStatus::draw(DrawingContext& context)
136 static int displayed_coins = -1;
137 static int next_count = 0;
139 if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
140 displayed_coins = coins;
142 if (++next_count > 2) {
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));
156 Surface* coin_surf = coin_surface.get();
158 context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - gold_text->get_text_width(coins_text), BORDER_Y + 1), LAYER_HUD);
160 context.draw_text(gold_text, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), RIGHT_ALLIGN, LAYER_HUD);
162 context.pop_transform();
166 PlayerStatus::operator= (const PlayerStatus& other)
170 score_multiplier = other.score_multiplier;
171 max_score_multiplier = other.max_score_multiplier;
175 PlayerStatus::consoleCommand(std::string command, std::vector<std::string> arguments)
177 if (command == "coins") {
178 if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
179 log_info << "Usage: coins <number>" << std::endl;
181 coins = Console::string_to<int>(arguments[0]);