- converted text_type into a class
[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   img_distro[0]->draw(base.x - scroll_x,
48                       base.y);
49 }
50
51
52 void
53 BrokenBrick::init(Tile* tile_, float x, float y, float xm, float ym)
54 {
55   tile    = tile_;
56   base.x  = x;
57   base.y  = y;
58   base.xm = xm;
59   base.ym = ym;
60
61   timer.init(true);
62   timer.start(200);
63 }
64
65 void
66 BrokenBrick::action(double frame_ratio)
67 {
68   base.x = base.x + base.xm * frame_ratio;
69   base.y = base.y + base.ym * frame_ratio;
70
71   if (!timer.check())
72     World::current()->broken_bricks.erase(static_cast<std::vector<BrokenBrick>::iterator>(this));
73 }
74
75 void
76 BrokenBrick::draw()
77 {
78   SDL_Rect src, dest;
79   src.x = rand() % 16;
80   src.y = rand() % 16;
81   src.w = 16;
82   src.h = 16;
83
84   dest.x = (int)(base.x - scroll_x);
85   dest.y = (int)base.y;
86   dest.w = 16;
87   dest.h = 16;
88   
89   if (tile->images.size() > 0)
90     tile->images[0]->draw_part(src.x,src.y,dest.x,dest.y,dest.w,dest.h);
91 }
92
93 void
94 BouncyBrick::init(float x, float y)
95 {
96   base.x   = x;
97   base.y   = y;
98   offset   = 0;
99   offset_m = -BOUNCY_BRICK_SPEED;
100   shape    = World::current()->get_level()->gettileid(x, y);
101 }
102
103 void
104 BouncyBrick::action(double frame_ratio)
105 {
106   offset = (offset + offset_m * frame_ratio);
107
108   /* Go back down? */
109   if (offset < -BOUNCY_BRICK_MAX_OFFSET)
110     offset_m = BOUNCY_BRICK_SPEED;
111
112
113   /* Stop bouncing? */
114   if (offset >= 0)
115     World::current()->bouncy_bricks.erase(static_cast<std::vector<BouncyBrick>::iterator>(this));
116 }
117
118 void
119 BouncyBrick::draw()
120 {
121   int s;
122   SDL_Rect dest;
123   
124   if (base.x >= scroll_x - 32 &&
125       base.x <= scroll_x + screen->w)
126     {
127       dest.x = (int)(base.x - scroll_x);
128       dest.y = (int)base.y;
129       dest.w = 32;
130       dest.h = 32;
131
132       Level* plevel = World::current()->get_level();
133
134       // FIXME: overdrawing hack to clean the tile from the screen to
135       // paint it later at on offseted position
136       if(plevel->bkgd_image[0] == '\0')
137         {
138           fillrect(base.x - scroll_x, base.y,
139                    32,32, 
140                    plevel->bkgd_top.red, plevel->bkgd_top.green, plevel->bkgd_top.blue, 0);
141 // FIXME: doesn't respect the gradient, futhermore is this necessary at all??
142         }
143       else
144         {
145           s = (int)scroll_x / 30;
146           plevel->img_bkgd->draw_part(dest.x + s, dest.y, 
147                                       dest.x, dest.y,dest.w,dest.h);
148         }
149
150       Tile::draw(base.x - scroll_x,
151                  base.y + offset,
152                  shape);
153     }
154 }
155
156 void
157 FloatingScore::init(float x, float y, int s)
158 {
159   base.x = x;
160   base.y = y - 16;
161   timer.init(true);
162   timer.start(1000);
163   value = s;
164 }
165
166 void
167 FloatingScore::action(double frame_ratio)
168 {
169   base.y = base.y - 2 * frame_ratio;
170
171   if(!timer.check())
172     World::current()->floating_scores.erase(static_cast<std::vector<FloatingScore>::iterator>(this));
173 }
174
175 void
176 FloatingScore::draw()
177 {
178   char str[10];
179   sprintf(str, "%d", value);
180   gold_text->draw(str, (int)base.x + 16 - strlen(str) * 8, (int)base.y, 1);
181 }
182
183 /* EOF */
184