More code cleanups. Fixed a few bugs.
[supertux.git] / src / world.c
1 //
2 // C Implementation: world
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 <string.h>
15 #include "globals.h"
16 #include "scene.h"
17 #include "screen.h"
18 #include "defines.h"
19 #include "world.h"
20
21
22 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
23 {
24       pbouncy_distro->base.alive = YES;
25       pbouncy_distro->base.x = x;
26       pbouncy_distro->base.y = y;
27       pbouncy_distro->base.ym = -6;
28 }
29       
30 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
31 {
32       if (pbouncy_distro->base.alive)
33         {
34           pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym;
35
36           pbouncy_distro->base.ym++;
37
38           if (pbouncy_distro->base.ym >= 0)
39             pbouncy_distro->base.alive = NO;
40         }
41 }
42
43 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
44 {
45       if (pbouncy_distro->base.alive)
46         {
47           texture_draw(&img_distro[0],
48                     pbouncy_distro->base.x - scroll_x,
49                     pbouncy_distro->base.y,
50                     NO_UPDATE);
51         }
52 }
53
54 void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym)
55 {
56       pbroken_brick->base.alive = YES;
57       pbroken_brick->base.x = x;
58       pbroken_brick->base.y = y;
59       pbroken_brick->base.xm = xm;
60       pbroken_brick->base.ym = ym;
61       timer_start(&pbroken_brick->timer,200);
62       pbroken_brick->base.updated = SDL_GetTicks();
63 }
64
65 void broken_brick_action(broken_brick_type* pbroken_brick)
66 {
67           double frame_ratio = get_frame_ratio(&pbroken_brick->base);
68           
69       if (pbroken_brick->base.alive)
70         {
71           pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
72           pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
73
74           if (!timer_check(&pbroken_brick->timer))
75             pbroken_brick->base.alive = NO;
76         }
77 }
78
79 void broken_brick_draw(broken_brick_type* pbroken_brick)
80 {
81       if (pbroken_brick->base.alive)
82         {
83           src.x = rand() % 16;
84           src.y = rand() % 16;
85           src.w = 16;
86           src.h = 16;
87
88           dest.x = pbroken_brick->base.x - scroll_x;
89           dest.y = pbroken_brick->base.y;
90           dest.w = 16;
91           dest.h = 16;
92
93           SDL_BlitSurface(img_brick[0].sdl_surface, &src, screen, &dest);
94         }
95 }
96
97 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
98 {
99       pbouncy_brick->base.alive = YES;
100       pbouncy_brick->base.x = x;
101       pbouncy_brick->base.y = y;
102       pbouncy_brick->offset = 0;
103       pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
104       pbouncy_brick->shape = shape(x, y);
105       pbouncy_brick->base.updated = SDL_GetTicks();
106 }
107
108 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
109 {
110         
111           double frame_ratio = get_frame_ratio(&pbouncy_brick->base);
112           
113       if (pbouncy_brick->base.alive)
114         {
115           
116           pbouncy_brick->offset = (pbouncy_brick->offset +
117                                      pbouncy_brick->offset_m * frame_ratio);
118
119           /* Go back down? */
120
121           if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
122             pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
123
124
125           /* Stop bouncing? */
126
127           if (pbouncy_brick->offset >= 0)
128             pbouncy_brick->base.alive = NO;
129         }
130 }
131
132 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
133 {
134       if (pbouncy_brick->base.alive)
135         {
136           if (pbouncy_brick->base.x >= scroll_x - 32 &&
137               pbouncy_brick->base.x <= scroll_x + screen->w)
138             {
139               dest.x = pbouncy_brick->base.x - scroll_x;
140               dest.y = pbouncy_brick->base.y;
141               dest.w = 32;
142               dest.h = 32;
143
144               SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format,
145                                                      current_level.bkgd_red,
146                                                      current_level.bkgd_green,
147                                                      current_level.bkgd_blue));
148
149               drawshape(pbouncy_brick->base.x - scroll_x,
150                         pbouncy_brick->base.y + pbouncy_brick->offset,
151                         pbouncy_brick->shape);
152             }
153         }
154 }
155
156 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
157 {
158       pfloating_score->base.alive = YES;
159       pfloating_score->base.x = x;
160       pfloating_score->base.y = y - 16;
161       timer_start(&pfloating_score->timer,1000);
162       pfloating_score->value = s;
163       pfloating_score->base.updated = SDL_GetTicks();
164
165 }
166
167 void floating_score_action(floating_score_type* pfloating_score)
168 {
169 double frame_ratio = get_frame_ratio(&pfloating_score->base);
170           
171       if (pfloating_score->base.alive)
172         {
173           pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
174
175       if(!timer_check(&pfloating_score->timer))
176           pfloating_score->base.alive = NO;
177         }
178 }
179
180 void floating_score_draw(floating_score_type* pfloating_score)
181 {
182       if (pfloating_score->base.alive)
183         {
184         char str[10];
185           sprintf(str, "%d", pfloating_score->value);
186           drawtext(str,
187                    pfloating_score->base.x + 16 - strlen(str) * 8,
188                    pfloating_score->base.y,
189                    letters_gold, NO_UPDATE, 1);
190         }
191 }
192