- Worldmap scripts have their own roottable now (like sectors already have)
[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 "lisp/writer.hpp"
23 #include "lisp/lisp.hpp"
24 #include "player_status.hpp"
25 #include "resources.hpp"
26 #include "gettext.hpp"
27 #include "video/drawing_context.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "sprite/sprite_manager.hpp"
30 #include "math/vector.hpp"
31 #include "main.hpp"
32 #include "log.hpp"
33
34 static const int START_COINS = 100;
35 static const int MAX_COINS = 99999;
36
37 PlayerStatus* player_status = 0;
38
39 PlayerStatus::PlayerStatus()
40   : coins(START_COINS),
41     bonus(NO_BONUS),
42     max_fire_bullets(0),
43     max_ice_bullets(0),
44     score_multiplier(1),
45     max_score_multiplier(1)
46 {
47   reset();
48   key_brass.reset(sprite_manager->create("images/objects/keys/key_brass.sprite"));
49   key_iron.reset(sprite_manager->create("images/objects/keys/key_iron.sprite"));
50   key_bronze.reset(sprite_manager->create("images/objects/keys/key_bronze.sprite"));
51   key_silver.reset(sprite_manager->create("images/objects/keys/key_silver.sprite"));
52   key_gold.reset(sprite_manager->create("images/objects/keys/key_gold.sprite"));
53   key_brass->set_action("outline");
54   key_iron->set_action("outline");
55   key_bronze->set_action("outline");
56   key_silver->set_action("outline");
57   key_gold->set_action("outline");
58   
59   tux_life.reset(sprite_manager->create("images/creatures/tux_small/tux-life.sprite"));
60
61   Console::instance->registerCommand("coins", this);
62 }
63
64 PlayerStatus::~PlayerStatus()
65 {
66 }
67
68 void PlayerStatus::reset()
69 {
70   coins = START_COINS;
71   keys = 0;
72   bonus = NO_BONUS;
73   score_multiplier = 1;
74   max_score_multiplier = 1;
75 }
76
77 void
78 PlayerStatus::add_coins(int count)
79 {
80   coins = std::min(coins + count, MAX_COINS);
81   if(count >= 100)
82     sound_manager->play("sounds/lifeup.wav");
83   else
84     sound_manager->play("sounds/coin.wav");
85 }
86
87 void
88 PlayerStatus::set_keys(int new_key)
89 {
90   keys |= new_key;
91   key_brass->set_action(keys & KEY_BRASS ? "display" : "outline");
92   key_iron->set_action(keys & KEY_IRON ? "display" : "outline");
93   key_bronze->set_action(keys & KEY_BRONZE ? "display" : "outline");
94   key_silver->set_action(keys & KEY_SILVER ? "display" : "outline");
95   key_gold->set_action(keys & KEY_GOLD ? "display" : "outline");
96 }
97
98 void
99 PlayerStatus::write(lisp::Writer& writer)
100 {
101   switch(bonus) {
102     case NO_BONUS:
103       writer.write_string("bonus", "none");
104       break;
105     case GROWUP_BONUS:
106       writer.write_string("bonus", "growup");
107       break;
108     case FIRE_BONUS:
109       writer.write_string("bonus", "fireflower");
110       break;
111     case ICE_BONUS:
112       writer.write_string("bonus", "iceflower");
113       break;
114     default:
115       log_warning << "Unknown bonus type." << std::endl;
116       writer.write_string("bonus", "none");
117   }
118   writer.write_int("fireflowers", max_fire_bullets);
119   writer.write_int("iceflowers", max_ice_bullets);
120   
121   writer.write_bool("key-brass", keys & KEY_BRASS);
122   writer.write_bool("key-iron", keys & KEY_IRON);
123   writer.write_bool("key-bronze", keys & KEY_BRONZE);
124   writer.write_bool("key-silver", keys & KEY_SILVER);
125   writer.write_bool("key-gold", keys & KEY_GOLD);
126
127   writer.write_int("coins", coins);
128   writer.write_int("max-score-multiplier", max_score_multiplier);
129 }
130
131 void
132 PlayerStatus::read(const lisp::Lisp& lisp)
133 {
134   reset();
135   
136   std::string bonusname;
137   if(lisp.get("bonus", bonusname)) {
138     if(bonusname == "none") {
139       bonus = NO_BONUS;
140     } else if(bonusname == "growup") {
141       bonus = GROWUP_BONUS;
142     } else if(bonusname == "fireflower") {
143       bonus = FIRE_BONUS;
144     } else if(bonusname == "iceflower") {
145       bonus = ICE_BONUS;
146     } else {
147       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
148       bonus = NO_BONUS;
149     }
150   }
151   lisp.get("fireflowers", max_fire_bullets);
152   lisp.get("iceflowers", max_ice_bullets);
153
154   bool val = false;
155   if(lisp.get("key-brass", val) && val == true)
156     set_keys(KEY_BRASS);
157   if(lisp.get("key-iron", val) && val == true)
158     set_keys(KEY_IRON);
159   if(lisp.get("key-bronze", val) && val == true)
160     set_keys(KEY_BRONZE);
161   if(lisp.get("key-silver", val) && val == true)
162     set_keys(KEY_SILVER);
163   if(lisp.get("key-gold", val) && val == true)
164     set_keys(KEY_GOLD);
165
166   lisp.get("coins", coins);
167   lisp.get("max-score-multiplier", max_score_multiplier);
168 }
169
170 void
171 PlayerStatus::draw_keys(DrawingContext& context)
172 {
173   const float SPACING = 10;
174   float x,y; 
175   x = BORDER_X; y = BORDER_Y;
176   key_brass->draw(context, Vector(x, y), LAYER_FOREGROUND1);
177   x += key_brass->get_width() + SPACING;
178   key_iron->draw(context, Vector(x, y), LAYER_FOREGROUND1);
179   x += key_iron->get_width() + SPACING;
180   key_bronze->draw(context, Vector(x, y), LAYER_FOREGROUND1);
181   x += key_bronze->get_width() + SPACING;
182   key_silver->draw(context, Vector(x, y), LAYER_FOREGROUND1);
183   x += key_silver->get_width() + SPACING;
184   key_gold->draw(context, Vector(x, y), LAYER_FOREGROUND1);
185   x += key_gold->get_width() + SPACING;
186 }
187
188 void
189 PlayerStatus::draw(DrawingContext& context)
190 {
191   context.push_transform();
192   context.set_translation(Vector(0, 0));
193
194   char str[60];
195  
196   int displayCoins = std::max(player_status->coins, 0);
197   sprintf(str, "%d", displayCoins);
198   const char* coinstext = _("COINS");
199   context.draw_text(white_text, coinstext, Vector(SCREEN_WIDTH - white_text->get_text_width(coinstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y), LEFT_ALLIGN, LAYER_FOREGROUND1);
200   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), RIGHT_ALLIGN, LAYER_FOREGROUND1);
201
202   //draw_keys(context);  
203
204   context.pop_transform();
205 }
206
207 void
208 PlayerStatus::operator= (const PlayerStatus& other)
209 {
210   coins = other.coins;
211   bonus = other.bonus;
212   score_multiplier = other.score_multiplier;
213   max_score_multiplier = other.max_score_multiplier;
214   keys = other.keys;
215 }
216
217 bool
218 PlayerStatus::consoleCommand(std::string command, std::vector<std::string> arguments)
219 {
220   if (command == "coins") {
221     if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
222       log_info << "Usage: coins <number>" << std::endl;
223     } else {
224       coins = Console::string_to<int>(arguments[0]);
225     }
226     return true;
227   }
228   return false;
229 }
230