Basic code structure to allow for two new powerups
[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 static const int DISPLAYED_COINS_UNSET = -1;
34
35 PlayerStatus* player_status = 0;
36
37 PlayerStatus::PlayerStatus() :
38   /* Do we really want -Weffc++ to bully us into duplicating code from "reset" here? */
39   coins(START_COINS),
40   bonus(NO_BONUS),
41   max_fire_bullets(0),
42   max_ice_bullets(0),
43   displayed_coins(DISPLAYED_COINS_UNSET),
44   displayed_coins_frame(0),
45   coin_surface()
46 {
47   reset();
48
49   coin_surface = Surface::create("images/engine/hud/coins-0.png");
50   SoundManager::current()->preload("sounds/coin.wav");
51   SoundManager::current()->preload("sounds/lifeup.wav");
52 }
53
54 PlayerStatus::~PlayerStatus()
55 {
56 }
57
58 void PlayerStatus::reset()
59 {
60   coins = START_COINS;
61   bonus = NO_BONUS;
62   displayed_coins = DISPLAYED_COINS_UNSET;
63 }
64
65 void
66 PlayerStatus::add_coins(int count, bool play_sound)
67 {
68   static float sound_played_time = 0;
69   coins = std::min(coins + count, MAX_COINS);
70   if(play_sound) {
71     if(count >= 100)
72       SoundManager::current()->play("sounds/lifeup.wav");
73     else if (real_time > sound_played_time + 0.010) {
74       SoundManager::current()->play("sounds/coin.wav");
75       sound_played_time = real_time;
76     }
77   }
78 }
79
80 void
81 PlayerStatus::write(lisp::Writer& writer)
82 {
83   switch(bonus) {
84     case NO_BONUS:
85       writer.write("bonus", "none");
86       break;
87     case GROWUP_BONUS:
88       writer.write("bonus", "growup");
89       break;
90     case FIRE_BONUS:
91       writer.write("bonus", "fireflower");
92       break;
93     case ICE_BONUS:
94       writer.write("bonus", "iceflower");
95       break;
96     case AIR_BONUS:
97       writer.write("bonus", "airflower");
98       break;
99     case EARTH_BONUS:
100       writer.write("bonus", "earthflower");
101       break;
102     default:
103       log_warning << "Unknown bonus type." << std::endl;
104       writer.write("bonus", "none");
105   }
106   writer.write("fireflowers", max_fire_bullets);
107   writer.write("iceflowers", max_ice_bullets);
108   writer.write("airflowers", max_air_time);
109   writer.write("earthflowers", max_earth_time);
110
111   writer.write("coins", coins);
112 }
113
114 void
115 PlayerStatus::read(const Reader& lisp)
116 {
117   reset();
118
119   std::string bonusname;
120   if(lisp.get("bonus", bonusname)) {
121     if(bonusname == "none") {
122       bonus = NO_BONUS;
123     } else if(bonusname == "growup") {
124       bonus = GROWUP_BONUS;
125     } else if(bonusname == "fireflower") {
126       bonus = FIRE_BONUS;
127     } else if(bonusname == "iceflower") {
128       bonus = ICE_BONUS;
129     } else if(bonusname == "airflower") {
130       bonus = AIR_BONUS;
131     } else if(bonusname == "earthflower") {
132       bonus = EARTH_BONUS;
133     } else {
134       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
135       bonus = NO_BONUS;
136     }
137   }
138   lisp.get("fireflowers", max_fire_bullets);
139   lisp.get("iceflowers", max_ice_bullets);
140   lisp.get("airflowers", max_air_time);
141   lisp.get("earthflowers", max_earth_time);
142
143   lisp.get("coins", coins);
144 }
145
146 void
147 PlayerStatus::draw(DrawingContext& context)
148 {
149   int player_id = 0;
150
151   if ((displayed_coins == DISPLAYED_COINS_UNSET) ||
152       (fabsf(displayed_coins - coins) > 100)) {
153     displayed_coins = coins;
154     displayed_coins_frame = 0;
155   }
156   if (++displayed_coins_frame > 2) {
157     displayed_coins_frame = 0;
158     if (displayed_coins < coins) displayed_coins++;
159     if (displayed_coins > coins) displayed_coins--;
160   }
161   displayed_coins = std::min(std::max(displayed_coins, 0), 9999);
162
163   std::stringstream ss;
164   ss << displayed_coins;
165   std::string coins_text = ss.str();
166
167   context.push_transform();
168   context.set_translation(Vector(0, 0));
169
170   if (coin_surface)
171   {
172     context.draw_surface(coin_surface,
173                          Vector(SCREEN_WIDTH - BORDER_X - coin_surface->get_width() - Resources::fixed_font->get_text_width(coins_text),
174                                 BORDER_Y + 1 + (Resources::fixed_font->get_text_height(coins_text) + 5) * player_id),
175                          LAYER_HUD);
176   }
177   context.draw_text(Resources::fixed_font,
178                     coins_text,
179                     Vector(SCREEN_WIDTH - BORDER_X - Resources::fixed_font->get_text_width(coins_text),
180                            BORDER_Y + (Resources::fixed_font->get_text_height(coins_text) + 5) * player_id),
181                     ALIGN_LEFT,
182                     LAYER_HUD,
183                     PlayerStatus::text_color);
184
185   context.pop_transform();
186 }
187
188 /* EOF */