4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
26 #include "app/globals.h"
28 #include "tile_manager.h"
31 #include "special/sprite_manager.h"
32 #include "resources.h"
35 #include "video/drawing_context.h"
38 BouncyDistro::BouncyDistro(const Vector& pos)
45 BouncyDistro::action(float elapsed_time)
47 position.y += ym * elapsed_time;
49 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
55 BouncyDistro::draw(DrawingContext& context)
57 context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
61 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
62 : tile(ntile), position(pos), movement(nmovement)
68 BrokenBrick::action(float elapsed_time)
70 position += movement * elapsed_time;
77 BrokenBrick::draw(DrawingContext& context)
79 if (tile->images.size() > 0)
80 context.draw_surface_part(tile->images[0],
81 Vector(rand() % 16, rand() % 16),
83 position, LAYER_OBJECTS + 1);
86 BouncyBrick::BouncyBrick(const Vector& pos)
87 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED),
88 shape(Sector::current()->solids->get_tile_id_at(pos))
94 BouncyBrick::action(float elapsed_time)
96 offset += offset_m * elapsed_time;
99 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
100 offset_m = BOUNCY_BRICK_SPEED;
105 shape.hidden = false;
111 BouncyBrick::draw(DrawingContext& context)
113 TileManager::instance()->
114 draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
117 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
118 : position(pos), text(text_)
121 position.x -= text.size() * 8;
124 FloatingText::FloatingText(const Vector& pos, int score)
129 // turn int into a string
131 snprintf(str, 10, "%d", score);
134 position.x -= text.size() * 8;
138 FloatingText::action(float elapsed_time)
140 position.y -= 1.4 * elapsed_time;
146 #define FADING_TIME 350
149 FloatingText::draw(DrawingContext& context)
151 // make an alpha animation when disapearing
153 if(timer.get_left() < FADING_TIME)
154 alpha = timer.get_left() * 255 / FADING_TIME;
158 context.push_transform();
159 context.set_alpha(alpha);
161 context.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS+1);
163 context.pop_transform();
168 Sprite *img_trampoline;
170 Trampoline::Trampoline(LispReader& reader)
172 reader.read_float("x", base.x);
173 reader.read_float("y", base.y);
177 reader.read_float("power", power);
184 Trampoline::Trampoline(float x, float y)
198 Trampoline::write(LispWriter& writer)
200 writer.start_list("trampoline");
202 writer.write_float("x", base.x);
203 writer.write_float("y", base.y);
204 writer.write_float("power", power);
206 writer.end_list("trampoline");
210 Trampoline::draw(DrawingContext& context)
212 img_trampoline->set_frame(frame);
213 img_trampoline->draw(context, base, LAYER_OBJECTS);
218 Trampoline::action(float frame_ratio)
220 // TODO: Remove if we're too far off the screen
225 if (issolid(base.x + base.width/2, base.y + base.height))
227 base.y = int((base.y + base.height)/32) * 32 - base.height;
229 physic.enable_gravity(false);
230 physic.set_velocity_y(0.0f);
232 physic.set_velocity_x(0);
236 physic.enable_gravity(true);
239 else // Player is carrying us around
241 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
242 /* If we're holding the iceblock */
243 Player& tux = *Sector::current()->player;
244 Direction dir = tux.dir;
248 base.x = tux.base.x + 16;
249 base.y = tux.base.y + tux.base.height/1.5 - base.height;
251 else /* facing left */
253 base.x = tux.base.x - 16;
254 base.y = tux.base.y + tux.base.height/1.5 - base.height;
257 if(collision_object_map(base))
260 base.y = tux.base.y + tux.base.height/1.5 - base.height;
264 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
265 collision_swept_object_map(&old_base, &base);
269 Trampoline::collision(const MovingObject&, int)
275 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
277 Player* pplayer_c = NULL;
281 pplayer_c = (Player*) p_c_object;
283 if (type == COLLISION_NORMAL)
285 // Pick up if HELD (done in Player)
288 else if (type == COLLISION_SQUISH)
290 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
292 if (squish_amount < 24)
294 else if (squish_amount < 28)
296 else if (squish_amount < 30)
301 if (squish_amount < 20) {
302 pplayer_c->physic.set_velocity_y(power);
303 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
305 else if (pplayer_c->physic.get_velocity_y() < 0)
306 pplayer_c->physic.set_velocity_y(-squish_amount/32);
317 /* Flying Platform */
319 Sprite *img_flying_platform;
321 FlyingPlatform::FlyingPlatform(LispReader& reader)
323 reader.read_int_vector("x", pos_x);
324 reader.read_int_vector("y", pos_y);
327 reader.read_float("velocity", velocity);
337 float x = pos_x[point+1] - pos_x[point];
338 float y = pos_y[point+1] - pos_y[point];
339 vel_x = x*velocity / sqrt(x*x + y*y);
340 vel_y = -(velocity - vel_x);
345 FlyingPlatform::FlyingPlatform(int x, int y)
354 FlyingPlatform::write(LispWriter& writer)
356 writer.start_list("flying-trampoline");
358 writer.write_int_vector("x", pos_x);
359 writer.write_int_vector("y", pos_y);
360 writer.write_float("velocity", velocity);
362 writer.end_list("flying-trampoline");
366 FlyingPlatform::draw(DrawingContext& context)
368 img_flying_platform->draw(context, base, LAYER_OBJECTS);
372 FlyingPlatform::action(float frame_ratio)
374 // TODO: Remove if we're too far off the screen
379 if((unsigned)point+1 != pos_x.size())
381 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
382 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
383 pos_x[point] == pos_x[point+1]) &&
384 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
385 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
386 pos_y[point] == pos_y[point+1]))
390 float x = pos_x[point+1] - pos_x[point];
391 float y = pos_y[point+1] - pos_y[point];
392 vel_x = x*velocity / sqrt(x*x + y*y);
393 vel_y = -(velocity - vel_x);
403 if(pos_x[point+1] > base.x)
404 base.x += velocity * frame_ratio;
405 else if(pos_x[point+1] < base.x)
406 base.x -= velocity * frame_ratio;
408 if(pos_y[point+1] > base.y)
409 base.y += velocity * frame_ratio;
410 else if(pos_y[point+1] < base.y)
411 base.y -= velocity * frame_ratio;
414 base.x += vel_x * frame_ratio;
415 base.y += vel_y * frame_ratio;
419 FlyingPlatform::collision(const MovingObject&, int)
425 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
430 // Player* pplayer_c = NULL;
434 // pplayer_c = (Player*) p_c_object;
445 Sprite *img_smoke_cloud;
447 SmokeCloud::SmokeCloud(const Vector& pos)
454 SmokeCloud::action(float elapsed_time)
456 position.y -= 1.2 * elapsed_time;
463 SmokeCloud::draw(DrawingContext& context)
465 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
468 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
469 : color(color_), size(size_), vel(velocity), accel(acceleration)
477 live_forever = false;
478 timer.start(life_time);
482 for(int p = 0; p < number; p++)
484 Particle* particle = new Particle;
485 particle->pos = epicenter;
486 particle->angle = (rand() % 360) * (M_PI / 180); // in radius
488 particles.push_back(particle);
492 Particles::~Particles()
495 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
500 Particles::action(float elapsed_time)
502 vel.x += accel.x * elapsed_time;
503 vel.y += accel.y * elapsed_time;
505 int camera_x = (int)Sector::current()->camera->get_translation().x;
506 int camera_y = (int)Sector::current()->camera->get_translation().y;
509 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
511 (*i)->pos.x += sin((*i)->angle) * vel.x * elapsed_time;
512 (*i)->pos.y += cos((*i)->angle) * vel.y * elapsed_time;
514 if((*i)->pos.x < camera_x || (*i)->pos.x > screen->w + camera_x ||
515 (*i)->pos.y < camera_y || (*i)->pos.y > screen->h + camera_y)
522 if((!timer.check() && !live_forever) || particles.size() == 0)
527 Particles::draw(DrawingContext& context)
530 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
532 context.draw_filled_rect((*i)->pos, Vector(size,size), color, LAYER_OBJECTS+10);
536 void load_object_gfx()
538 img_trampoline = sprite_manager->load("trampoline");
539 img_trampoline->start_animation(0);
540 img_flying_platform = sprite_manager->load("flying_platform");
541 img_smoke_cloud = sprite_manager->load("stomp");