applied Ricardo's upgrade direction patch again.
[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, YES);
78
79   counting_distros = NO;
80   distro_counter = 0;
81
82   endpos = 0;
83
84   /* set current song/music */
85   set_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   int i, found;
129
130   found = -1;
131
132   for (i = 0; i < num_bouncy_distros && found == -1; i++)
133     {
134       if (!bouncy_distros[i].base.alive)
135         found = i;
136     }
137     
138   if (found == -1)
139   {
140   ++num_bouncy_distros;
141   bouncy_distros = (bouncy_distro_type*) realloc(bouncy_distros,num_bouncy_distros*sizeof(bouncy_distro_type));
142   found = num_bouncy_distros - 1;
143   }
144     
145   if (found != -1)
146     {
147         bouncy_distro_init(&bouncy_distros[found],x,y);
148     }
149 }
150
151
152 /* Add broken brick pieces: */
153
154 void add_broken_brick(float x, float y)
155 {
156   add_broken_brick_piece(x, y, -1, -4);
157   add_broken_brick_piece(x, y + 16, -1.5, -3);
158
159   add_broken_brick_piece(x + 16, y, 1, -4);
160   add_broken_brick_piece(x + 16, y + 16, 1.5, -3);
161 }
162
163
164 /* Add a broken brick piece: */
165
166 void add_broken_brick_piece(float x, float y, float xm, float ym)
167 {
168   int i, found;
169
170   found = -1;
171
172   for (i = 0; i < num_broken_bricks && found == -1; i++)
173     {
174       if (!broken_bricks[i].base.alive)
175         found = i;
176     }
177
178   if (found == -1)
179   {
180   ++num_broken_bricks;
181   broken_bricks = (broken_brick_type*) realloc(broken_bricks,num_broken_bricks*sizeof(broken_brick_type));
182   found = num_broken_bricks - 1;
183   }
184
185   if (found != -1)
186     {
187        broken_brick_init(&broken_bricks[found], x, y, xm, ym);
188     }
189 }
190
191
192 /* Add a bouncy brick piece: */
193
194 void add_bouncy_brick(float x, float y)
195 {
196   int i, found;
197
198   found = -1;
199
200   for (i = 0; i < num_bouncy_bricks && found == -1; i++)
201     {
202       if (!bouncy_bricks[i].base.alive)
203         found = i;
204     }
205     
206   if (found == -1)
207   {
208   ++num_bouncy_bricks;
209   bouncy_bricks = (bouncy_brick_type*) realloc(bouncy_bricks,num_bouncy_bricks*sizeof(bouncy_brick_type));
210   found = num_bouncy_bricks - 1;
211   }
212
213   if (found != -1)
214     {
215       bouncy_brick_init(&bouncy_bricks[found],x,y);
216     }
217 }
218
219
220 /* Add a bad guy: */
221
222 void add_bad_guy(float x, float y, int kind)
223 {
224   int i, found;
225
226   found = -1;
227
228   for (i = 0; i < num_bad_guys && found == -1; i++)
229     {
230       if (!bad_guys[i].base.alive)
231         found = i;
232     }
233  
234   if (found == -1)
235   {
236   ++num_bad_guys;
237   bad_guys = (bad_guy_type*) realloc(bad_guys,num_bad_guys*sizeof(bad_guy_type));
238   found = num_bad_guys - 1;
239   }
240
241   if (found != -1)
242     {
243        badguy_init(&bad_guys[found], x, y, kind);
244     }
245 }
246
247 /* Add an upgrade: */
248
249 void add_upgrade(float x, float y, int dir, int kind)
250 {
251   int i, found;
252
253   found = -1;
254
255   for (i = 0; i < num_upgrades && found == -1; i++)
256     {
257       if (!upgrades[i].base.alive)
258         found = i;
259     }
260
261   if (found == -1)
262   {
263   ++num_upgrades;
264   upgrades = (upgrade_type*) realloc(upgrades,num_upgrades*sizeof(upgrade_type));
265   found = num_upgrades - 1;
266   }
267
268   if (found != -1)
269     {
270       upgrade_init(&upgrades[found], x, y, dir, kind);
271     }
272 }
273
274 /* Add a bullet: */
275
276 void add_bullet(float x, float y, float xm, int dir)
277 {
278   int i, found;
279   
280   found = -1;
281
282   for (i = 0; i < num_bullets && found == -1; i++)
283     {
284       if (!bullets[i].base.alive)
285         found = i;
286     }
287
288   if (found == -1)
289   {
290   ++num_bullets;
291   bullets = (bullet_type*) realloc(bullets,num_bullets*sizeof(bullet_type));
292   found = num_bullets - 1;
293   }
294
295   if (found != -1)
296     {
297       bullet_init(&bullets[found], x, y, xm, dir);
298
299       play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
300     }
301 }
302