Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / player_status.cpp
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 <math.h>
19
20 #include "audio/sound_manager.hpp"
21 #include "lisp/lisp.hpp"
22 #include "util/writer.hpp"
23 #include "supertux/main.hpp"
24 #include "supertux/player_status.hpp"
25 #include "supertux/resources.hpp"
26 #include "supertux/timer.hpp"
27 #include "video/drawing_context.hpp"
28
29 static const int START_COINS = 100;
30 static const int MAX_COINS = 9999;
31
32 PlayerStatus* player_status = 0;
33
34 PlayerStatus::PlayerStatus()
35   : coins(START_COINS),
36     bonus(NO_BONUS),
37     max_fire_bullets(0),
38     max_ice_bullets(0)
39 {
40   reset();
41
42   coin_surface.reset(new Surface("images/engine/hud/coins-0.png"));
43   sound_manager->preload("sounds/coin.wav");
44   sound_manager->preload("sounds/lifeup.wav");
45 }
46
47 PlayerStatus::~PlayerStatus()
48 {
49 }
50
51 void PlayerStatus::reset()
52 {
53   coins = START_COINS;
54   bonus = NO_BONUS;
55 }
56
57 void
58 PlayerStatus::add_coins(int count, bool play_sound)
59 {
60   static float sound_played_time = 0;
61   coins = std::min(coins + count, MAX_COINS);
62   if(play_sound) {
63     if(count >= 100)
64       sound_manager->play("sounds/lifeup.wav");
65     else if (real_time > sound_played_time + 0.010) {
66       sound_manager->play("sounds/coin.wav");
67       sound_played_time = real_time;
68     }
69   }
70 }
71
72 void
73 PlayerStatus::write(lisp::Writer& writer)
74 {
75   switch(bonus) {
76     case NO_BONUS:
77       writer.write("bonus", "none");
78       break;
79     case GROWUP_BONUS:
80       writer.write("bonus", "growup");
81       break;
82     case FIRE_BONUS:
83       writer.write("bonus", "fireflower");
84       break;
85     case ICE_BONUS:
86       writer.write("bonus", "iceflower");
87       break;
88     default:
89       log_warning << "Unknown bonus type." << std::endl;
90       writer.write("bonus", "none");
91   }
92   writer.write("fireflowers", max_fire_bullets);
93   writer.write("iceflowers", max_ice_bullets);
94
95   writer.write("coins", coins);
96 }
97
98 void
99 PlayerStatus::read(const Reader& lisp)
100 {
101   reset();
102
103   std::string bonusname;
104   if(lisp.get("bonus", bonusname)) {
105     if(bonusname == "none") {
106       bonus = NO_BONUS;
107     } else if(bonusname == "growup") {
108       bonus = GROWUP_BONUS;
109     } else if(bonusname == "fireflower") {
110       bonus = FIRE_BONUS;
111     } else if(bonusname == "iceflower") {
112       bonus = ICE_BONUS;
113     } else {
114       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
115       bonus = NO_BONUS;
116     }
117   }
118   lisp.get("fireflowers", max_fire_bullets);
119   lisp.get("iceflowers", max_ice_bullets);
120
121   lisp.get("coins", coins);
122 }
123
124 void
125 PlayerStatus::draw(DrawingContext& context)
126 {
127   static int displayed_coins = -1;
128   static int next_count = 0;
129
130   if ((displayed_coins == -1) || (fabsf(displayed_coins - coins) > 100)) {
131     displayed_coins = coins;
132   }
133   if (++next_count > 2) {
134     next_count = 0;
135     if (displayed_coins < coins) displayed_coins++;
136     if (displayed_coins > coins) displayed_coins--;
137   }
138   displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
139
140   std::stringstream ss;
141   ss << displayed_coins;
142   std::string coins_text = ss.str();
143
144   context.push_transform();
145   context.set_translation(Vector(0, 0));
146
147   Surface* coin_surf = coin_surface.get();
148   if (coin_surf) {
149     context.draw_surface(coin_surf, Vector(SCREEN_WIDTH - BORDER_X - coin_surf->get_width() - fixed_font->get_text_width(coins_text), BORDER_Y + 1), LAYER_HUD);
150   }
151   context.draw_text(fixed_font, coins_text, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), ALIGN_RIGHT, LAYER_HUD, PlayerStatus::text_color);
152
153   context.pop_transform();
154 }
155
156 void
157 PlayerStatus::operator= (const PlayerStatus& other)
158 {
159   coins = other.coins;
160   bonus = other.bonus;
161 }
162
163 /* EOF */