added new class for particle systems that need absolute coordinates (currently only...
[supertux.git] / src / player_status.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include "lisp/writer.h"
22 #include "lisp/lisp.h"
23 #include "player_status.h"
24 #include "resources.h"
25 #include "gettext.h"
26 #include "video/drawing_context.h"
27 #include "main.h"
28
29 static const int START_LIVES = 4;
30 static const int MAX_LIVES = 99;
31
32 PlayerStatus player_status;
33
34 PlayerStatus::PlayerStatus()
35   : coins(0),
36     lives(START_LIVES),
37     bonus(NO_BONUS),
38     score_multiplier(1),
39     max_score_multiplier(1)
40 {
41 }
42
43 void PlayerStatus::reset()
44 {
45   coins = 0;
46   lives = START_LIVES;
47   bonus = NO_BONUS;
48   score_multiplier = 1;
49   max_score_multiplier = 1;
50 }
51
52 void
53 PlayerStatus::incLives()
54 {
55   if(lives < MAX_LIVES)
56     ++lives;
57   sound_manager->play_sound("lifeup");
58 }
59
60 void
61 PlayerStatus::incCoins()
62 {
63   coins++;
64   if(coins >= 100) {
65     incLives();
66     coins = 0;
67   }
68   sound_manager->play_sound("coin");
69 }
70
71 void
72 PlayerStatus::write(lisp::Writer& writer)
73 {
74   switch(bonus) {
75     case NO_BONUS:
76       writer.write_string("bonus", "none");
77       break;
78     case GROWUP_BONUS:
79       writer.write_string("bonus", "growup");
80       break;
81     case FIRE_BONUS:
82       writer.write_string("bonus", "fireflower");
83       break;
84     case ICE_BONUS:
85       writer.write_string("bonus", "iceflower");
86       break;
87     default:
88       std::cerr << "Unknown bonus type.\n";
89       writer.write_string("bonus", "none");
90   }
91
92   writer.write_int("lives", lives);
93   writer.write_int("coins", coins);
94   writer.write_int("max-score-multiplier", max_score_multiplier);
95 }
96
97 void
98 PlayerStatus::read(const lisp::Lisp& lisp)
99 {
100   reset();
101   
102   std::string bonusname;
103   if(lisp.get("bonus", bonusname)) {
104     if(bonusname == "none") {
105       bonus = NO_BONUS;
106     } else if(bonusname == "growup") {
107       bonus = GROWUP_BONUS;
108     } else if(bonusname == "fireflower") {
109       bonus = FIRE_BONUS;
110     } else if(bonusname == "iceflower") {
111       bonus = ICE_BONUS;
112     } else {
113       std::cerr << "Unknown bonus '" << bonusname << "' in savefile.\n";
114       bonus = NO_BONUS;
115     }
116   }
117
118   lisp.get("lives", lives);
119   lisp.get("coins", coins);
120   lisp.get("max-score-multiplier", max_score_multiplier);
121 }
122
123 void
124 PlayerStatus::draw(DrawingContext& context)
125 {
126   context.push_transform();
127   context.set_translation(Vector(0, 0));
128
129   char str[60];
130   
131   sprintf(str, " %d", player_status.coins);
132   const char* coinstext = _("COINS");
133   context.draw_text(white_text, coinstext,
134       Vector(SCREEN_WIDTH - white_text->get_text_width(coinstext) 
135               - white_text->get_text_width("   99"), 0),
136       LEFT_ALLIGN, LAYER_FOREGROUND1);
137   context.draw_text(gold_text, str,
138       Vector(SCREEN_WIDTH - gold_text->get_text_width(" 99"), 0),
139       LEFT_ALLIGN, LAYER_FOREGROUND1);
140
141   if (player_status.lives >= 5) {
142     sprintf(str, "%dx", player_status.lives);
143     float x = SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w;
144     context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN,
145                       LAYER_FOREGROUND1);
146     context.draw_surface(tux_life, Vector(SCREEN_WIDTH - 16, 20),
147                          LAYER_FOREGROUND1);
148   } else {
149     for(int i= 0; i < player_status.lives; ++i)
150       context.draw_surface(tux_life, 
151           Vector(SCREEN_WIDTH - tux_life->w*4 +(tux_life->w*i), 20),
152           LAYER_FOREGROUND1);
153   }
154
155   const char* livestext = _("LIVES");
156   context.draw_text(white_text, livestext,
157       Vector(SCREEN_WIDTH - white_text->get_text_width(livestext) 
158                 - white_text->get_text_width("   99"), 20),
159       LEFT_ALLIGN, LAYER_FOREGROUND1);
160
161   context.pop_transform();
162 }