Added support for gradients :)
[supertux.git] / src / gameobjs.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include "world.h"
21 #include "tile.h"
22 #include "gameloop.h"
23 #include "gameobjs.h"
24
25 void
26 BouncyDistro::init(float x, float y)
27 {
28   base.x = x;
29   base.y = y;
30   base.ym = -2;
31 }
32
33 void
34 BouncyDistro::action(double frame_ratio)
35 {
36   base.y = base.y + base.ym * frame_ratio;
37
38   base.ym += 0.1 * frame_ratio;
39
40   if (base.ym >= 0)
41     World::current()->bouncy_distros.erase(static_cast<std::vector<BouncyDistro>::iterator>(this));
42 }
43
44 void
45 BouncyDistro::draw()
46 {
47   texture_draw(&img_distro[0],
48                base.x - scroll_x,
49                base.y);
50 }
51
52
53 void
54 BrokenBrick::init(Tile* tile_, float x, float y, float xm, float ym)
55 {
56   tile    = tile_;
57   base.x  = x;
58   base.y  = y;
59   base.xm = xm;
60   base.ym = ym;
61
62   timer.init(true);
63   timer.start(200);
64 }
65
66 void
67 BrokenBrick::action(double frame_ratio)
68 {
69   base.x = base.x + base.xm * frame_ratio;
70   base.y = base.y + base.ym * frame_ratio;
71
72   if (!timer.check())
73     World::current()->broken_bricks.erase(static_cast<std::vector<BrokenBrick>::iterator>(this));
74 }
75
76 void
77 BrokenBrick::draw()
78 {
79   SDL_Rect src, dest;
80   src.x = rand() % 16;
81   src.y = rand() % 16;
82   src.w = 16;
83   src.h = 16;
84
85   dest.x = (int)(base.x - scroll_x);
86   dest.y = (int)base.y;
87   dest.w = 16;
88   dest.h = 16;
89   
90   if (tile->images.size() > 0)
91     texture_draw_part(&tile->images[0],
92                       src.x,src.y,dest.x,dest.y,dest.w,dest.h);
93 }
94
95 void
96 BouncyBrick::init(float x, float y)
97 {
98   base.x   = x;
99   base.y   = y;
100   offset   = 0;
101   offset_m = -BOUNCY_BRICK_SPEED;
102   shape    = World::current()->get_level()->gettileid(x, y);
103 }
104
105 void
106 BouncyBrick::action(double frame_ratio)
107 {
108   offset = (offset + offset_m * frame_ratio);
109
110   /* Go back down? */
111   if (offset < -BOUNCY_BRICK_MAX_OFFSET)
112     offset_m = BOUNCY_BRICK_SPEED;
113
114
115   /* Stop bouncing? */
116   if (offset >= 0)
117     World::current()->bouncy_bricks.erase(static_cast<std::vector<BouncyBrick>::iterator>(this));
118 }
119
120 void
121 BouncyBrick::draw()
122 {
123   int s;
124   SDL_Rect dest;
125   
126   if (base.x >= scroll_x - 32 &&
127       base.x <= scroll_x + screen->w)
128     {
129       dest.x = (int)(base.x - scroll_x);
130       dest.y = (int)base.y;
131       dest.w = 32;
132       dest.h = 32;
133
134       Level* plevel = World::current()->get_level();
135
136       // FIXME: overdrawing hack to clean the tile from the screen to
137       // paint it later at on offseted position
138       if(plevel->bkgd_image[0] == '\0')
139         {
140           fillrect(base.x - scroll_x, base.y,
141                    32,32, 
142                    plevel->bkgd_top_red, plevel->bkgd_top_green, plevel->bkgd_top_blue, 0);
143 // FIXME: doesn't respect the gradient, futhermore is this necessary at all??
144         }
145       else
146         {
147           s = (int)scroll_x / 30;
148           texture_draw_part(&plevel->img_bkgd, dest.x + s, dest.y, 
149                             dest.x, dest.y,dest.w,dest.h);
150         }
151
152       Tile::draw(base.x - scroll_x,
153                  base.y + offset,
154                  shape);
155     }
156 }
157
158 void
159 FloatingScore::init(float x, float y, int s)
160 {
161   base.x = x;
162   base.y = y - 16;
163   timer.init(true);
164   timer.start(1000);
165   value = s;
166 }
167
168 void
169 FloatingScore::action(double frame_ratio)
170 {
171   base.y = base.y - 2 * frame_ratio;
172
173   if(!timer.check())
174     World::current()->floating_scores.erase(static_cast<std::vector<FloatingScore>::iterator>(this));
175 }
176
177 void
178 FloatingScore::draw()
179 {
180   char str[10];
181   sprintf(str, "%d", value);
182   text_draw(&gold_text, str, (int)base.x + 16 - strlen(str) * 8, (int)base.y, 1);
183 }
184
185 /* EOF */
186