fixed save/load-game.
[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 texture_type img_distro[4];
22
23 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
24 {
25   pbouncy_distro->base.alive = YES;
26   pbouncy_distro->base.x = x;
27   pbouncy_distro->base.y = y;
28   pbouncy_distro->base.ym = -2;
29 }
30
31 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
32 {
33   if (pbouncy_distro->base.alive)
34     {
35       pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
36
37       pbouncy_distro->base.ym += 0.1 * frame_ratio;
38
39       if (pbouncy_distro->base.ym >= 0)
40         pbouncy_distro->base.alive = NO;
41     }
42 }
43
44 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
45 {
46   if (pbouncy_distro->base.alive)
47     {
48       texture_draw(&img_distro[0],
49                    pbouncy_distro->base.x - scroll_x,
50                    pbouncy_distro->base.y,
51                    NO_UPDATE);
52     }
53 }
54
55 void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym)
56 {
57   pbroken_brick->base.alive = YES;
58   pbroken_brick->base.x = x;
59   pbroken_brick->base.y = y;
60   pbroken_brick->base.xm = xm;
61   pbroken_brick->base.ym = ym;
62   timer_init(&pbroken_brick->timer,YES);
63   timer_start(&pbroken_brick->timer,200);
64 }
65
66 void broken_brick_action(broken_brick_type* pbroken_brick)
67 {
68   if (pbroken_brick->base.alive)
69     {
70       pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
71       pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
72
73       if (!timer_check(&pbroken_brick->timer))
74         pbroken_brick->base.alive = NO;
75     }
76 }
77
78 void broken_brick_draw(broken_brick_type* pbroken_brick)
79 {
80 SDL_Rect src, dest;
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 = (int)(pbroken_brick->base.x - scroll_x);
89       dest.y = (int)pbroken_brick->base.y;
90       dest.w = 16;
91       dest.h = 16;
92
93       texture_draw_part(&img_brick[0],src.x,src.y,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
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 }
106
107 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
108 {
109
110   if (pbouncy_brick->base.alive)
111     {
112
113       pbouncy_brick->offset = (pbouncy_brick->offset +
114                                pbouncy_brick->offset_m * frame_ratio);
115
116       /* Go back down? */
117
118       if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
119         pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
120
121
122       /* Stop bouncing? */
123
124       if (pbouncy_brick->offset >= 0)
125         pbouncy_brick->base.alive = NO;
126     }
127 }
128
129 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
130 {
131   int s;
132   SDL_Rect dest;
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 = (int)(pbouncy_brick->base.x - scroll_x);
140           dest.y = (int)pbouncy_brick->base.y;
141           dest.w = 32;
142           dest.h = 32;
143
144           if(current_level.bkgd_image[0] == '\0')
145             {
146               fillrect(pbouncy_brick->base.x - scroll_x,pbouncy_brick->base.y,32,32,current_level.bkgd_red,current_level.bkgd_green,
147                        current_level.bkgd_blue,0);
148             }
149           else
150             {
151               s = (int)scroll_x / 30;
152               texture_draw_part(&img_bkgd,dest.x + s,dest.y,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
153             }
154
155           drawshape(pbouncy_brick->base.x - scroll_x,
156                     pbouncy_brick->base.y + pbouncy_brick->offset,
157                     pbouncy_brick->shape);
158         }
159     }
160 }
161
162 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
163 {
164   pfloating_score->base.alive = YES;
165   pfloating_score->base.x = x;
166   pfloating_score->base.y = y - 16;
167   timer_init(&pfloating_score->timer,YES);
168   timer_start(&pfloating_score->timer,1000);
169   pfloating_score->value = s;
170 }
171
172 void floating_score_action(floating_score_type* pfloating_score)
173 {
174   if (pfloating_score->base.alive)
175     {
176       pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
177
178       if(!timer_check(&pfloating_score->timer))
179         pfloating_score->base.alive = NO;
180     }
181 }
182
183 void floating_score_draw(floating_score_type* pfloating_score)
184 {
185   if (pfloating_score->base.alive)
186     {
187       char str[10];
188       sprintf(str, "%d", pfloating_score->value);
189       text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1, NO_UPDATE);
190     }
191 }
192