Fixed trailing whitespaces in all(?) source files of supertux, also fixed some svn...
[supertux.git] / src / player_status.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include <math.h>
23 #include "lisp/writer.hpp"
24 #include "lisp/lisp.hpp"
25 #include "player_status.hpp"
26 #include "resources.hpp"
27 #include "gettext.hpp"
28 #include "video/drawing_context.hpp"
29 #include "audio/sound_manager.hpp"
30 #include "sprite/sprite_manager.hpp"
31 #include "math/vector.hpp"
32 #include "main.hpp"
33 #include "log.hpp"
34
35 static const int START_COINS = 100;
36 static const int MAX_COINS = 99999;
37
38 PlayerStatus* player_status = 0;
39
40 PlayerStatus::PlayerStatus()
41   : coins(START_COINS),
42     bonus(NO_BONUS),
43     max_fire_bullets(0),
44     max_ice_bullets(0),
45     score_multiplier(1),
46     max_score_multiplier(1)
47 {
48   reset();
49
50   coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
51 }
52
53 PlayerStatus::~PlayerStatus()
54 {
55 }
56
57 void PlayerStatus::reset()
58 {
59   coins = START_COINS;
60   bonus = NO_BONUS;
61   score_multiplier = 1;
62   max_score_multiplier = 1;
63 }
64
65 void
66 PlayerStatus::add_coins(int count, bool play_sound)
67 {
68   coins = std::min(coins + count, MAX_COINS);
69   if(play_sound) {
70     if(count >= 100)
71       sound_manager->play("sounds/lifeup.wav");
72     else
73       sound_manager->play("sounds/coin.wav");
74   }
75 }
76
77 void
78 PlayerStatus::write(lisp::Writer& writer)
79 {
80   switch(bonus) {
81     case NO_BONUS:
82       writer.write_string("bonus", "none");
83       break;
84     case GROWUP_BONUS:
85       writer.write_string("bonus", "growup");
86       break;
87     case FIRE_BONUS:
88       writer.write_string("bonus", "fireflower");
89       break;
90     case ICE_BONUS:
91       writer.write_string("bonus", "iceflower");
92       break;
93     default:
94       log_warning << "Unknown bonus type." << std::endl;
95       writer.write_string("bonus", "none");
96   }
97   writer.write_int("fireflowers", max_fire_bullets);
98   writer.write_int("iceflowers", max_ice_bullets);
99
100   writer.write_int("coins", coins);
101   writer.write_int("max-score-multiplier", max_score_multiplier);
102 }
103
104 void
105 PlayerStatus::read(const lisp::Lisp& lisp)
106 {
107   reset();
108
109   std::string bonusname;
110   if(lisp.get("bonus", bonusname)) {
111     if(bonusname == "none") {
112       bonus = NO_BONUS;
113     } else if(bonusname == "growup") {
114       bonus = GROWUP_BONUS;
115     } else if(bonusname == "fireflower") {
116       bonus = FIRE_BONUS;
117     } else if(bonusname == "iceflower") {
118       bonus = ICE_BONUS;
119     } else {
120       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
121       bonus = NO_BONUS;
122     }
123   }
124   lisp.get("fireflowers", max_fire_bullets);
125   lisp.get("iceflowers", max_ice_bullets);
126
127   lisp.get("coins", coins);
128   lisp.get("max-score-multiplier", max_score_multiplier);
129 }
130
131 void
132 PlayerStatus::draw(DrawingContext& context)
133 {
134   static int displayed_coins = -1;
135   static int next_count = 0;
136
137   if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
138     displayed_coins = coins;
139   }
140   if (++next_count > 2) {
141     next_count = 0;
142     if (displayed_coins < coins) displayed_coins++;
143     if (displayed_coins > coins) displayed_coins--;
144   }
145   displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
146
147   std::stringstream ss;
148   ss << displayed_coins;
149   std::string coins_text = ss.str();
150
151   context.push_transform();
152   context.set_translation(Vector(0, 0));
153
154   Surface* coin_surf = coin_surface.get();
155   if (coin_surf) {
156     context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - gold_text->get_text_width(coins_text), BORDER_Y + 1), LAYER_HUD);
157   }
158   context.draw_text(gold_text, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), RIGHT_ALLIGN, LAYER_HUD);
159
160   context.pop_transform();
161 }
162
163 void
164 PlayerStatus::operator= (const PlayerStatus& other)
165 {
166   coins = other.coins;
167   bonus = other.bonus;
168   score_multiplier = other.score_multiplier;
169   max_score_multiplier = other.max_score_multiplier;
170 }