move over rewritten lispreader from tuxkart (with additional fixes), generalized...
[supertux.git] / src / object / gameobjs.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <config.h>
22
23 #include <algorithm>
24 #include <iostream>
25 #include <cmath>
26
27 #include "app/globals.h"
28 #include "tile.h"
29 #include "tile_manager.h"
30 #include "gameloop.h"
31 #include "gameobjs.h"
32 #include "special/sprite_manager.h"
33 #include "resources.h"
34 #include "sector.h"
35 #include "tilemap.h"
36 #include "video/drawing_context.h"
37 #include "camera.h"
38
39 BouncyCoin::BouncyCoin(const Vector& pos)
40   : position(pos)
41 {
42   timer.start(.3);
43   sprite = sprite_manager->create("coin");
44   sprite->set_action("still");
45 }
46
47 BouncyCoin::~BouncyCoin()
48 {
49   delete sprite;
50 }
51
52 void
53 BouncyCoin::action(float elapsed_time)
54 {
55   position.y += -200 * elapsed_time;
56
57   if(timer.check())
58     remove_me();
59 }
60
61 void
62 BouncyCoin::draw(DrawingContext& context)
63 {
64   sprite->draw(context, position, LAYER_OBJECTS);
65 }
66
67 //---------------------------------------------------------------------------
68
69 BrokenBrick::BrokenBrick(Sprite* nsprite,
70     const Vector& pos, const Vector& nmovement)
71   : sprite(new Sprite(*nsprite)), position(pos), movement(nmovement)
72 {
73   timer.start(.2);
74 }
75
76 BrokenBrick::~BrokenBrick()
77 {
78   delete sprite;
79 }
80
81 void
82 BrokenBrick::action(float elapsed_time)
83 {
84   position += movement * elapsed_time;
85
86   if (timer.check())
87     remove_me();
88 }
89
90 void
91 BrokenBrick::draw(DrawingContext& context)
92 {
93   sprite->draw_part(context,
94       Vector(rand() % 16, rand() % 16), Vector(16, 16),
95       position, LAYER_OBJECTS + 1);
96 }
97
98 //---------------------------------------------------------------------------
99
100 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
101   : position(pos), text(text_)
102 {
103   timer.start(.1);
104   position.x -= text.size() * 8;
105 }
106
107 FloatingText::FloatingText(const Vector& pos, int score)
108   : position(pos)
109 {
110   timer.start(.1);
111
112   // turn int into a string
113   char str[10];
114   snprintf(str, 10, "%d", score);
115   text = str;
116
117   position.x -= text.size() * 8;
118 }
119
120 void
121 FloatingText::action(float elapsed_time)
122 {
123   position.y -= 1.4 * elapsed_time;
124
125   if(timer.check())
126     remove_me();
127 }
128
129 #define FADING_TIME .350
130
131 void
132 FloatingText::draw(DrawingContext& context)
133 {
134   // make an alpha animation when disapearing
135   int alpha;
136   if(timer.get_timeleft() < FADING_TIME)
137     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
138   else
139     alpha = 255;
140
141   context.push_transform();
142   context.set_alpha(alpha);
143
144   context.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS+1);
145
146   context.pop_transform();
147 }
148
149 Sprite *img_smoke_cloud = 0;
150
151 SmokeCloud::SmokeCloud(const Vector& pos)
152   : position(pos)
153 {
154   timer.start(.3);
155 }
156
157 void
158 SmokeCloud::action(float elapsed_time)
159 {
160   position.y -= 120 * elapsed_time;
161
162   if(timer.check())
163     remove_me();
164 }
165
166 void
167 SmokeCloud::draw(DrawingContext& context)
168 {
169   img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
170 }
171
172 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
173         const Vector& initial_velocity, const Vector& acceleration, int number,
174         Color color_, int size_, float life_time, int drawing_layer_)
175   : accel(acceleration), color(color_), size(size_), drawing_layer(drawing_layer_)
176 {
177   if(life_time == 0) {
178     live_forever = true;
179   } else {
180     live_forever = false;
181     timer.start(life_time);
182   }
183
184   // create particles
185   for(int p = 0; p < number; p++)
186     {
187     Particle* particle = new Particle;
188     particle->pos = epicenter;
189
190     float angle = ((rand() % (max_angle-min_angle))+min_angle)
191                       * (M_PI / 180);  // convert to radius
192     particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
193 //    if(angle >= M_PI && angle < M_PI*2)
194 //      particle->vel.x *= -1;  // work around to fix signal
195     particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
196 //    if(angle >= M_PI_2 && angle < 3*M_PI_2)
197 //      particle->vel.y *= -1;
198
199     particles.push_back(particle);
200     }
201 }
202
203 Particles::~Particles()
204 {
205   // free particles
206   for(std::vector<Particle*>::iterator i = particles.begin();
207       i < particles.end(); i++)
208     delete (*i);
209 }
210
211 void
212 Particles::action(float elapsed_time)
213 {
214   Vector camera = Sector::current()->camera->get_translation();
215
216   // update particles
217   for(std::vector<Particle*>::iterator i = particles.begin();
218       i != particles.end(); ) {
219     (*i)->pos.x += (*i)->vel.x * elapsed_time;
220     (*i)->pos.y += (*i)->vel.y * elapsed_time;
221
222     (*i)->vel.x += accel.x * elapsed_time;
223     (*i)->vel.y += accel.y * elapsed_time;
224
225     if((*i)->pos.x < camera.x || (*i)->pos.x > screen->w + camera.x ||
226        (*i)->pos.y < camera.y || (*i)->pos.y > screen->h + camera.y) {
227       delete (*i);
228       i = particles.erase(i);
229     } else {
230       ++i;
231     }
232   }
233
234   if((timer.check() && !live_forever) || particles.size() == 0)
235     remove_me();
236 }
237
238 void
239 Particles::draw(DrawingContext& context)
240 {
241   // draw particles
242   for(std::vector<Particle*>::iterator i = particles.begin();
243       i != particles.end(); i++) {
244     context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
245   }
246 }
247
248 void load_object_gfx()
249 {
250 #if 0
251   img_trampoline = sprite_manager->load("trampoline");
252   img_trampoline->start_animation(0);
253   img_flying_platform = sprite_manager->load("flying_platform");
254 #endif
255   img_smoke_cloud = sprite_manager->create("stomp");
256 }
257
258 void free_object_gfx()
259 {
260   delete img_smoke_cloud;
261 }
262