converted sounds back to .wav
[supertux.git] / src / player_status.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //
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.
10 //
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.
15 //
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.
19 #include <config.h>
20
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 "main.hpp"
29
30 static const int START_LIVES = 4;
31 static const int MAX_LIVES = 99;
32
33 PlayerStatus player_status;
34
35 PlayerStatus::PlayerStatus()
36   : coins(0),
37     lives(START_LIVES),
38     bonus(NO_BONUS),
39     score_multiplier(1),
40     max_score_multiplier(1)
41 {
42 }
43
44 void PlayerStatus::reset()
45 {
46   coins = 0;
47   lives = START_LIVES;
48   bonus = NO_BONUS;
49   score_multiplier = 1;
50   max_score_multiplier = 1;
51 }
52
53 void
54 PlayerStatus::incLives()
55 {
56   if(lives < MAX_LIVES)
57     ++lives;
58   sound_manager->play("sounds/lifeup.wav");
59 }
60
61 void
62 PlayerStatus::incCoins()
63 {
64   coins++;
65   if(coins >= 100) {
66     incLives();
67     coins = 0;
68   }
69   sound_manager->play("sounds/coin.wav");
70 }
71
72 void
73 PlayerStatus::write(lisp::Writer& writer)
74 {
75   switch(bonus) {
76     case NO_BONUS:
77       writer.write_string("bonus", "none");
78       break;
79     case GROWUP_BONUS:
80       writer.write_string("bonus", "growup");
81       break;
82     case FIRE_BONUS:
83       writer.write_string("bonus", "fireflower");
84       break;
85     case ICE_BONUS:
86       writer.write_string("bonus", "iceflower");
87       break;
88     default:
89       std::cerr << "Unknown bonus type.\n";
90       writer.write_string("bonus", "none");
91   }
92
93   writer.write_int("lives", lives);
94   writer.write_int("coins", coins);
95   writer.write_int("max-score-multiplier", max_score_multiplier);
96 }
97
98 void
99 PlayerStatus::read(const lisp::Lisp& lisp)
100 {
101   reset();
102   
103   std::string bonusname;
104   if(lisp.get("bonus", bonusname)) {
105     if(bonusname == "none") {
106       bonus = NO_BONUS;
107     } else if(bonusname == "growup") {
108       bonus = GROWUP_BONUS;
109     } else if(bonusname == "fireflower") {
110       bonus = FIRE_BONUS;
111     } else if(bonusname == "iceflower") {
112       bonus = ICE_BONUS;
113     } else {
114       std::cerr << "Unknown bonus '" << bonusname << "' in savefile.\n";
115       bonus = NO_BONUS;
116     }
117   }
118
119   lisp.get("lives", lives);
120   lisp.get("coins", coins);
121   lisp.get("max-score-multiplier", max_score_multiplier);
122 }
123
124 void
125 PlayerStatus::draw(DrawingContext& context)
126 {
127   context.push_transform();
128   context.set_translation(Vector(0, 0));
129
130   char str[60];
131   
132   sprintf(str, " %d", player_status.coins);
133   const char* coinstext = _("COINS");
134   context.draw_text(white_text, coinstext,
135       Vector(SCREEN_WIDTH - white_text->get_text_width(coinstext) 
136               - white_text->get_text_width("   99"), 0),
137       LEFT_ALLIGN, LAYER_FOREGROUND1);
138   context.draw_text(gold_text, str,
139       Vector(SCREEN_WIDTH - gold_text->get_text_width(" 99"), 0),
140       LEFT_ALLIGN, LAYER_FOREGROUND1);
141
142   if (player_status.lives >= 5) {
143     sprintf(str, "%dx", player_status.lives);
144     float x = SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w;
145     context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN,
146                       LAYER_FOREGROUND1);
147     context.draw_surface(tux_life, Vector(SCREEN_WIDTH - 16, 20),
148                          LAYER_FOREGROUND1);
149   } else {
150     for(int i= 0; i < player_status.lives; ++i)
151       context.draw_surface(tux_life, 
152           Vector(SCREEN_WIDTH - tux_life->w*4 +(tux_life->w*i), 20),
153           LAYER_FOREGROUND1);
154   }
155
156   const char* livestext = _("LIVES");
157   context.draw_text(white_text, livestext,
158       Vector(SCREEN_WIDTH - white_text->get_text_width(livestext) 
159                 - white_text->get_text_width("   99"), 20),
160       LEFT_ALLIGN, LAYER_FOREGROUND1);
161
162   context.pop_transform();
163 }