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.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS, NONE_EFFECT, alpha);
163 Sprite *img_trampoline;
165 Trampoline::Trampoline(LispReader& reader)
167 reader.read_float("x", base.x);
168 reader.read_float("y", base.y);
172 reader.read_float("power", power);
179 Trampoline::Trampoline(float x, float y)
193 Trampoline::write(LispWriter& writer)
195 writer.start_list("trampoline");
197 writer.write_float("x", base.x);
198 writer.write_float("y", base.y);
199 writer.write_float("power", power);
201 writer.end_list("trampoline");
205 Trampoline::draw(DrawingContext& context)
207 img_trampoline->set_frame(frame);
208 img_trampoline->draw(context, base, LAYER_OBJECTS);
213 Trampoline::action(float frame_ratio)
215 // TODO: Remove if we're too far off the screen
220 if (issolid(base.x + base.width/2, base.y + base.height))
222 base.y = int((base.y + base.height)/32) * 32 - base.height;
224 physic.enable_gravity(false);
225 physic.set_velocity_y(0.0f);
227 physic.set_velocity_x(0);
231 physic.enable_gravity(true);
234 else // Player is carrying us around
236 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
237 /* If we're holding the iceblock */
238 Player& tux = *Sector::current()->player;
239 Direction dir = tux.dir;
243 base.x = tux.base.x + 16;
244 base.y = tux.base.y + tux.base.height/1.5 - base.height;
246 else /* facing left */
248 base.x = tux.base.x - 16;
249 base.y = tux.base.y + tux.base.height/1.5 - base.height;
252 if(collision_object_map(base))
255 base.y = tux.base.y + tux.base.height/1.5 - base.height;
259 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
260 collision_swept_object_map(&old_base, &base);
264 Trampoline::collision(const MovingObject&, int)
270 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
272 Player* pplayer_c = NULL;
276 pplayer_c = (Player*) p_c_object;
278 if (type == COLLISION_NORMAL)
280 // Pick up if HELD (done in Player)
283 else if (type == COLLISION_SQUISH)
285 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
287 if (squish_amount < 24)
289 else if (squish_amount < 28)
291 else if (squish_amount < 30)
296 if (squish_amount < 20) {
297 pplayer_c->physic.set_velocity_y(power);
298 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
300 else if (pplayer_c->physic.get_velocity_y() < 0)
301 pplayer_c->physic.set_velocity_y(-squish_amount/32);
312 /* Flying Platform */
314 Sprite *img_flying_platform;
316 FlyingPlatform::FlyingPlatform(LispReader& reader)
318 reader.read_int_vector("x", pos_x);
319 reader.read_int_vector("y", pos_y);
322 reader.read_float("velocity", velocity);
332 float x = pos_x[point+1] - pos_x[point];
333 float y = pos_y[point+1] - pos_y[point];
334 vel_x = x*velocity / sqrt(x*x + y*y);
335 vel_y = -(velocity - vel_x);
340 FlyingPlatform::FlyingPlatform(int x, int y)
349 FlyingPlatform::write(LispWriter& writer)
351 writer.start_list("flying-trampoline");
353 writer.write_int_vector("x", pos_x);
354 writer.write_int_vector("y", pos_y);
355 writer.write_float("velocity", velocity);
357 writer.end_list("flying-trampoline");
361 FlyingPlatform::draw(DrawingContext& context)
363 img_flying_platform->draw(context, base, LAYER_OBJECTS);
367 FlyingPlatform::action(float frame_ratio)
369 // TODO: Remove if we're too far off the screen
374 if((unsigned)point+1 != pos_x.size())
376 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
377 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
378 pos_x[point] == pos_x[point+1]) &&
379 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
380 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
381 pos_y[point] == pos_y[point+1]))
385 float x = pos_x[point+1] - pos_x[point];
386 float y = pos_y[point+1] - pos_y[point];
387 vel_x = x*velocity / sqrt(x*x + y*y);
388 vel_y = -(velocity - vel_x);
398 if(pos_x[point+1] > base.x)
399 base.x += velocity * frame_ratio;
400 else if(pos_x[point+1] < base.x)
401 base.x -= velocity * frame_ratio;
403 if(pos_y[point+1] > base.y)
404 base.y += velocity * frame_ratio;
405 else if(pos_y[point+1] < base.y)
406 base.y -= velocity * frame_ratio;
409 base.x += vel_x * frame_ratio;
410 base.y += vel_y * frame_ratio;
414 FlyingPlatform::collision(const MovingObject&, int)
420 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
425 // Player* pplayer_c = NULL;
429 // pplayer_c = (Player*) p_c_object;
440 Sprite *img_smoke_cloud;
442 SmokeCloud::SmokeCloud(const Vector& pos)
449 SmokeCloud::action(float elapsed_time)
451 position.y -= 1.2 * elapsed_time;
458 SmokeCloud::draw(DrawingContext& context)
460 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
463 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
464 : color(color_), size(size_), vel(velocity), accel(acceleration)
472 live_forever = false;
473 timer.start(life_time);
477 for(int p = 0; p < number; p++)
479 Particle* particle = new Particle;
480 particle->pos = epicenter;
481 particle->angle = (rand() % 360) * (M_PI / 180); // in radius
483 particles.push_back(particle);
487 Particles::~Particles()
490 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
495 Particles::action(float elapsed_time)
497 vel.x += accel.x * elapsed_time;
498 vel.y += accel.y * elapsed_time;
500 int camera_x = (int)Sector::current()->camera->get_translation().x;
501 int camera_y = (int)Sector::current()->camera->get_translation().y;
504 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
506 (*i)->pos.x += sin((*i)->angle) * vel.x * elapsed_time;
507 (*i)->pos.y += cos((*i)->angle) * vel.y * elapsed_time;
509 if((*i)->pos.x < camera_x || (*i)->pos.x > screen->w + camera_x ||
510 (*i)->pos.y < camera_y || (*i)->pos.y > screen->h + camera_y)
517 if((!timer.check() && !live_forever) || particles.size() == 0)
522 Particles::draw(DrawingContext& context)
525 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
527 context.draw_filled_rect((*i)->pos, Vector(size,size), color, LAYER_OBJECTS+10);
531 void load_object_gfx()
533 img_trampoline = sprite_manager->load("trampoline");
534 img_trampoline->start_animation(0);
535 img_flying_platform = sprite_manager->load("flying_platform");
536 img_smoke_cloud = sprite_manager->load("stomp");