3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "lisp/writer.hpp"
22 #include "lisp/lisp.hpp"
23 #include "player_status.hpp"
24 #include "resources.hpp"
25 #include "gettext.hpp"
26 #include "video/drawing_context.hpp"
27 #include "audio/sound_manager.hpp"
28 #include "sprite/sprite_manager.hpp"
29 #include "math/vector.hpp"
33 static const int START_LIVES = 4;
34 static const int MAX_LIVES = 99;
36 PlayerStatus* player_status = 0;
38 PlayerStatus::PlayerStatus()
43 max_score_multiplier(1)
46 key_brass = sprite_manager->create("images/objects/keys/key_brass.sprite");
47 key_iron = sprite_manager->create("images/objects/keys/key_iron.sprite");
48 key_bronze = sprite_manager->create("images/objects/keys/key_bronze.sprite");
49 key_silver = sprite_manager->create("images/objects/keys/key_silver.sprite");
50 key_gold = 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");
58 PlayerStatus::~PlayerStatus()
67 void PlayerStatus::reset()
74 max_score_multiplier = 1;
78 PlayerStatus::incLives()
82 sound_manager->play("sounds/lifeup.wav");
86 PlayerStatus::incCoins()
93 sound_manager->play("sounds/coin.wav");
97 PlayerStatus::set_keys(int new_key)
100 key_brass->set_action(keys & KEY_BRASS ? "display" : "outline");
101 key_iron->set_action(keys & KEY_IRON ? "display" : "outline");
102 key_bronze->set_action(keys & KEY_BRONZE ? "display" : "outline");
103 key_silver->set_action(keys & KEY_SILVER ? "display" : "outline");
104 key_gold->set_action(keys & KEY_GOLD ? "display" : "outline");
108 PlayerStatus::write(lisp::Writer& writer)
112 writer.write_string("bonus", "none");
115 writer.write_string("bonus", "growup");
118 writer.write_string("bonus", "fireflower");
121 writer.write_string("bonus", "iceflower");
124 msg_warning("Unknown bonus type.");
125 writer.write_string("bonus", "none");
127 writer.write_bool("key-brass", keys & KEY_BRASS);
128 writer.write_bool("key-iron", keys & KEY_IRON);
129 writer.write_bool("key-bronze", keys & KEY_BRONZE);
130 writer.write_bool("key-silver", keys & KEY_SILVER);
131 writer.write_bool("key-gold", keys & KEY_GOLD);
133 writer.write_int("lives", lives);
134 writer.write_int("coins", coins);
135 writer.write_int("max-score-multiplier", max_score_multiplier);
139 PlayerStatus::read(const lisp::Lisp& lisp)
143 std::string bonusname;
144 if(lisp.get("bonus", bonusname)) {
145 if(bonusname == "none") {
147 } else if(bonusname == "growup") {
148 bonus = GROWUP_BONUS;
149 } else if(bonusname == "fireflower") {
151 } else if(bonusname == "iceflower") {
154 msg_warning("Unknown bonus '" << bonusname << "' in savefile");
159 if(lisp.get("key-brass", val) && val == true)
161 if(lisp.get("key-iron", val) && val == true)
163 if(lisp.get("key-bronze", val) && val == true)
164 set_keys(KEY_BRONZE);
165 if(lisp.get("key-silver", val) && val == true)
166 set_keys(KEY_SILVER);
167 if(lisp.get("key-gold", val) && val == true)
170 lisp.get("lives", lives);
171 lisp.get("coins", coins);
172 lisp.get("max-score-multiplier", max_score_multiplier);
176 PlayerStatus::draw_keys(DrawingContext& context)
178 const float SPACING = 10;
180 x = BORDER_X; y = BORDER_Y;
181 key_brass->draw(context, Vector(x, y), LAYER_FOREGROUND1);
182 x += key_brass->get_width() + SPACING;
183 key_iron->draw(context, Vector(x, y), LAYER_FOREGROUND1);
184 x += key_iron->get_width() + SPACING;
185 key_bronze->draw(context, Vector(x, y), LAYER_FOREGROUND1);
186 x += key_bronze->get_width() + SPACING;
187 key_silver->draw(context, Vector(x, y), LAYER_FOREGROUND1);
188 x += key_silver->get_width() + SPACING;
189 key_gold->draw(context, Vector(x, y), LAYER_FOREGROUND1);
190 x += key_gold->get_width() + SPACING;
194 PlayerStatus::draw(DrawingContext& context)
196 context.push_transform();
197 context.set_translation(Vector(0, 0));
201 sprintf(str, " %d", player_status->coins);
202 const char* coinstext = _("COINS");
203 context.draw_text(white_text, coinstext,
204 Vector(SCREEN_WIDTH - white_text->get_text_width(coinstext)
205 - white_text->get_text_width(" 99") - BORDER_X, BORDER_Y),
206 LEFT_ALLIGN, LAYER_FOREGROUND1);
207 context.draw_text(gold_text, str,
208 Vector(SCREEN_WIDTH - gold_text->get_text_width(" 99") - BORDER_X, BORDER_Y),
209 LEFT_ALLIGN, LAYER_FOREGROUND1);
211 if (player_status->lives >= 5) {
212 sprintf(str, "%dx", player_status->lives);
213 float x = SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->get_width();
214 context.draw_text(gold_text, str, Vector(x - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN,
216 context.draw_surface(tux_life, Vector(SCREEN_WIDTH - 16 - BORDER_X, BORDER_Y + 20),
219 for(int i= 0; i < player_status->lives; ++i)
220 context.draw_surface(tux_life,
221 Vector(SCREEN_WIDTH - tux_life->get_width()*4 +(tux_life->get_width()*i) - BORDER_X,
226 const char* livestext = _("LIVES");
227 context.draw_text(white_text, livestext,
228 Vector(SCREEN_WIDTH - white_text->get_text_width(livestext)
229 - white_text->get_text_width(" 99") - BORDER_X, BORDER_Y + 20),
230 LEFT_ALLIGN, LAYER_FOREGROUND1);
234 context.pop_transform();
238 PlayerStatus::operator= (const PlayerStatus& other)
243 score_multiplier = other.score_multiplier;
244 max_score_multiplier = other.max_score_multiplier;