Had a bit of time today and worked on supertux:
[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   : distros(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   distros = 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   SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
55 }
56
57 void
58 PlayerStatus::incCoins()
59 {
60   distros++;
61   if(distros >= 100) {
62     incLives();
63     distros = 0;
64   }
65   SoundManager::get()->play_sound(IDToSound(SND_DISTRO));
66 }
67
68 void
69 PlayerStatus::write(lisp::Writer& writer)
70 {
71   switch(bonus) {
72     case PlayerStatus::NO_BONUS:
73       writer.write_string("bonus", "none");
74       break;
75     case PlayerStatus::GROWUP_BONUS:
76       writer.write_string("bonus", "growup");
77       break;
78     case PlayerStatus::FLOWER_BONUS:
79       writer.write_string("bonus", "fireflower");
80       break;
81     default:
82       std::cerr << "Unknown bonus type.\n";
83       writer.write_string("bonus", "none");
84   }
85
86   writer.write_int("lives", lives);
87   writer.write_int("distros", distros);
88   writer.write_int("max-score-multiplier", max_score_multiplier);
89 }
90
91 void
92 PlayerStatus::read(const lisp::Lisp& lisp)
93 {
94   reset();
95   
96   std::string bonusname;
97   if(lisp.get("bonus", bonusname)) {
98     if(bonusname == "none") {
99       bonus = NO_BONUS;
100     } else if(bonusname == "growup") {
101       bonus = GROWUP_BONUS;
102     } else if(bonusname == "fireflower") {
103       bonus = FLOWER_BONUS;
104     } else {
105       std::cerr << "Unknown bonus '" << bonusname << "' in savefile.\n";
106       bonus = NO_BONUS;
107     }
108   }
109
110   lisp.get("lives", lives);
111   lisp.get("distros", distros);
112   lisp.get("max-score-multiplier", max_score_multiplier);
113 }
114