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