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
27 #include "sprite_manager.h"
28 #include "resources.h"
30 #include "display_manager.h"
32 BouncyDistro::BouncyDistro(DisplayManager& displaymanager, const Vector& pos)
36 displaymanager.add_drawable(this, LAYER_OBJECTS);
40 BouncyDistro::action(float elapsed_time)
42 position.y += ym * elapsed_time;
44 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
50 BouncyDistro::draw(Camera& viewport, int )
52 img_distro[0]->draw(viewport.world2screen(position));
56 BrokenBrick::BrokenBrick(DisplayManager& displaymanager, Tile* ntile,
57 const Vector& pos, const Vector& nmovement)
58 : tile(ntile), position(pos), movement(nmovement)
60 displaymanager.add_drawable(this, LAYER_OBJECTS);
65 BrokenBrick::action(float elapsed_time)
67 position += movement * elapsed_time;
74 BrokenBrick::draw(Camera& viewport, int )
82 dest.x = (int)(position.x - viewport.get_translation().x);
83 dest.y = (int)(position.y - viewport.get_translation().y);
87 if (tile->images.size() > 0)
88 tile->images[0]->draw_part(src.x,src.y,dest.x,dest.y,dest.w,dest.h);
91 BouncyBrick::BouncyBrick(DisplayManager& displaymanager, const Vector& pos)
92 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED)
94 displaymanager.add_drawable(this, LAYER_OBJECTS);
95 shape = World::current()->get_level()->gettileid(pos.x, pos.y);
99 BouncyBrick::action(float elapsed_time)
101 offset += offset_m * elapsed_time;
104 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
105 offset_m = BOUNCY_BRICK_SPEED;
113 BouncyBrick::draw(Camera& viewport, int)
115 Tile::draw(viewport.world2screen(position + Vector(0, offset)), shape);
118 FloatingScore::FloatingScore(DisplayManager& displaymanager,
119 const Vector& pos, int score)
122 displaymanager.add_drawable(this, LAYER_OBJECTS);
124 snprintf(str, 10, "%d", score);
125 position.x -= strlen(str) * 8;
129 FloatingScore::action(float elapsed_time)
131 position.y -= 2 * elapsed_time;
138 FloatingScore::draw(Camera& viewport, int )
140 gold_text->draw(str, viewport.world2screen(position));
145 #define TRAMPOLINE_FRAMES 4
146 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
148 Trampoline::Trampoline(DisplayManager& displaymanager, LispReader& reader)
150 displaymanager.add_drawable(this, LAYER_OBJECTS);
152 reader.read_float("x", &base.x);
153 reader.read_float("y", &base.y);
157 reader.read_float("power", &power);
165 Trampoline::write(LispWriter& writer)
167 writer.start_list("trampoline");
169 writer.write_float("x", base.x);
170 writer.write_float("y", base.y);
171 writer.write_float("power", power);
173 writer.end_list("trampoline");
177 Trampoline::draw(Camera& viewport, int )
179 img_trampoline[frame]->draw(viewport.world2screen(Vector(base.x, base.y)));
184 Trampoline::action(float frame_ratio)
186 // TODO: Remove if we're too far off the screen
191 if (issolid(base.x + base.width/2, base.y + base.height))
193 base.y = int((base.y + base.height)/32) * 32 - base.height;
195 physic.enable_gravity(false);
196 physic.set_velocity_y(0.0f);
198 physic.set_velocity_x(0);
202 physic.enable_gravity(true);
205 else // Player is carrying us around
207 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
208 /* If we're holding the iceblock */
209 Player& tux = *World::current()->get_tux();
210 Direction dir = tux.dir;
214 base.x = tux.base.x + 16;
215 base.y = tux.base.y + tux.base.height/1.5 - base.height;
217 else /* facing left */
219 base.x = tux.base.x - 16;
220 base.y = tux.base.y + tux.base.height/1.5 - base.height;
223 if(collision_object_map(base))
226 base.y = tux.base.y + tux.base.height/1.5 - base.height;
230 physic.apply(frame_ratio, base.x, base.y);
231 collision_swept_object_map(&old_base, &base);
235 Trampoline::collision(const MovingObject&, int)
241 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
243 Player* pplayer_c = NULL;
247 pplayer_c = (Player*) p_c_object;
249 if (type == COLLISION_NORMAL)
251 // Pick up if HELD (done in Player)
254 else if (type == COLLISION_SQUISH)
256 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
258 if (squish_amount < 24)
260 else if (squish_amount < 28)
262 else if (squish_amount < 30)
267 if (squish_amount < 20) {
268 pplayer_c->physic.set_velocity_y(power);
269 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
271 else if (pplayer_c->physic.get_velocity_y() < 0)
272 pplayer_c->physic.set_velocity_y(-squish_amount/32);
283 /* Flying Platform */
285 Sprite *img_flying_platform;
287 FlyingPlatform::FlyingPlatform(DisplayManager& displaymanager, LispReader& reader)
289 displaymanager.add_drawable(this, LAYER_OBJECTS);
291 reader.read_int_vector("x", &pos_x);
292 reader.read_int_vector("y", &pos_y);
295 reader.read_float("velocity", &velocity);
305 float x = pos_x[point+1] - pos_x[point];
306 float y = pos_y[point+1] - pos_y[point];
307 vel_x = x*velocity / sqrt(x*x + y*y);
308 vel_y = -(velocity - vel_x);
314 FlyingPlatform::write(LispWriter& writer)
316 writer.start_list("flying-trampoline");
318 writer.write_int_vector("x", pos_x);
319 writer.write_int_vector("y", pos_y);
320 writer.write_float("velocity", velocity);
322 writer.end_list("flying-trampoline");
326 FlyingPlatform::draw(Camera& viewport, int )
328 img_flying_platform->draw(viewport.world2screen(Vector(base.x, base.y)));
332 FlyingPlatform::action(float frame_ratio)
334 // TODO: Remove if we're too far off the screen
339 if((unsigned)point+1 != pos_x.size())
341 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
342 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
343 pos_x[point] == pos_x[point+1]) &&
344 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
345 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
346 pos_y[point] == pos_y[point+1]))
350 float x = pos_x[point+1] - pos_x[point];
351 float y = pos_y[point+1] - pos_y[point];
352 vel_x = x*velocity / sqrt(x*x + y*y);
353 vel_y = -(velocity - vel_x);
363 if(pos_x[point+1] > base.x)
364 base.x += velocity * frame_ratio;
365 else if(pos_x[point+1] < base.x)
366 base.x -= velocity * frame_ratio;
368 if(pos_y[point+1] > base.y)
369 base.y += velocity * frame_ratio;
370 else if(pos_y[point+1] < base.y)
371 base.y -= velocity * frame_ratio;
374 base.x += vel_x * frame_ratio;
375 base.y += vel_y * frame_ratio;
379 FlyingPlatform::collision(const MovingObject&, int)
385 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
390 // Player* pplayer_c = NULL;
394 // pplayer_c = (Player*) p_c_object;
405 void load_object_gfx()
407 char sprite_name[16];
409 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
411 sprintf(sprite_name, "trampoline-%i", i+1);
412 img_trampoline[i] = sprite_manager->load(sprite_name);
415 img_flying_platform = sprite_manager->load("flying_platform");