bouncy bricks are drawn correctly now, if a level has a background image.
[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       texture_draw_part(&img_brick[0],src.x,src.y,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
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   int s;
129
130   if (pbouncy_brick->base.alive)
131     {
132       if (pbouncy_brick->base.x >= scroll_x - 32 &&
133           pbouncy_brick->base.x <= scroll_x + screen->w)
134         {
135           dest.x = pbouncy_brick->base.x - scroll_x;
136           dest.y = pbouncy_brick->base.y;
137           dest.w = 32;
138           dest.h = 32;
139
140           if(current_level.bkgd_image[0] == '\0')
141             {
142               fillrect(pbouncy_brick->base.x - scroll_x,pbouncy_brick->base.y,32,32,current_level.bkgd_red,current_level.bkgd_green,
143                        current_level.bkgd_blue);
144             }
145           else
146             {
147               s = scroll_x / 30;
148               texture_draw_part(&img_bkgd,dest.x + s,dest.y,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
149             }
150
151           drawshape(pbouncy_brick->base.x - scroll_x,
152                     pbouncy_brick->base.y + pbouncy_brick->offset,
153                     pbouncy_brick->shape);
154         }
155     }
156 }
157
158 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
159 {
160   pfloating_score->base.alive = YES;
161   pfloating_score->base.x = x;
162   pfloating_score->base.y = y - 16;
163   timer_start(&pfloating_score->timer,1000);
164   pfloating_score->value = s;
165 }
166
167 void floating_score_action(floating_score_type* pfloating_score)
168 {
169   if (pfloating_score->base.alive)
170     {
171       pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
172
173       if(!timer_check(&pfloating_score->timer))
174         pfloating_score->base.alive = NO;
175     }
176 }
177
178 void floating_score_draw(floating_score_type* pfloating_score)
179 {
180   if (pfloating_score->base.alive)
181     {
182       char str[10];
183       sprintf(str, "%d", pfloating_score->value);
184       text_draw(&gold_text, str, pfloating_score->base.x + 16 - strlen(str) * 8, pfloating_score->base.y, 1, NO_UPDATE);
185     }
186 }
187