1// SuperTux
2// Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
3// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18#include "supertux/player_status.hpp"
19
20#include <sstream>
21
22#include "audio/sound_manager.hpp"
23#include "supertux/globals.hpp"
24#include "supertux/game_session.hpp"
25#include "util/log.hpp"
26#include "util/reader_mapping.hpp"
27#include "util/writer.hpp"
28
29static const int START_COINS = 100;
30static const int MAX_COINS = 9999;
31
32PlayerStatus::PlayerStatus() :
33 coins(START_COINS),
34 bonus(NO_BONUS),
35 max_fire_bullets(0),
36 max_ice_bullets(0),
37 max_air_time(0),
38 max_earth_time(0),
39 worldmap_sprite("images/worldmap/common/tux.sprite"),
40 last_worldmap()
41{
42 reset();
43
44 // FIXME: Move sound handling into PlayerStatusHUD
45 if (SoundManager::current()) {
46 SoundManager::current()->preload("sounds/coin.wav");
47 SoundManager::current()->preload("sounds/lifeup.wav");
48 }
49}
50
51void PlayerStatus::reset()
52{
53 coins = START_COINS;
54 bonus = NO_BONUS;
55}
56
57int
58PlayerStatus::get_max_coins() const
59{
60 return MAX_COINS;
61}
62
63bool
64PlayerStatus::can_reach_checkpoint() const
65{
66 return coins >= 25
67 && !GameSession::current()->get_reset_point_sectorname().empty();
68}
69
70void
71PlayerStatus::add_coins(int count, bool play_sound)
72{
73 coins = std::min(coins + count, MAX_COINS);
74
75 if (!play_sound)
76 return;
77
78 static float sound_played_time = 0;
79 if (count >= 100)
80 SoundManager::current()->play("sounds/lifeup.wav");
81 else if (g_real_time > sound_played_time + 0.010f) {
82 SoundManager::current()->play("sounds/coin.wav");
83 sound_played_time = g_real_time;
84 }
85}
86
87void
88PlayerStatus::write(Writer& writer)
89{
90 switch (bonus) {
91 case NO_BONUS:
92 writer.write("bonus", "none");
93 break;
94 case GROWUP_BONUS:
95 writer.write("bonus", "growup");
96 break;
97 case FIRE_BONUS:
98 writer.write("bonus", "fireflower");
99 break;
100 case ICE_BONUS:
101 writer.write("bonus", "iceflower");
102 break;
103 case AIR_BONUS:
104 writer.write("bonus", "airflower");
105 break;
106 case EARTH_BONUS:
107 writer.write("bonus", "earthflower");
108 break;
109 default:
110 log_warning << "Unknown bonus type." << std::endl;
111 writer.write("bonus", "none");
112 }
113 writer.write("fireflowers", max_fire_bullets);
114 writer.write("iceflowers", max_ice_bullets);
115 writer.write("airflowers", max_air_time);
116 writer.write("earthflowers", max_earth_time);
117
118 writer.write("coins", coins);
119
120 writer.write("worldmap-sprite", worldmap_sprite, false);
121 writer.write("last-worldmap", last_worldmap, false);
122}
123
124void
125PlayerStatus::read(const ReaderMapping& mapping)
126{
127 reset();
128
129 std::string bonusname;
130 if (mapping.get("bonus", bonusname)) {
131 if (bonusname == "none") {
132 bonus = NO_BONUS;
133 } else if (bonusname == "growup") {
134 bonus = GROWUP_BONUS;
135 } else if (bonusname == "fireflower") {
136 bonus = FIRE_BONUS;
137 } else if (bonusname == "iceflower") {
138 bonus = ICE_BONUS;
139 } else if (bonusname == "airflower") {
140 bonus = AIR_BONUS;
141 } else if (bonusname == "earthflower") {
142 bonus = EARTH_BONUS;
143 } else {
144 log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
145 bonus = NO_BONUS;
146 }
147 }
148 mapping.get("fireflowers", max_fire_bullets);
149 mapping.get("iceflowers", max_ice_bullets);
150 mapping.get("airflowers", max_air_time);
151 mapping.get("earthflowers", max_earth_time);
152
153 mapping.get("coins", coins);
154
155 mapping.get("worldmap-sprite", worldmap_sprite);
156 mapping.get("last-worldmap", last_worldmap);
157}
158
159std::string PlayerStatus::get_bonus_prefix() const
160{
161 switch (bonus) {
162 default:
163 case NO_BONUS:
164 return "small";
165 case GROWUP_BONUS:
166 return "big";
167 case FIRE_BONUS:
168 return "fire";
169 case ICE_BONUS:
170 return "ice";
171 case AIR_BONUS:
172 return "air";
173 case EARTH_BONUS:
174 return "earth";
175 }
176}
177
178/* EOF */
179