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[TRAMPOLINE_FRAMES];
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[frame]->draw(context, base, LAYER_OBJECTS);
189 Trampoline::action(float frame_ratio)
191 // TODO: Remove if we're too far off the screen
196 if (issolid(base.x + base.width/2, base.y + base.height))
198 base.y = int((base.y + base.height)/32) * 32 - base.height;
200 physic.enable_gravity(false);
201 physic.set_velocity_y(0.0f);
203 physic.set_velocity_x(0);
207 physic.enable_gravity(true);
210 else // Player is carrying us around
212 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
213 /* If we're holding the iceblock */
214 Player& tux = *Sector::current()->player;
215 Direction dir = tux.dir;
219 base.x = tux.base.x + 16;
220 base.y = tux.base.y + tux.base.height/1.5 - base.height;
222 else /* facing left */
224 base.x = tux.base.x - 16;
225 base.y = tux.base.y + tux.base.height/1.5 - base.height;
228 if(collision_object_map(base))
231 base.y = tux.base.y + tux.base.height/1.5 - base.height;
235 physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
236 collision_swept_object_map(&old_base, &base);
240 Trampoline::collision(const MovingObject&, int)
246 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
248 Player* pplayer_c = NULL;
252 pplayer_c = (Player*) p_c_object;
254 if (type == COLLISION_NORMAL)
256 // Pick up if HELD (done in Player)
259 else if (type == COLLISION_SQUISH)
261 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
263 if (squish_amount < 24)
265 else if (squish_amount < 28)
267 else if (squish_amount < 30)
272 if (squish_amount < 20) {
273 pplayer_c->physic.set_velocity_y(power);
274 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
276 else if (pplayer_c->physic.get_velocity_y() < 0)
277 pplayer_c->physic.set_velocity_y(-squish_amount/32);
288 /* Flying Platform */
290 Sprite *img_flying_platform;
292 FlyingPlatform::FlyingPlatform(LispReader& reader)
294 reader.read_int_vector("x", pos_x);
295 reader.read_int_vector("y", pos_y);
298 reader.read_float("velocity", velocity);
308 float x = pos_x[point+1] - pos_x[point];
309 float y = pos_y[point+1] - pos_y[point];
310 vel_x = x*velocity / sqrt(x*x + y*y);
311 vel_y = -(velocity - vel_x);
316 FlyingPlatform::FlyingPlatform(int x, int y)
325 FlyingPlatform::write(LispWriter& writer)
327 writer.start_list("flying-trampoline");
329 writer.write_int_vector("x", pos_x);
330 writer.write_int_vector("y", pos_y);
331 writer.write_float("velocity", velocity);
333 writer.end_list("flying-trampoline");
337 FlyingPlatform::draw(DrawingContext& context)
339 img_flying_platform->draw(context, base, LAYER_OBJECTS);
343 FlyingPlatform::action(float frame_ratio)
345 // TODO: Remove if we're too far off the screen
350 if((unsigned)point+1 != pos_x.size())
352 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
353 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
354 pos_x[point] == pos_x[point+1]) &&
355 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
356 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
357 pos_y[point] == pos_y[point+1]))
361 float x = pos_x[point+1] - pos_x[point];
362 float y = pos_y[point+1] - pos_y[point];
363 vel_x = x*velocity / sqrt(x*x + y*y);
364 vel_y = -(velocity - vel_x);
374 if(pos_x[point+1] > base.x)
375 base.x += velocity * frame_ratio;
376 else if(pos_x[point+1] < base.x)
377 base.x -= velocity * frame_ratio;
379 if(pos_y[point+1] > base.y)
380 base.y += velocity * frame_ratio;
381 else if(pos_y[point+1] < base.y)
382 base.y -= velocity * frame_ratio;
385 base.x += vel_x * frame_ratio;
386 base.y += vel_y * frame_ratio;
390 FlyingPlatform::collision(const MovingObject&, int)
396 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
401 // Player* pplayer_c = NULL;
405 // pplayer_c = (Player*) p_c_object;
416 Sprite *img_smoke_cloud;
418 SmokeCloud::SmokeCloud(const Vector& pos)
425 SmokeCloud::action(float elapsed_time)
427 position.y -= 1.2 * elapsed_time;
434 SmokeCloud::draw(DrawingContext& context)
436 img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
439 void load_object_gfx()
441 char sprite_name[16];
443 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
445 sprintf(sprite_name, "trampoline-%i", i+1);
446 img_trampoline[i] = sprite_manager->load(sprite_name);
449 img_flying_platform = sprite_manager->load("flying_platform");
451 img_smoke_cloud = sprite_manager->load("stomp");