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