- moved gameobjects into there own file
[supertux.git] / src / gameobjs.cpp
1 #include "world.h"
2 #include "tile.h"
3 #include "gameloop.h"
4 #include "gameobjs.h"
5
6 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
7 {
8   pbouncy_distro->base.x = x;
9   pbouncy_distro->base.y = y;
10   pbouncy_distro->base.ym = -2;
11 }
12
13 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
14 {
15   pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
16
17   pbouncy_distro->base.ym += 0.1 * frame_ratio;
18
19   if (pbouncy_distro->base.ym >= 0)
20     world.bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
21 }
22
23 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
24 {
25   texture_draw(&img_distro[0],
26                pbouncy_distro->base.x - scroll_x,
27                pbouncy_distro->base.y);
28 }
29
30 void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile, 
31                        float x, float y, float xm, float ym)
32 {
33   pbroken_brick->tile   = tile;
34   pbroken_brick->base.x = x;
35   pbroken_brick->base.y = y;
36   pbroken_brick->base.xm = xm;
37   pbroken_brick->base.ym = ym;
38
39   timer_init(&pbroken_brick->timer, true);
40   timer_start(&pbroken_brick->timer,200);
41 }
42
43 void broken_brick_action(broken_brick_type* pbroken_brick)
44 {
45   pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
46   pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
47
48   if (!timer_check(&pbroken_brick->timer))
49     world.broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
50 }
51
52 void broken_brick_draw(broken_brick_type* pbroken_brick)
53 {
54   SDL_Rect src, dest;
55   src.x = rand() % 16;
56   src.y = rand() % 16;
57   src.w = 16;
58   src.h = 16;
59
60   dest.x = (int)(pbroken_brick->base.x - scroll_x);
61   dest.y = (int)pbroken_brick->base.y;
62   dest.w = 16;
63   dest.h = 16;
64   
65   if (pbroken_brick->tile->images.size() > 0)
66     texture_draw_part(&pbroken_brick->tile->images[0],
67                       src.x,src.y,dest.x,dest.y,dest.w,dest.h);
68 }
69
70 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
71 {
72   pbouncy_brick->base.x   = x;
73   pbouncy_brick->base.y   = y;
74   pbouncy_brick->offset   = 0;
75   pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
76   pbouncy_brick->shape    = GameSession::current()->get_level()->gettileid(x, y);
77 }
78
79 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
80 {
81
82   pbouncy_brick->offset = (pbouncy_brick->offset +
83                            pbouncy_brick->offset_m * frame_ratio);
84
85   /* Go back down? */
86
87   if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
88     pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
89
90
91   /* Stop bouncing? */
92
93   if (pbouncy_brick->offset >= 0)
94     world.bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
95 }
96
97 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
98 {
99   int s;
100   SDL_Rect dest;
101   
102   if (pbouncy_brick->base.x >= scroll_x - 32 &&
103       pbouncy_brick->base.x <= scroll_x + screen->w)
104     {
105       dest.x = (int)(pbouncy_brick->base.x - scroll_x);
106       dest.y = (int)pbouncy_brick->base.y;
107       dest.w = 32;
108       dest.h = 32;
109
110       Level* plevel = GameSession::current()->get_level();
111
112       // FIXME: overdrawing hack to clean the tile from the screen to
113       // paint it later at on offseted position
114       if(plevel->bkgd_image[0] == '\0')
115         {
116           fillrect(pbouncy_brick->base.x - scroll_x, pbouncy_brick->base.y,
117                    32,32, 
118                    plevel->bkgd_red, plevel->bkgd_green, plevel->bkgd_blue, 0);
119         }
120       else
121         {
122           s = (int)scroll_x / 30;
123           texture_draw_part(&plevel->img_bkgd, dest.x + s, dest.y, 
124                             dest.x, dest.y,dest.w,dest.h);
125         }
126
127       Tile::draw(pbouncy_brick->base.x - scroll_x,
128                  pbouncy_brick->base.y + pbouncy_brick->offset,
129                  pbouncy_brick->shape);
130     }
131 }
132
133 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
134 {
135   pfloating_score->base.x = x;
136   pfloating_score->base.y = y - 16;
137   timer_init(&pfloating_score->timer,true);
138   timer_start(&pfloating_score->timer,1000);
139   pfloating_score->value = s;
140 }
141
142 void floating_score_action(floating_score_type* pfloating_score)
143 {
144   pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
145
146   if(!timer_check(&pfloating_score->timer))
147     world.floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
148 }
149
150 void floating_score_draw(floating_score_type* pfloating_score)
151 {
152   char str[10];
153   sprintf(str, "%d", pfloating_score->value);
154   text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1);
155 }
156
157 /* EOF */
158