make GameObjects reference counted (this avoids crashs when scripts hold reference...
[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   
49   tux_life.reset(sprite_manager->create("images/creatures/tux_small/tux-life.sprite"));
50
51   Console::instance->registerCommand("coins", this);
52 }
53
54 PlayerStatus::~PlayerStatus()
55 {
56 }
57
58 void PlayerStatus::reset()
59 {
60   coins = START_COINS;
61   bonus = NO_BONUS;
62   score_multiplier = 1;
63   max_score_multiplier = 1;
64 }
65
66 void
67 PlayerStatus::add_coins(int count)
68 {
69   coins = std::min(coins + count, MAX_COINS);
70   if(count >= 100)
71     sound_manager->play("sounds/lifeup.wav");
72   else
73     sound_manager->play("sounds/coin.wav");
74 }
75
76 void
77 PlayerStatus::write(lisp::Writer& writer)
78 {
79   switch(bonus) {
80     case NO_BONUS:
81       writer.write_string("bonus", "none");
82       break;
83     case GROWUP_BONUS:
84       writer.write_string("bonus", "growup");
85       break;
86     case FIRE_BONUS:
87       writer.write_string("bonus", "fireflower");
88       break;
89     case ICE_BONUS:
90       writer.write_string("bonus", "iceflower");
91       break;
92     default:
93       log_warning << "Unknown bonus type." << std::endl;
94       writer.write_string("bonus", "none");
95   }
96   writer.write_int("fireflowers", max_fire_bullets);
97   writer.write_int("iceflowers", max_ice_bullets);
98   
99   writer.write_int("coins", coins);
100   writer.write_int("max-score-multiplier", max_score_multiplier);
101 }
102
103 void
104 PlayerStatus::read(const lisp::Lisp& lisp)
105 {
106   reset();
107   
108   std::string bonusname;
109   if(lisp.get("bonus", bonusname)) {
110     if(bonusname == "none") {
111       bonus = NO_BONUS;
112     } else if(bonusname == "growup") {
113       bonus = GROWUP_BONUS;
114     } else if(bonusname == "fireflower") {
115       bonus = FIRE_BONUS;
116     } else if(bonusname == "iceflower") {
117       bonus = ICE_BONUS;
118     } else {
119       log_warning << "Unknown bonus '" << bonusname << "' in savefile" << std::endl;
120       bonus = NO_BONUS;
121     }
122   }
123   lisp.get("fireflowers", max_fire_bullets);
124   lisp.get("iceflowers", max_ice_bullets);
125
126   lisp.get("coins", coins);
127   lisp.get("max-score-multiplier", max_score_multiplier);
128 }
129
130 void
131 PlayerStatus::draw(DrawingContext& context)
132 {
133   context.push_transform();
134   context.set_translation(Vector(0, 0));
135
136   char str[60];
137  
138   int displayCoins = std::max(player_status->coins, 0);
139   sprintf(str, "%d", displayCoins);
140   const char* coinstext = _("COINS");
141   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);
142   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y), RIGHT_ALLIGN, LAYER_FOREGROUND1);
143
144   context.pop_transform();
145 }
146
147 void
148 PlayerStatus::operator= (const PlayerStatus& other)
149 {
150   coins = other.coins;
151   bonus = other.bonus;
152   score_multiplier = other.score_multiplier;
153   max_score_multiplier = other.max_score_multiplier;
154 }
155
156 bool
157 PlayerStatus::consoleCommand(std::string command, std::vector<std::string> arguments)
158 {
159   if (command == "coins") {
160     if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
161       log_info << "Usage: coins <number>" << std::endl;
162     } else {
163       coins = Console::string_to<int>(arguments[0]);
164     }
165     return true;
166   }
167   return false;
168 }
169