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.
22 #include "lisp/writer.hpp"
23 #include "lisp/lisp.hpp"
24 #include "player_status.hpp"
25 #include "resources.hpp"
26 #include "gettext.hpp"
27 #include "video/drawing_context.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "sprite/sprite_manager.hpp"
30 #include "math/vector.hpp"
34 static const int START_COINS = 100;
35 static const int MAX_COINS = 99999;
37 PlayerStatus* player_status = 0;
39 PlayerStatus::PlayerStatus()
43 max_score_multiplier(1)
46 key_brass.reset(sprite_manager->create("images/objects/keys/key_brass.sprite"));
47 key_iron.reset(sprite_manager->create("images/objects/keys/key_iron.sprite"));
48 key_bronze.reset(sprite_manager->create("images/objects/keys/key_bronze.sprite"));
49 key_silver.reset(sprite_manager->create("images/objects/keys/key_silver.sprite"));
50 key_gold.reset(sprite_manager->create("images/objects/keys/key_gold.sprite"));
51 key_brass->set_action("outline");
52 key_iron->set_action("outline");
53 key_bronze->set_action("outline");
54 key_silver->set_action("outline");
55 key_gold->set_action("outline");
57 tux_life.reset(sprite_manager->create("images/creatures/tux_small/tux-life.sprite"));
59 Console::instance->registerCommand("coins", this);
62 PlayerStatus::~PlayerStatus()
66 void PlayerStatus::reset()
72 max_score_multiplier = 1;
76 PlayerStatus::add_coins(int count)
78 coins = std::min(coins + count, MAX_COINS);
80 sound_manager->play("sounds/lifeup.wav");
82 sound_manager->play("sounds/coin.wav");
86 PlayerStatus::set_keys(int new_key)
89 key_brass->set_action(keys & KEY_BRASS ? "display" : "outline");
90 key_iron->set_action(keys & KEY_IRON ? "display" : "outline");
91 key_bronze->set_action(keys & KEY_BRONZE ? "display" : "outline");
92 key_silver->set_action(keys & KEY_SILVER ? "display" : "outline");
93 key_gold->set_action(keys & KEY_GOLD ? "display" : "outline");
97 PlayerStatus::write(lisp::Writer& writer)
101 writer.write_string("bonus", "none");
104 writer.write_string("bonus", "growup");
107 writer.write_string("bonus", "fireflower");
110 writer.write_string("bonus", "iceflower");
113 log_warning << "Unknown bonus type." << std::endl;
114 writer.write_string("bonus", "none");
116 writer.write_bool("key-brass", keys & KEY_BRASS);
117 writer.write_bool("key-iron", keys & KEY_IRON);
118 writer.write_bool("key-bronze", keys & KEY_BRONZE);
119 writer.write_bool("key-silver", keys & KEY_SILVER);
120 writer.write_bool("key-gold", keys & KEY_GOLD);
122 writer.write_int("coins", coins);
123 writer.write_int("max-score-multiplier", max_score_multiplier);
127 PlayerStatus::read(const lisp::Lisp& lisp)
131 std::string bonusname;
132 if(lisp.get("bonus", bonusname)) {
133 if(bonusname == "none") {
135 } else if(bonusname == "growup") {
136 bonus = GROWUP_BONUS;
137 } else if(bonusname == "fireflower") {
139 } else if(bonusname == "iceflower") {
142 log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
147 if(lisp.get("key-brass", val) && val == true)
149 if(lisp.get("key-iron", val) && val == true)
151 if(lisp.get("key-bronze", val) && val == true)
152 set_keys(KEY_BRONZE);
153 if(lisp.get("key-silver", val) && val == true)
154 set_keys(KEY_SILVER);
155 if(lisp.get("key-gold", val) && val == true)
158 lisp.get("coins", coins);
159 lisp.get("max-score-multiplier", max_score_multiplier);
163 PlayerStatus::draw_keys(DrawingContext& context)
165 const float SPACING = 10;
167 x = BORDER_X; y = BORDER_Y;
168 key_brass->draw(context, Vector(x, y), LAYER_FOREGROUND1);
169 x += key_brass->get_width() + SPACING;
170 key_iron->draw(context, Vector(x, y), LAYER_FOREGROUND1);
171 x += key_iron->get_width() + SPACING;
172 key_bronze->draw(context, Vector(x, y), LAYER_FOREGROUND1);
173 x += key_bronze->get_width() + SPACING;
174 key_silver->draw(context, Vector(x, y), LAYER_FOREGROUND1);
175 x += key_silver->get_width() + SPACING;
176 key_gold->draw(context, Vector(x, y), LAYER_FOREGROUND1);
177 x += key_gold->get_width() + SPACING;
181 PlayerStatus::draw(DrawingContext& context)
183 context.push_transform();
184 context.set_translation(Vector(0, 0));
188 int displayCoins = std::max(player_status->coins, 0);
189 sprintf(str, "%d", displayCoins);
190 const char* coinstext = _("COINS");
191 context.draw_text(white_text, coinstext, Vector(SCREEN_WIDTH - white_text->get_text_width(coinstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y), LEFT_ALLIGN, LAYER_FOREGROUND1);
192 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), RIGHT_ALLIGN, LAYER_FOREGROUND1);
196 context.pop_transform();
200 PlayerStatus::operator= (const PlayerStatus& other)
204 score_multiplier = other.score_multiplier;
205 max_score_multiplier = other.max_score_multiplier;
210 PlayerStatus::consoleCommand(std::string command, std::vector<std::string> arguments)
212 if (command == "coins") {
213 if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
214 log_info << "Usage: coins <number>" << std::endl;
216 coins = Console::string_to<int>(arguments[0]);