argh, clean out copy
[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 #include "timer.hpp"
35
36 static const int START_COINS = 100;
37 static const int MAX_COINS = 99999;
38
39 PlayerStatus* player_status = 0;
40
41 PlayerStatus::PlayerStatus()
42   : coins(START_COINS),
43     bonus(NO_BONUS),
44     max_fire_bullets(0),
45     max_ice_bullets(0),
46     score_multiplier(1),
47     max_score_multiplier(1)
48 {
49   reset();
50
51   coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
52 }
53
54 PlayerStatus::~PlayerStatus()
55 {
56 }
57
58 void PlayerStatus::reset()
59 {
60   coins = START_COINS;
61   bonus = NO_BONUS;
62   score_multiplier = 1;
63   max_score_multiplier = 1;
64 }
65
66 void
67 PlayerStatus::add_coins(int count, bool play_sound)
68 {
69   static float sound_played_time = 0;
70   coins = std::min(coins + count, MAX_COINS);
71   if(play_sound) {
72     if(count >= 100)
73       sound_manager->play("sounds/lifeup.wav");
74     else if (real_time > sound_played_time + 0.010) {
75       sound_manager->play("sounds/coin.wav");
76       sound_played_time = real_time;
77     }
78   }
79 }
80
81 void
82 PlayerStatus::write(lisp::Writer& writer)
83 {
84   switch(bonus) {
85     case NO_BONUS:
86       writer.write_string("bonus", "none");
87       break;
88     case GROWUP_BONUS:
89       writer.write_string("bonus", "growup");
90       break;
91     case FIRE_BONUS:
92       writer.write_string("bonus", "fireflower");
93       break;
94     case ICE_BONUS:
95       writer.write_string("bonus", "iceflower");
96       break;
97     default:
98       log_warning << "Unknown bonus type." << std::endl;
99       writer.write_string("bonus", "none");
100   }
101   writer.write_int("fireflowers", max_fire_bullets);
102   writer.write_int("iceflowers", max_ice_bullets);
103
104   writer.write_int("coins", coins);
105   writer.write_int("max-score-multiplier", max_score_multiplier);
106 }
107
108 void
109 PlayerStatus::read(const lisp::Lisp& lisp)
110 {
111   reset();
112
113   std::string bonusname;
114   if(lisp.get("bonus", bonusname)) {
115     if(bonusname == "none") {
116       bonus = NO_BONUS;
117     } else if(bonusname == "growup") {
118       bonus = GROWUP_BONUS;
119     } else if(bonusname == "fireflower") {
120       bonus = FIRE_BONUS;
121     } else if(bonusname == "iceflower") {
122       bonus = ICE_BONUS;
123     } else {
124       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
125       bonus = NO_BONUS;
126     }
127   }
128   lisp.get("fireflowers", max_fire_bullets);
129   lisp.get("iceflowers", max_ice_bullets);
130
131   lisp.get("coins", coins);
132   lisp.get("max-score-multiplier", max_score_multiplier);
133 }
134
135 void
136 PlayerStatus::draw(DrawingContext& context)
137 {
138   static int displayed_coins = -1;
139   static int next_count = 0;
140
141   if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
142     displayed_coins = coins;
143   }
144   if (++next_count > 2) {
145     next_count = 0;
146     if (displayed_coins < coins) displayed_coins++;
147     if (displayed_coins > coins) displayed_coins--;
148   }
149   displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
150
151   std::stringstream ss;
152   ss << displayed_coins;
153   std::string coins_text = ss.str();
154
155   context.push_transform();
156   context.set_translation(Vector(0, 0));
157
158   Surface* coin_surf = coin_surface.get();
159   if (coin_surf) {
160     context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - gold_fixed_text->get_text_width(coins_text), BORDER_Y + 1), LAYER_HUD);
161   }
162   context.draw_text(gold_fixed_text, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), ALIGN_RIGHT, LAYER_HUD);
163
164   context.pop_transform();
165 }
166
167 void
168 PlayerStatus::operator= (const PlayerStatus& other)
169 {
170   coins = other.coins;
171   bonus = other.bonus;
172   score_multiplier = other.score_multiplier;
173   max_score_multiplier = other.max_score_multiplier;
174 }