1 /**
2  * @file controller_indicators.cc
3  * @brief Controller of money amount, reverse penalty
4  * @created 2002-11-28
5  * @date 2007-10-21
6  * @copyright 1991-2014 TLK Games
7  * @author Bruno Ethvignot
8  * @version $Revision: 24 $
9  */
10 /*
11  * copyright (c) 1991-2014 TLK Games all rights reserved
12  * $Id: controller_indicators.cc 24 2014-09-28 15:30:04Z bruno.ethvignot@gmail.com $
13  *
14  * TecnoballZ is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * TecnoballZ is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA  02110-1301, USA.
28  */
29 #include "../include/controller_indicators.h"
30 
31 /**
32  * Create the indicators controller
33  */
controller_indicators()34 controller_indicators::controller_indicators ()
35 {
36   littleInit ();
37   max_of_sprites = 8;
38   sprites_have_shades = false;
39   y_position = YCOORD_INDICATORS * resolution;
40   sprite_type_id = sprite_object::FONT_MONEY;
41   money_posy = y_position;
42   life_sprite = NULL;
43   reverse_sprite = NULL;
44   money_sprite = NULL;
45 }
46 
47 /**
48  * Release the indicators controller
49  */
~controller_indicators()50 controller_indicators::~controller_indicators ()
51 {
52   if (NULL != life_sprite)
53     {
54       delete life_sprite;
55       life_sprite = NULL;
56     }
57   if (NULL != reverse_sprite)
58     {
59       delete reverse_sprite;
60       reverse_sprite = NULL;
61     }
62   if (NULL != money_sprite)
63     {
64       delete money_sprite;
65       money_sprite = NULL;
66     }
67   release_sprites_list ();
68 }
69 
70 void
create_indicators_sprites()71 controller_indicators::create_indicators_sprites ()
72 {
73 
74   create_sprites_list ();
75 
76   /*
77    * money sprite indicator
78    */
79   controller_moneys *moneys = controller_moneys::get_instance ();
80   sprite_money *money = moneys->get_first_sprite ();
81   money_sprite = new sprite_money ();
82   money->duplicate_to (money_sprite);
83   money_sprite->set_shadow (false);
84   sprites->add (money_sprite);
85   init_money ();
86 
87   /*
88    * reverse sprite is only enable in the bricks levels
89    */
90   controller_capsules *capsules = controller_capsules::get_instance ();
91   sprite_capsule *caps = capsules->get_first_sprite ();
92   if (current_phase == BRICKS_LEVEL)
93     {
94       reverse_sprite = new sprite_capsule ();
95       caps->duplicate_to (reverse_sprite);
96       reverse_sprite->set_shadow (false);
97       sprites->add (reverse_sprite);
98 
99       Sint32 x = 215 * resolution;
100       reverse_sprite->enable_indicator_capsule (sprite_capsule::INVERSE_CONTROL);
101       reverse_sprite->set_coordinates (x, money_posy);
102       reverse_sprite->set_frame_delay (5);
103       x += reverse_sprite->get_sprite_width ();
104       sprite_object **liste = sprites_list + 6;
105       for (Uint32 i = 0; i < 2; i++)
106         {
107           sprite_object *x_bob = *(liste++);
108           x_bob->set_coordinates (x, money_posy);
109           x += 8 * resolution;
110         }
111     }
112   /*
113    * life sprite is only enable in the guards levels
114    */
115   if (current_phase == GUARDS_LEVEL)
116     {
117       life_sprite = new sprite_capsule ();
118       caps->duplicate_to (life_sprite);
119       sprites->add (life_sprite);
120       life_sprite->set_shadow (false);
121       Sint32 x = 264 * resolution;
122       life_sprite->enable_indicator_capsule (sprite_capsule::EXTRA_LIFE);
123       life_sprite->set_coordinates (x, money_posy);
124       life_sprite->set_frame_delay (5);
125       x += life_sprite->get_sprite_width ();
126       sprite_object **liste = sprites_list + 6;
127       for (Uint32 i = 0; i < 2; i++)
128         {
129           sprite_object *x_bob = *(liste++);
130           x_bob->set_coordinates (x, money_posy);
131           x += 8 * resolution;
132           x_bob->enable ();
133         }
134     }
135 }
136 
137 /**
138  * initialize credit of money sprites
139  */
140 void
init_money()141 controller_indicators::init_money ()
142 {
143   Uint32 x = 24 * resolution;
144   money_sprite->set_coordinates (x, money_posy);
145   money_sprite->set_frame_delay (5);
146   money_sprite->enable ();
147   x += money_sprite->get_sprite_width ();
148 
149   /* chars sprites */
150   sprite_object **liste = sprites_list;
151   for (Sint32 i = 0; i < 6; i++)
152     {
153       sprite_object *x_bob = *(liste++);
154       x_bob->set_coordinates (x, money_posy);
155       x_bob->enable ();
156       x += 8 * resolution;
157     }
158 }
159 
160 /**
161  * Display amount of money and reverse penalty
162  */
163 void
display_money_and_reverse()164 controller_indicators::display_money_and_reverse ()
165 {
166   display_money_amount ();
167   controller_paddles* paddles = controller_paddles::get_instance ();
168 
169   /* display reverse penalty if enable */
170   sprite_object **chars = sprites_list + 6;
171   Uint32 counter = paddles->get_reverse_counter ();
172   if (counter > 0)
173     {
174       counter--;
175       paddles->set_reverse_counter (counter);
176       counter = counter / 2;
177       Uint32 digits = 10;
178       while (digits > 0)
179         {
180           Uint32 i = 0;
181           while (counter >= digits)
182             {
183               counter -= digits;
184               i++;
185             }
186           digits /= 10;
187           sprite_object *character = *(chars++);
188           character->set_image (i);
189           character->enable ();
190         }
191       reverse_sprite->enable ();
192       reverse_sprite->play_animation_loop ();
193     }
194   else
195     {
196       for (Sint32 i = 0; i < 2; i++)
197         {
198           sprite_object *character = *(chars++);
199           character->disable ();
200         }
201       reverse_sprite->disable ();
202     }
203 }
204 
205 /**
206  * Display amount of money and number of lifes
207  */
208 void
display_money_and_lifes()209 controller_indicators::display_money_and_lifes ()
210 {
211   display_money_amount ();
212   Uint32 lifes = current_player->number_of_lifes;
213   Uint32 digits = 10;
214   sprite_object **chars = sprites_list + 6;
215   while (digits > 0)
216     {
217       Sint32 i = 0;
218       while (lifes >= digits)
219         {
220           lifes -= digits;
221           i++;
222         }
223       digits /= 10;
224       sprite_object *character = *(chars++);
225       character->set_image (i);
226     }
227   life_sprite->play_animation_loop ();
228 }
229 
230 /**
231  * Display amount of money
232  */
233 void
display_money_amount()234 controller_indicators::display_money_amount ()
235 {
236   Uint32 amount = current_player->amount_of_money;
237   Uint32 digits = 100000;
238   sprite_object **chars = sprites_list;
239   while (digits > 0)
240     {
241       Uint32 i = 0;
242       while (amount >= digits)
243         {
244           amount -= digits;
245           i++;
246         }
247       digits /= 10;
248       sprite_object *character = *(chars++);
249       character->set_y_coord (money_posy);
250       character->set_image (i);
251     }
252   money_sprite->set_y_coord (money_posy);
253   money_sprite->play_animation_loop ();
254   if (money_posy < y_position)
255     {
256       money_posy += resolution;
257     }
258 }
259 
260 /**
261  * Increase the amount of money
262  * @param value money amount, 10, 20, 30, ...
263  */
264 void
increase_money_amount(Uint32 value)265 controller_indicators::increase_money_amount (Uint32 value)
266 {
267   money_posy = y_position - 5 * resolution;
268   current_player->amount_of_money += value;
269   if (current_player->area_number >= 3)
270     {
271       current_player->amount_of_money += value;
272     }
273   if (current_player->area_number >= 5)
274     {
275       current_player->amount_of_money += value;
276     }
277 }
278