-Started to move stuff from library back to main game
[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.h"
22 #include "lisp/lisp.h"
23 #include "player_status.h"
24 #include "resources.h"
25
26 static const int START_LIVES = 4;
27 static const int MAX_LIVES = 99;
28
29 PlayerStatus player_status;
30
31 PlayerStatus::PlayerStatus()
32   : coins(0),
33     lives(START_LIVES),
34     bonus(NO_BONUS),
35     score_multiplier(1),
36     max_score_multiplier(1)
37 {
38 }
39
40 void PlayerStatus::reset()
41 {
42   coins = 0;
43   lives = START_LIVES;
44   bonus = NO_BONUS;
45   score_multiplier = 1;
46   max_score_multiplier = 1;
47 }
48
49 void
50 PlayerStatus::incLives()
51 {
52   if(lives < MAX_LIVES)
53     ++lives;
54   sound_manager->play_sound("lifeup");
55 }
56
57 void
58 PlayerStatus::incCoins()
59 {
60   coins++;
61   if(coins >= 100) {
62     incLives();
63     coins = 0;
64   }
65   sound_manager->play_sound("coin");
66 }
67
68 void
69 PlayerStatus::write(lisp::Writer& writer)
70 {
71   switch(bonus) {
72     case NO_BONUS:
73       writer.write_string("bonus", "none");
74       break;
75     case GROWUP_BONUS:
76       writer.write_string("bonus", "growup");
77       break;
78     case FIRE_BONUS:
79       writer.write_string("bonus", "fireflower");
80       break;
81     case ICE_BONUS:
82       writer.write_string("bonus", "iceflower");
83       break;
84     default:
85       std::cerr << "Unknown bonus type.\n";
86       writer.write_string("bonus", "none");
87   }
88
89   writer.write_int("lives", lives);
90   writer.write_int("coins", coins);
91   writer.write_int("max-score-multiplier", max_score_multiplier);
92 }
93
94 void
95 PlayerStatus::read(const lisp::Lisp& lisp)
96 {
97   reset();
98   
99   std::string bonusname;
100   if(lisp.get("bonus", bonusname)) {
101     if(bonusname == "none") {
102       bonus = NO_BONUS;
103     } else if(bonusname == "growup") {
104       bonus = GROWUP_BONUS;
105     } else if(bonusname == "fireflower") {
106       bonus = FIRE_BONUS;
107     } else if(bonusname == "iceflower") {
108       bonus = ICE_BONUS;
109     } else {
110       std::cerr << "Unknown bonus '" << bonusname << "' in savefile.\n";
111       bonus = NO_BONUS;
112     }
113   }
114
115   lisp.get("lives", lives);
116   lisp.get("coins", coins);
117   lisp.get("max-score-multiplier", max_score_multiplier);
118 }
119