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"
37 BouncyDistro::BouncyDistro(const Vector& pos)
44 BouncyDistro::action(float elapsed_time)
46 position.y += ym * elapsed_time;
48 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
54 BouncyDistro::draw(DrawingContext& context)
56 context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
60 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
61 : tile(ntile), position(pos), movement(nmovement)
67 BrokenBrick::action(float elapsed_time)
69 position += movement * elapsed_time;
76 BrokenBrick::draw(DrawingContext& context)
78 if (tile->images.size() > 0)
79 context.draw_surface_part(tile->images[0],
80 Vector(rand() % 16, rand() % 16),
82 position, LAYER_OBJECTS + 1);
85 BouncyBrick::BouncyBrick(const Vector& pos)
86 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED),
87 shape(Sector::current()->solids->get_tile_id_at(pos))
93 BouncyBrick::action(float elapsed_time)
95 offset += offset_m * elapsed_time;
98 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
99 offset_m = BOUNCY_BRICK_SPEED;
104 shape.hidden = false;
110 BouncyBrick::draw(DrawingContext& context)
112 TileManager::instance()->
113 draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
116 FloatingScore::FloatingScore(const Vector& pos, int score)
120 snprintf(str, 10, "%d", score);
121 position.x -= strlen(str) * 8;
125 FloatingScore::action(float elapsed_time)
127 position.y -= 2 * elapsed_time;
134 FloatingScore::draw(DrawingContext& context)
136 context.draw_text(gold_text, str, position, LAYER_OBJECTS);
141 Sprite *img_trampoline;
143 Trampoline::Trampoline(LispReader& reader)
145 reader.read_float("x", base.x);
146 reader.read_float("y", base.y);
150 reader.read_float("power", power);
157 Trampoline::Trampoline(float x, float y)
171 Trampoline::write(LispWriter& writer)
173 writer.start_list("trampoline");
175 writer.write_float("x", base.x);
176 writer.write_float("y", base.y);
177 writer.write_float("power", power);
179 writer.end_list("trampoline");
183 Trampoline::draw(DrawingContext& context)
185 img_trampoline->set_frame(frame);
186 img_trampoline->draw(context, base, LAYER_OBJECTS);
191 Trampoline::action(float frame_ratio)
193 // TODO: Remove if we're too far off the screen
198 if (issolid(base.x + base.width/2, base.y + base.height))
200 base.y = int((base.y + base.height)/32) * 32 - base.height;
202 physic.enable_gravity(false);
203 physic.set_velocity_y(0.0f);
205 physic.set_velocity_x(0);
209 physic.enable_gravity(true);
212 else // Player is carrying us around
214 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
215 /* If we're holding the iceblock */
216 Player& tux = *Sector::current()->player;
217 Direction dir = tux.dir;
221 base.x = tux.base.x + 16;
222 base.y = tux.base.y + tux.base.height/1.5 - base.height;
224 else /* facing left */
226 base.x = tux.base.x - 16;
227 base.y = tux.base.y + tux.base.height/1.5 - base.height;
230 if(collision_object_map(base))
233 base.y = tux.base.y + tux.base.height/1.5 - base.height;
237 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
238 collision_swept_object_map(&old_base, &base);
242 Trampoline::collision(const MovingObject&, int)
248 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
250 Player* pplayer_c = NULL;
254 pplayer_c = (Player*) p_c_object;
256 if (type == COLLISION_NORMAL)
258 // Pick up if HELD (done in Player)
261 else if (type == COLLISION_SQUISH)
263 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
265 if (squish_amount < 24)
267 else if (squish_amount < 28)
269 else if (squish_amount < 30)
274 if (squish_amount < 20) {
275 pplayer_c->physic.set_velocity_y(power);
276 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
278 else if (pplayer_c->physic.get_velocity_y() < 0)
279 pplayer_c->physic.set_velocity_y(-squish_amount/32);
290 /* Flying Platform */
292 Sprite *img_flying_platform;
294 FlyingPlatform::FlyingPlatform(LispReader& reader)
296 reader.read_int_vector("x", pos_x);
297 reader.read_int_vector("y", pos_y);
300 reader.read_float("velocity", velocity);
310 float x = pos_x[point+1] - pos_x[point];
311 float y = pos_y[point+1] - pos_y[point];
312 vel_x = x*velocity / sqrt(x*x + y*y);
313 vel_y = -(velocity - vel_x);
318 FlyingPlatform::FlyingPlatform(int x, int y)
327 FlyingPlatform::write(LispWriter& writer)
329 writer.start_list("flying-trampoline");
331 writer.write_int_vector("x", pos_x);
332 writer.write_int_vector("y", pos_y);
333 writer.write_float("velocity", velocity);
335 writer.end_list("flying-trampoline");
339 FlyingPlatform::draw(DrawingContext& context)
341 img_flying_platform->draw(context, base, LAYER_OBJECTS);
345 FlyingPlatform::action(float frame_ratio)
347 // TODO: Remove if we're too far off the screen
352 if((unsigned)point+1 != pos_x.size())
354 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
355 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
356 pos_x[point] == pos_x[point+1]) &&
357 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
358 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
359 pos_y[point] == pos_y[point+1]))
363 float x = pos_x[point+1] - pos_x[point];
364 float y = pos_y[point+1] - pos_y[point];
365 vel_x = x*velocity / sqrt(x*x + y*y);
366 vel_y = -(velocity - vel_x);
376 if(pos_x[point+1] > base.x)
377 base.x += velocity * frame_ratio;
378 else if(pos_x[point+1] < base.x)
379 base.x -= velocity * frame_ratio;
381 if(pos_y[point+1] > base.y)
382 base.y += velocity * frame_ratio;
383 else if(pos_y[point+1] < base.y)
384 base.y -= velocity * frame_ratio;
387 base.x += vel_x * frame_ratio;
388 base.y += vel_y * frame_ratio;
392 FlyingPlatform::collision(const MovingObject&, int)
398 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
403 // Player* pplayer_c = NULL;
407 // pplayer_c = (Player*) p_c_object;
418 Sprite *img_smoke_cloud;
420 SmokeCloud::SmokeCloud(const Vector& pos)
427 SmokeCloud::action(float elapsed_time)
429 position.y -= 1.2 * elapsed_time;
436 SmokeCloud::draw(DrawingContext& context)
438 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
441 Particles::Particles(const Vector& epicenter, const Vector& velocity, const Vector& acceleration, int number, Color color_, int size_, int life_time)
442 : color(color_), size(size_), vel(velocity), accel(acceleration)
444 timer.start(life_time);
447 for(int p = 0; p < number; p++)
449 Particle* particle = new Particle;
450 particle->pos = epicenter;
451 particle->angle = (rand() % 360) * (M_PI / 180); // in radius
453 particles.push_back(particle);
457 Particles::~Particles()
460 for(std::vector<Particle*>::iterator i = particles.begin(); i < particles.end(); i++)
465 Particles::action(float elapsed_time)
467 vel.x += accel.x * elapsed_time;
468 vel.y += accel.y * elapsed_time;
471 for(int p = 0; p < particles.size(); p++)
473 particles[p]->pos.x += sin(particles[p]->angle) * vel.x * elapsed_time;
474 particles[p]->pos.y += cos(particles[p]->angle) * vel.y * elapsed_time;
482 Particles::draw(DrawingContext& context)
485 for(int p = 0; p < particles.size(); p++)
487 context.draw_filled_rect(particles[p]->pos, Vector(size,size), color, LAYER_OBJECTS+10);
491 void load_object_gfx()
493 img_trampoline = sprite_manager->load("trampoline");
494 img_trampoline->start_animation(0);
495 img_flying_platform = sprite_manager->load("flying_platform");
496 img_smoke_cloud = sprite_manager->load("stomp");