4af687d78173e433989e5f3561dae6ef50154cf3
[supertux.git] / src / object / gameobjs.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 <config.h>
21
22 #include <algorithm>
23 #include <iostream>
24 #include <cmath>
25
26 #include "tile.hpp"
27 #include "tile_manager.hpp"
28 #include "game_session.hpp"
29 #include "gameobjs.hpp"
30 #include "sprite/sprite_manager.hpp"
31 #include "sprite/sprite.hpp"
32 #include "resources.hpp"
33 #include "sector.hpp"
34 #include "tilemap.hpp"
35 #include "video/drawing_context.hpp"
36 #include "camera.hpp"
37 #include "main.hpp"
38 #include "random_generator.hpp"
39
40 /** this controls the time over which a bouncy coin fades */
41 static const float FADE_TIME = .2f;
42 /** this is the total life time of a bouncy coin */
43 static const float LIFE_TIME = .5f;
44
45 BouncyCoin::BouncyCoin(const Vector& pos, bool emerge)
46   : position(pos), emerge_distance(0)
47 {
48   timer.start(LIFE_TIME);
49   sprite = sprite_manager->create("images/objects/coin/coin.sprite");
50
51   if(emerge) {
52     emerge_distance = sprite->get_height();
53   }
54 }
55
56 BouncyCoin::~BouncyCoin()
57 {
58   delete sprite;
59 }
60
61 void
62 BouncyCoin::update(float elapsed_time)
63 {
64   float dist = -200 * elapsed_time;
65   position.y += dist;
66   emerge_distance += dist;
67
68   if(timer.check())
69     remove_me();
70 }
71
72 void
73 BouncyCoin::draw(DrawingContext& context)
74 {
75   float time_left = timer.get_timeleft();
76   bool fading = time_left < FADE_TIME;
77   if(fading) {
78     float alpha = time_left/FADE_TIME;
79     context.push_transform();
80     context.set_alpha(alpha);
81   }
82
83   int layer;
84   if(emerge_distance > 0) {
85     layer = LAYER_OBJECTS - 5;
86   } else {
87     layer = LAYER_OBJECTS + 5;
88   }
89   sprite->draw(context, position, layer);
90
91   if(fading) {
92     context.pop_transform();
93   }
94 }
95
96 //---------------------------------------------------------------------------
97
98 BrokenBrick::BrokenBrick(Sprite* nsprite,
99     const Vector& pos, const Vector& nmovement)
100   : sprite(new Sprite(*nsprite)), position(pos), movement(nmovement)
101 {
102   timer.start(.2f);
103 }
104
105 BrokenBrick::~BrokenBrick()
106 {
107   delete sprite;
108 }
109
110 void
111 BrokenBrick::update(float elapsed_time)
112 {
113   position += movement * elapsed_time;
114
115   if (timer.check())
116     remove_me();
117 }
118
119 void
120 BrokenBrick::draw(DrawingContext& context)
121 {
122   sprite->draw_part(context,
123       Vector(systemRandom.rand(16), systemRandom.rand(16)), Vector(16, 16),
124       position, LAYER_OBJECTS + 1);
125 }
126
127 //---------------------------------------------------------------------------
128
129 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
130   : position(pos), text(text_)
131 {
132   timer.start(.1f);
133   position.x -= text.size() * 8;
134 }
135
136 FloatingText::FloatingText(const Vector& pos, int score)
137   : position(pos)
138 {
139   timer.start(.1f);
140
141   // turn int into a string
142   char str[10];
143   snprintf(str, 10, "%d", score);
144   text = str;
145
146   position.x -= text.size() * 8;
147 }
148
149 void
150 FloatingText::update(float elapsed_time)
151 {
152   position.y -= 1.4 * elapsed_time;
153
154   if(timer.check())
155     remove_me();
156 }
157
158 #define FADING_TIME .350
159
160 void
161 FloatingText::draw(DrawingContext& context)
162 {
163   // make an alpha animation when disapearing
164   int alpha;
165   if(timer.get_timeleft() < FADING_TIME)
166     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
167   else
168     alpha = 255;
169
170   context.push_transform();
171   context.set_alpha(alpha);
172
173   context.draw_text(gold_text, text, position, ALIGN_LEFT, LAYER_OBJECTS+1);
174
175   context.pop_transform();
176 }
177
178 Sprite *img_smoke_cloud = 0;
179
180 SmokeCloud::SmokeCloud(const Vector& pos)
181   : position(pos)
182 {
183   timer.start(.3f);
184   sprite = sprite_manager->create("images/objects/particles/stomp.sprite");
185 }
186
187 SmokeCloud::~SmokeCloud()
188 {
189   delete sprite;
190 }
191
192 void
193 SmokeCloud::update(float elapsed_time)
194 {
195   position.y -= 120 * elapsed_time;
196
197   if(timer.check())
198     remove_me();
199 }
200
201 void
202 SmokeCloud::draw(DrawingContext& context)
203 {
204   sprite->draw(context, position, LAYER_OBJECTS+1);
205 }