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 FloatingScore::FloatingScore(const Vector& pos, int score)
121 snprintf(str, 10, "%d", score);
122 position.x -= strlen(str) * 8;
126 FloatingScore::action(float elapsed_time)
128 position.y -= 2 * elapsed_time;
135 FloatingScore::draw(DrawingContext& context)
137 context.draw_text(gold_text, str, position, LEFT_ALLIGN, LAYER_OBJECTS);
142 Sprite *img_trampoline;
144 Trampoline::Trampoline(LispReader& reader)
146 reader.read_float("x", base.x);
147 reader.read_float("y", base.y);
151 reader.read_float("power", power);
158 Trampoline::Trampoline(float x, float y)
172 Trampoline::write(LispWriter& writer)
174 writer.start_list("trampoline");
176 writer.write_float("x", base.x);
177 writer.write_float("y", base.y);
178 writer.write_float("power", power);
180 writer.end_list("trampoline");
184 Trampoline::draw(DrawingContext& context)
186 img_trampoline->set_frame(frame);
187 img_trampoline->draw(context, base, LAYER_OBJECTS);
192 Trampoline::action(float frame_ratio)
194 // TODO: Remove if we're too far off the screen
199 if (issolid(base.x + base.width/2, base.y + base.height))
201 base.y = int((base.y + base.height)/32) * 32 - base.height;
203 physic.enable_gravity(false);
204 physic.set_velocity_y(0.0f);
206 physic.set_velocity_x(0);
210 physic.enable_gravity(true);
213 else // Player is carrying us around
215 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
216 /* If we're holding the iceblock */
217 Player& tux = *Sector::current()->player;
218 Direction dir = tux.dir;
222 base.x = tux.base.x + 16;
223 base.y = tux.base.y + tux.base.height/1.5 - base.height;
225 else /* facing left */
227 base.x = tux.base.x - 16;
228 base.y = tux.base.y + tux.base.height/1.5 - base.height;
231 if(collision_object_map(base))
234 base.y = tux.base.y + tux.base.height/1.5 - base.height;
238 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
239 collision_swept_object_map(&old_base, &base);
243 Trampoline::collision(const MovingObject&, int)
249 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
251 Player* pplayer_c = NULL;
255 pplayer_c = (Player*) p_c_object;
257 if (type == COLLISION_NORMAL)
259 // Pick up if HELD (done in Player)
262 else if (type == COLLISION_SQUISH)
264 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
266 if (squish_amount < 24)
268 else if (squish_amount < 28)
270 else if (squish_amount < 30)
275 if (squish_amount < 20) {
276 pplayer_c->physic.set_velocity_y(power);
277 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
279 else if (pplayer_c->physic.get_velocity_y() < 0)
280 pplayer_c->physic.set_velocity_y(-squish_amount/32);
291 /* Flying Platform */
293 Sprite *img_flying_platform;
295 FlyingPlatform::FlyingPlatform(LispReader& reader)
297 reader.read_int_vector("x", pos_x);
298 reader.read_int_vector("y", pos_y);
301 reader.read_float("velocity", velocity);
311 float x = pos_x[point+1] - pos_x[point];
312 float y = pos_y[point+1] - pos_y[point];
313 vel_x = x*velocity / sqrt(x*x + y*y);
314 vel_y = -(velocity - vel_x);
319 FlyingPlatform::FlyingPlatform(int x, int y)
328 FlyingPlatform::write(LispWriter& writer)
330 writer.start_list("flying-trampoline");
332 writer.write_int_vector("x", pos_x);
333 writer.write_int_vector("y", pos_y);
334 writer.write_float("velocity", velocity);
336 writer.end_list("flying-trampoline");
340 FlyingPlatform::draw(DrawingContext& context)
342 img_flying_platform->draw(context, base, LAYER_OBJECTS);
346 FlyingPlatform::action(float frame_ratio)
348 // TODO: Remove if we're too far off the screen
353 if((unsigned)point+1 != pos_x.size())
355 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
356 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
357 pos_x[point] == pos_x[point+1]) &&
358 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
359 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
360 pos_y[point] == pos_y[point+1]))
364 float x = pos_x[point+1] - pos_x[point];
365 float y = pos_y[point+1] - pos_y[point];
366 vel_x = x*velocity / sqrt(x*x + y*y);
367 vel_y = -(velocity - vel_x);
377 if(pos_x[point+1] > base.x)
378 base.x += velocity * frame_ratio;
379 else if(pos_x[point+1] < base.x)
380 base.x -= velocity * frame_ratio;
382 if(pos_y[point+1] > base.y)
383 base.y += velocity * frame_ratio;
384 else if(pos_y[point+1] < base.y)
385 base.y -= velocity * frame_ratio;
388 base.x += vel_x * frame_ratio;
389 base.y += vel_y * frame_ratio;
393 FlyingPlatform::collision(const MovingObject&, int)
399 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
404 // Player* pplayer_c = NULL;
408 // pplayer_c = (Player*) p_c_object;
419 Sprite *img_smoke_cloud;
421 SmokeCloud::SmokeCloud(const Vector& pos)
428 SmokeCloud::action(float elapsed_time)
430 position.y -= 1.2 * elapsed_time;
437 SmokeCloud::draw(DrawingContext& context)
439 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
442 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
443 : color(color_), size(size_), vel(velocity), accel(acceleration)
451 live_forever = false;
452 timer.start(life_time);
456 for(int p = 0; p < number; p++)
458 Particle* particle = new Particle;
459 particle->pos = epicenter;
460 particle->angle = (rand() % 360) * (M_PI / 180); // in radius
462 particles.push_back(particle);
466 Particles::~Particles()
469 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
474 Particles::action(float elapsed_time)
476 vel.x += accel.x * elapsed_time;
477 vel.y += accel.y * elapsed_time;
479 int camera_x = (int)Sector::current()->camera->get_translation().x;
480 int camera_y = (int)Sector::current()->camera->get_translation().y;
483 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
485 (*i)->pos.x += sin((*i)->angle) * vel.x * elapsed_time;
486 (*i)->pos.y += cos((*i)->angle) * vel.y * elapsed_time;
488 if((*i)->pos.x < camera_x || (*i)->pos.x > screen->w + camera_x ||
489 (*i)->pos.y < camera_y || (*i)->pos.y > screen->h + camera_y)
496 if((!timer.check() && !live_forever) || particles.size() == 0)
501 Particles::draw(DrawingContext& context)
504 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
506 context.draw_filled_rect((*i)->pos, Vector(size,size), color, LAYER_OBJECTS+10);
510 void load_object_gfx()
512 img_trampoline = sprite_manager->load("trampoline");
513 img_trampoline->start_animation(0);
514 img_flying_platform = sprite_manager->load("flying_platform");
515 img_smoke_cloud = sprite_manager->load("stomp");