Yet another huge code merge representing the current status of my rewrite. It include...
[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 }
63
64 void broken_brick_action(broken_brick_type* pbroken_brick)
65 {
66       if (pbroken_brick->base.alive)
67         {
68           pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
69           pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
70
71           if (!timer_check(&pbroken_brick->timer))
72             pbroken_brick->base.alive = NO;
73         }
74 }
75
76 void broken_brick_draw(broken_brick_type* pbroken_brick)
77 {
78       if (pbroken_brick->base.alive)
79         {
80           src.x = rand() % 16;
81           src.y = rand() % 16;
82           src.w = 16;
83           src.h = 16;
84
85           dest.x = pbroken_brick->base.x - scroll_x;
86           dest.y = pbroken_brick->base.y;
87           dest.w = 16;
88           dest.h = 16;
89
90           SDL_BlitSurface(img_brick[0].sdl_surface, &src, screen, &dest);
91         }
92 }
93
94 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
95 {
96       pbouncy_brick->base.alive = YES;
97       pbouncy_brick->base.x = x;
98       pbouncy_brick->base.y = y;
99       pbouncy_brick->offset = 0;
100       pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
101       pbouncy_brick->shape = shape(x, y);
102 }
103
104 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
105 {
106
107       if (pbouncy_brick->base.alive)
108         {
109           
110           pbouncy_brick->offset = (pbouncy_brick->offset +
111                                      pbouncy_brick->offset_m * frame_ratio);
112
113           /* Go back down? */
114
115           if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
116             pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
117
118
119           /* Stop bouncing? */
120
121           if (pbouncy_brick->offset >= 0)
122             pbouncy_brick->base.alive = NO;
123         }
124 }
125
126 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
127 {
128       if (pbouncy_brick->base.alive)
129         {
130           if (pbouncy_brick->base.x >= scroll_x - 32 &&
131               pbouncy_brick->base.x <= scroll_x + screen->w)
132             {
133               dest.x = pbouncy_brick->base.x - scroll_x;
134               dest.y = pbouncy_brick->base.y;
135               dest.w = 32;
136               dest.h = 32;
137
138               fillrect(pbouncy_brick->base.x - scroll_x,pbouncy_brick->base.y,32,32,current_level.bkgd_red,current_level.bkgd_green,
139                                                      current_level.bkgd_blue);
140
141               drawshape(pbouncy_brick->base.x - scroll_x,
142                         pbouncy_brick->base.y + pbouncy_brick->offset,
143                         pbouncy_brick->shape);
144             }
145         }
146 }
147
148 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
149 {
150       pfloating_score->base.alive = YES;
151       pfloating_score->base.x = x;
152       pfloating_score->base.y = y - 16;
153       timer_start(&pfloating_score->timer,1000);
154       pfloating_score->value = s;
155 }
156
157 void floating_score_action(floating_score_type* pfloating_score)
158 {
159       if (pfloating_score->base.alive)
160         {
161           pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
162
163       if(!timer_check(&pfloating_score->timer))
164           pfloating_score->base.alive = NO;
165         }
166 }
167
168 void floating_score_draw(floating_score_type* pfloating_score)
169 {
170       if (pfloating_score->base.alive)
171         {
172         char str[10];
173           sprintf(str, "%d", pfloating_score->value);
174           text_draw(&gold_text, str, pfloating_score->base.x + 16 - strlen(str) * 8, pfloating_score->base.y, 1, NO_UPDATE);
175         }
176 }
177