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"
36 BouncyDistro::BouncyDistro(const Vector& pos)
43 BouncyDistro::action(float elapsed_time)
45 position.y += ym * elapsed_time;
47 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
53 BouncyDistro::draw(DrawingContext& context)
55 context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
59 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
60 : tile(ntile), position(pos), movement(nmovement)
66 BrokenBrick::action(float elapsed_time)
68 position += movement * elapsed_time;
75 BrokenBrick::draw(DrawingContext& context)
77 if (tile->images.size() > 0)
78 context.draw_surface_part(tile->images[0],
79 Vector(rand() % 16, rand() % 16),
81 position, LAYER_OBJECTS + 1);
84 BouncyBrick::BouncyBrick(const Vector& pos)
85 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED),
86 shape(Sector::current()->solids->get_tile_id_at(pos))
92 BouncyBrick::action(float elapsed_time)
94 offset += offset_m * elapsed_time;
97 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
98 offset_m = BOUNCY_BRICK_SPEED;
103 shape.hidden = false;
109 BouncyBrick::draw(DrawingContext& context)
111 TileManager::instance()->
112 draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
115 FloatingScore::FloatingScore(const Vector& pos, int score)
119 snprintf(str, 10, "%d", score);
120 position.x -= strlen(str) * 8;
124 FloatingScore::action(float elapsed_time)
126 position.y -= 2 * elapsed_time;
133 FloatingScore::draw(DrawingContext& context)
135 context.draw_text(gold_text, str, position, LAYER_OBJECTS);
140 Sprite *img_trampoline;
142 Trampoline::Trampoline(LispReader& reader)
144 reader.read_float("x", base.x);
145 reader.read_float("y", base.y);
149 reader.read_float("power", power);
156 Trampoline::Trampoline(float x, float y)
170 Trampoline::write(LispWriter& writer)
172 writer.start_list("trampoline");
174 writer.write_float("x", base.x);
175 writer.write_float("y", base.y);
176 writer.write_float("power", power);
178 writer.end_list("trampoline");
182 Trampoline::draw(DrawingContext& context)
184 img_trampoline->set_frame(frame);
185 img_trampoline->draw(context, base, LAYER_OBJECTS);
190 Trampoline::action(float frame_ratio)
192 // TODO: Remove if we're too far off the screen
197 if (issolid(base.x + base.width/2, base.y + base.height))
199 base.y = int((base.y + base.height)/32) * 32 - base.height;
201 physic.enable_gravity(false);
202 physic.set_velocity_y(0.0f);
204 physic.set_velocity_x(0);
208 physic.enable_gravity(true);
211 else // Player is carrying us around
213 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
214 /* If we're holding the iceblock */
215 Player& tux = *Sector::current()->player;
216 Direction dir = tux.dir;
220 base.x = tux.base.x + 16;
221 base.y = tux.base.y + tux.base.height/1.5 - base.height;
223 else /* facing left */
225 base.x = tux.base.x - 16;
226 base.y = tux.base.y + tux.base.height/1.5 - base.height;
229 if(collision_object_map(base))
232 base.y = tux.base.y + tux.base.height/1.5 - base.height;
236 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
237 collision_swept_object_map(&old_base, &base);
241 Trampoline::collision(const MovingObject&, int)
247 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
249 Player* pplayer_c = NULL;
253 pplayer_c = (Player*) p_c_object;
255 if (type == COLLISION_NORMAL)
257 // Pick up if HELD (done in Player)
260 else if (type == COLLISION_SQUISH)
262 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
264 if (squish_amount < 24)
266 else if (squish_amount < 28)
268 else if (squish_amount < 30)
273 if (squish_amount < 20) {
274 pplayer_c->physic.set_velocity_y(power);
275 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
277 else if (pplayer_c->physic.get_velocity_y() < 0)
278 pplayer_c->physic.set_velocity_y(-squish_amount/32);
289 /* Flying Platform */
291 Sprite *img_flying_platform;
293 FlyingPlatform::FlyingPlatform(LispReader& reader)
295 reader.read_int_vector("x", pos_x);
296 reader.read_int_vector("y", pos_y);
299 reader.read_float("velocity", velocity);
309 float x = pos_x[point+1] - pos_x[point];
310 float y = pos_y[point+1] - pos_y[point];
311 vel_x = x*velocity / sqrt(x*x + y*y);
312 vel_y = -(velocity - vel_x);
317 FlyingPlatform::FlyingPlatform(int x, int y)
326 FlyingPlatform::write(LispWriter& writer)
328 writer.start_list("flying-trampoline");
330 writer.write_int_vector("x", pos_x);
331 writer.write_int_vector("y", pos_y);
332 writer.write_float("velocity", velocity);
334 writer.end_list("flying-trampoline");
338 FlyingPlatform::draw(DrawingContext& context)
340 img_flying_platform->draw(context, base, LAYER_OBJECTS);
344 FlyingPlatform::action(float frame_ratio)
346 // TODO: Remove if we're too far off the screen
351 if((unsigned)point+1 != pos_x.size())
353 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
354 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
355 pos_x[point] == pos_x[point+1]) &&
356 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
357 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
358 pos_y[point] == pos_y[point+1]))
362 float x = pos_x[point+1] - pos_x[point];
363 float y = pos_y[point+1] - pos_y[point];
364 vel_x = x*velocity / sqrt(x*x + y*y);
365 vel_y = -(velocity - vel_x);
375 if(pos_x[point+1] > base.x)
376 base.x += velocity * frame_ratio;
377 else if(pos_x[point+1] < base.x)
378 base.x -= velocity * frame_ratio;
380 if(pos_y[point+1] > base.y)
381 base.y += velocity * frame_ratio;
382 else if(pos_y[point+1] < base.y)
383 base.y -= velocity * frame_ratio;
386 base.x += vel_x * frame_ratio;
387 base.y += vel_y * frame_ratio;
391 FlyingPlatform::collision(const MovingObject&, int)
397 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
402 // Player* pplayer_c = NULL;
406 // pplayer_c = (Player*) p_c_object;
417 Sprite *img_smoke_cloud;
419 SmokeCloud::SmokeCloud(const Vector& pos)
426 SmokeCloud::action(float elapsed_time)
428 position.y -= 1.2 * elapsed_time;
435 SmokeCloud::draw(DrawingContext& context)
437 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
440 void load_object_gfx()
442 img_trampoline = sprite_manager->load("trampoline");
443 img_trampoline->start_animation(0);
444 img_flying_platform = sprite_manager->load("flying_platform");
445 img_smoke_cloud = sprite_manager->load("stomp");