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
28 #include "tile_manager.h"
31 #include "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 #define TRAMPOLINE_FRAMES 4
141 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
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);
158 Trampoline::write(LispWriter& writer)
160 writer.start_list("trampoline");
162 writer.write_float("x", base.x);
163 writer.write_float("y", base.y);
164 writer.write_float("power", power);
166 writer.end_list("trampoline");
170 Trampoline::draw(DrawingContext& context)
172 img_trampoline[frame]->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
177 Trampoline::action(float frame_ratio)
179 // TODO: Remove if we're too far off the screen
184 if (issolid(base.x + base.width/2, base.y + base.height))
186 base.y = int((base.y + base.height)/32) * 32 - base.height;
188 physic.enable_gravity(false);
189 physic.set_velocity_y(0.0f);
191 physic.set_velocity_x(0);
195 physic.enable_gravity(true);
198 else // Player is carrying us around
200 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
201 /* If we're holding the iceblock */
202 Player& tux = *Sector::current()->player;
203 Direction dir = tux.dir;
207 base.x = tux.base.x + 16;
208 base.y = tux.base.y + tux.base.height/1.5 - base.height;
210 else /* facing left */
212 base.x = tux.base.x - 16;
213 base.y = tux.base.y + tux.base.height/1.5 - base.height;
216 if(collision_object_map(base))
219 base.y = tux.base.y + tux.base.height/1.5 - base.height;
223 physic.apply(frame_ratio, base.x, base.y);
224 collision_swept_object_map(&old_base, &base);
228 Trampoline::collision(const MovingObject&, int)
234 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
236 Player* pplayer_c = NULL;
240 pplayer_c = (Player*) p_c_object;
242 if (type == COLLISION_NORMAL)
244 // Pick up if HELD (done in Player)
247 else if (type == COLLISION_SQUISH)
249 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
251 if (squish_amount < 24)
253 else if (squish_amount < 28)
255 else if (squish_amount < 30)
260 if (squish_amount < 20) {
261 pplayer_c->physic.set_velocity_y(power);
262 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
264 else if (pplayer_c->physic.get_velocity_y() < 0)
265 pplayer_c->physic.set_velocity_y(-squish_amount/32);
276 /* Flying Platform */
278 Sprite *img_flying_platform;
280 FlyingPlatform::FlyingPlatform(LispReader& reader)
282 reader.read_int_vector("x", pos_x);
283 reader.read_int_vector("y", pos_y);
286 reader.read_float("velocity", velocity);
296 float x = pos_x[point+1] - pos_x[point];
297 float y = pos_y[point+1] - pos_y[point];
298 vel_x = x*velocity / sqrt(x*x + y*y);
299 vel_y = -(velocity - vel_x);
305 FlyingPlatform::write(LispWriter& writer)
307 writer.start_list("flying-trampoline");
309 writer.write_int_vector("x", pos_x);
310 writer.write_int_vector("y", pos_y);
311 writer.write_float("velocity", velocity);
313 writer.end_list("flying-trampoline");
317 FlyingPlatform::draw(DrawingContext& context)
319 img_flying_platform->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
323 FlyingPlatform::action(float frame_ratio)
325 // TODO: Remove if we're too far off the screen
330 if((unsigned)point+1 != pos_x.size())
332 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
333 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
334 pos_x[point] == pos_x[point+1]) &&
335 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
336 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
337 pos_y[point] == pos_y[point+1]))
341 float x = pos_x[point+1] - pos_x[point];
342 float y = pos_y[point+1] - pos_y[point];
343 vel_x = x*velocity / sqrt(x*x + y*y);
344 vel_y = -(velocity - vel_x);
354 if(pos_x[point+1] > base.x)
355 base.x += velocity * frame_ratio;
356 else if(pos_x[point+1] < base.x)
357 base.x -= velocity * frame_ratio;
359 if(pos_y[point+1] > base.y)
360 base.y += velocity * frame_ratio;
361 else if(pos_y[point+1] < base.y)
362 base.y -= velocity * frame_ratio;
365 base.x += vel_x * frame_ratio;
366 base.y += vel_y * frame_ratio;
370 FlyingPlatform::collision(const MovingObject&, int)
376 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
381 // Player* pplayer_c = NULL;
385 // pplayer_c = (Player*) p_c_object;
396 void load_object_gfx()
398 char sprite_name[16];
400 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
402 sprintf(sprite_name, "trampoline-%i", i+1);
403 img_trampoline[i] = sprite_manager->load(sprite_name);
406 img_flying_platform = sprite_manager->load("flying_platform");