gameplay improvements
[supertux.git] / src / scene.c
1 //
2 // C Implementation: scene
3 //
4 // Description: 
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include "scene.h"
15
16 int score, distros, level, next_level, game_pause, quit, score_multiplier, endpos, counting_distros, distro_counter;
17 timer_type  super_bkgd_timer;
18 float scroll_x;
19 int frame;
20 bouncy_distro_type *bouncy_distros;
21 broken_brick_type *broken_bricks;
22 bouncy_brick_type *bouncy_bricks;
23 bad_guy_type *bad_guys;
24 floating_score_type *floating_scores;
25 upgrade_type *upgrades;
26 bullet_type *bullets;
27 int num_bad_guys;
28 int num_bouncy_distros;
29 int num_broken_bricks;
30 int num_bouncy_bricks;
31 int num_floating_scores;
32 int num_upgrades;
33 int num_bullets;
34 player_type tux;
35 texture_type img_box_full, img_box_empty, img_mints, img_coffee, img_super_bkgd, img_red_glow;
36 timer_type time_left;
37 double frame_ratio;
38
39 /* Initialize all 'dynamic' arrays */
40 void arrays_init(void)
41 {
42 num_bad_guys = 0;
43 num_bouncy_distros = 0;
44 num_broken_bricks = 0;
45 num_bouncy_bricks = 0;
46 num_floating_scores = 0;
47 num_upgrades = 0;
48 num_bullets = 0;
49 bad_guys = NULL;
50 bouncy_distros = NULL;
51 broken_bricks = NULL;
52 bouncy_bricks = NULL;
53 floating_scores = NULL;
54 upgrades = NULL;
55 bullets = NULL;
56 }
57
58 /* Free memory of 'dynamic' arrays */
59 void arrays_free(void)
60 {
61 free(bad_guys);
62 free(bouncy_distros);
63 free(broken_bricks);
64 free(bouncy_bricks);
65 free(floating_scores);
66 free(upgrades);
67 free(bullets);
68 }
69
70 void set_defaults(void)
71 {
72   /* Set defaults: */
73   
74   scroll_x = 0;
75
76   score_multiplier = 1;
77   timer_init(&super_bkgd_timer);
78
79   counting_distros = NO;
80   distro_counter = 0;
81
82   endpos = 0;
83
84   /* set current song/music */
85   current_music = LEVEL_MUSIC;
86 }
87
88 /* Add score: */
89
90 void add_score(float x, float y, int s)
91 {
92   int i, found;
93
94
95   /* Add the score: */
96
97   score += s;
98
99
100   /* Add a floating score thing to the game: */
101
102   found = -1;
103
104   for (i = 0; i < num_floating_scores && found == -1; i++)
105     {
106       if (!floating_scores[i].base.alive)
107         found = i;
108     }
109     
110   if (found == -1)
111   {
112   ++num_floating_scores;
113   floating_scores = (floating_score_type*) realloc(floating_scores,num_floating_scores*sizeof(floating_score_type));
114   floating_score_init(&floating_scores[num_floating_scores-1],x,y,s);
115   found = -1;
116   }
117
118   if (found != -1)
119     {
120         floating_score_init(&floating_scores[found],x,y,s);
121     }
122 }
123
124 /* Add a bouncy distro: */
125
126 void add_bouncy_distro(float x, float y)
127 {
128
129
130 printf("X _--- %f Y-- isdojf %f\n",x,y);
131
132   int i, found;
133
134   found = -1;
135
136   for (i = 0; i < num_bouncy_distros && found == -1; i++)
137     {
138       if (!bouncy_distros[i].base.alive)
139         found = i;
140     }
141     
142   if (found == -1)
143   {
144   ++num_bouncy_distros;
145   bouncy_distros = (bouncy_distro_type*) realloc(bouncy_distros,num_bouncy_distros*sizeof(bouncy_distro_type));
146   found = num_bouncy_distros - 1;
147   }
148     
149   if (found != -1)
150     {
151         bouncy_distro_init(&bouncy_distros[found],x,y);
152     }
153 }
154
155
156 /* Add broken brick pieces: */
157
158 void add_broken_brick(float x, float y)
159 {
160   add_broken_brick_piece(x, y, -1, -4);
161   add_broken_brick_piece(x, y + 16, -1.5, -3);
162
163   add_broken_brick_piece(x + 16, y, 1, -4);
164   add_broken_brick_piece(x + 16, y + 16, 1.5, -3);
165 }
166
167
168 /* Add a broken brick piece: */
169
170 void add_broken_brick_piece(float x, float y, float xm, float ym)
171 {
172   int i, found;
173
174   found = -1;
175
176   for (i = 0; i < num_broken_bricks && found == -1; i++)
177     {
178       if (!broken_bricks[i].base.alive)
179         found = i;
180     }
181
182   if (found == -1)
183   {
184   ++num_broken_bricks;
185   broken_bricks = (broken_brick_type*) realloc(broken_bricks,num_broken_bricks*sizeof(broken_brick_type));
186   found = num_broken_bricks - 1;
187   }
188
189   if (found != -1)
190     {
191        broken_brick_init(&broken_bricks[found], x, y, xm, ym);
192     }
193 }
194
195
196 /* Add a bouncy brick piece: */
197
198 void add_bouncy_brick(float x, float y)
199 {
200   int i, found;
201
202   found = -1;
203
204   for (i = 0; i < num_bouncy_bricks && found == -1; i++)
205     {
206       if (!bouncy_bricks[i].base.alive)
207         found = i;
208     }
209     
210   if (found == -1)
211   {
212   ++num_bouncy_bricks;
213   bouncy_bricks = (bouncy_brick_type*) realloc(bouncy_bricks,num_bouncy_bricks*sizeof(bouncy_brick_type));
214   found = num_bouncy_bricks - 1;
215   }
216
217   if (found != -1)
218     {
219       bouncy_brick_init(&bouncy_bricks[found],x,y);
220     }
221 }
222
223
224 /* Add a bad guy: */
225
226 void add_bad_guy(float x, float y, int kind)
227 {
228   int i, found;
229
230   found = -1;
231
232   for (i = 0; i < num_bad_guys && found == -1; i++)
233     {
234       if (!bad_guys[i].base.alive)
235         found = i;
236     }
237  
238   if (found == -1)
239   {
240   ++num_bad_guys;
241   bad_guys = (bad_guy_type*) realloc(bad_guys,num_bad_guys*sizeof(bad_guy_type));
242   found = num_bad_guys - 1;
243   }
244
245   if (found != -1)
246     {
247        badguy_init(&bad_guys[found], x, y, kind);
248     }
249 }
250
251 /* Add an upgrade: */
252
253 void add_upgrade(float x, float y, int kind)
254 {
255   int i, found;
256
257   found = -1;
258
259   for (i = 0; i < num_upgrades && found == -1; i++)
260     {
261       if (!upgrades[i].base.alive)
262         found = i;
263     }
264
265   if (found == -1)
266   {
267   ++num_upgrades;
268   upgrades = (upgrade_type*) realloc(upgrades,num_upgrades*sizeof(upgrade_type));
269   found = num_upgrades - 1;
270   }
271
272   if (found != -1)
273     {
274       upgrade_init(&upgrades[found], x, y, kind);
275     }
276 }
277
278 /* Add a bullet: */
279
280 void add_bullet(float x, float y, float xm, int dir)
281 {
282   int i, found;
283   
284   found = -1;
285
286   for (i = 0; i < num_bullets && found == -1; i++)
287     {
288       if (!bullets[i].base.alive)
289         found = i;
290     }
291
292   if (found == -1)
293   {
294   ++num_bullets;
295   bullets = (bullet_type*) realloc(bullets,num_bullets*sizeof(bullet_type));
296   found = num_bullets - 1;
297   }
298
299   if (found != -1)
300     {
301       bullet_init(&bullets[found], x, y, xm, dir);
302
303       play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
304     }
305 }
306