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