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
29 #include "sprite_manager.h"
30 #include "resources.h"
34 BouncyDistro::BouncyDistro(const Vector& pos)
41 BouncyDistro::action(float elapsed_time)
43 position.y += ym * elapsed_time;
45 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
51 BouncyDistro::draw(DrawingContext& context)
53 context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
57 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
58 : tile(ntile), position(pos), movement(nmovement)
64 BrokenBrick::action(float elapsed_time)
66 position += movement * elapsed_time;
73 BrokenBrick::draw(DrawingContext& context)
75 if (tile->images.size() > 0)
76 context.draw_surface_part(tile->images[0],
77 Vector(rand() % 16, rand() % 16),
79 position, LAYER_OBJECTS + 1);
82 BouncyBrick::BouncyBrick(const Vector& pos)
83 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED),
84 shape(Sector::current()->solids->get_tile_id_at(pos))
90 BouncyBrick::action(float elapsed_time)
92 offset += offset_m * elapsed_time;
95 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
96 offset_m = BOUNCY_BRICK_SPEED;
101 shape.hidden = false;
107 BouncyBrick::draw(DrawingContext& context)
109 TileManager::instance()->
110 draw_tile(context, shape.id, position + Vector(0, offset), LAYER_TILES+1);
113 FloatingScore::FloatingScore(const Vector& pos, int score)
117 snprintf(str, 10, "%d", score);
118 position.x -= strlen(str) * 8;
122 FloatingScore::action(float elapsed_time)
124 position.y -= 2 * elapsed_time;
131 FloatingScore::draw(DrawingContext& context)
133 context.draw_text(gold_text, str, position, LAYER_OBJECTS);
138 #define TRAMPOLINE_FRAMES 4
139 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
141 Trampoline::Trampoline(LispReader& reader)
143 reader.read_float("x", base.x);
144 reader.read_float("y", base.y);
148 reader.read_float("power", power);
156 Trampoline::write(LispWriter& writer)
158 writer.start_list("trampoline");
160 writer.write_float("x", base.x);
161 writer.write_float("y", base.y);
162 writer.write_float("power", power);
164 writer.end_list("trampoline");
168 Trampoline::draw(DrawingContext& context)
170 img_trampoline[frame]->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
175 Trampoline::action(float frame_ratio)
177 // TODO: Remove if we're too far off the screen
182 if (issolid(base.x + base.width/2, base.y + base.height))
184 base.y = int((base.y + base.height)/32) * 32 - base.height;
186 physic.enable_gravity(false);
187 physic.set_velocity_y(0.0f);
189 physic.set_velocity_x(0);
193 physic.enable_gravity(true);
196 else // Player is carrying us around
198 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
199 /* If we're holding the iceblock */
200 Player& tux = *Sector::current()->player;
201 Direction dir = tux.dir;
205 base.x = tux.base.x + 16;
206 base.y = tux.base.y + tux.base.height/1.5 - base.height;
208 else /* facing left */
210 base.x = tux.base.x - 16;
211 base.y = tux.base.y + tux.base.height/1.5 - base.height;
214 if(collision_object_map(base))
217 base.y = tux.base.y + tux.base.height/1.5 - base.height;
221 physic.apply(frame_ratio, base.x, base.y);
222 collision_swept_object_map(&old_base, &base);
226 Trampoline::collision(const MovingObject&, int)
232 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
234 Player* pplayer_c = NULL;
238 pplayer_c = (Player*) p_c_object;
240 if (type == COLLISION_NORMAL)
242 // Pick up if HELD (done in Player)
245 else if (type == COLLISION_SQUISH)
247 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
249 if (squish_amount < 24)
251 else if (squish_amount < 28)
253 else if (squish_amount < 30)
258 if (squish_amount < 20) {
259 pplayer_c->physic.set_velocity_y(power);
260 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
262 else if (pplayer_c->physic.get_velocity_y() < 0)
263 pplayer_c->physic.set_velocity_y(-squish_amount/32);
274 /* Flying Platform */
276 Sprite *img_flying_platform;
278 FlyingPlatform::FlyingPlatform(LispReader& reader)
280 reader.read_int_vector("x", pos_x);
281 reader.read_int_vector("y", pos_y);
284 reader.read_float("velocity", velocity);
294 float x = pos_x[point+1] - pos_x[point];
295 float y = pos_y[point+1] - pos_y[point];
296 vel_x = x*velocity / sqrt(x*x + y*y);
297 vel_y = -(velocity - vel_x);
303 FlyingPlatform::write(LispWriter& writer)
305 writer.start_list("flying-trampoline");
307 writer.write_int_vector("x", pos_x);
308 writer.write_int_vector("y", pos_y);
309 writer.write_float("velocity", velocity);
311 writer.end_list("flying-trampoline");
315 FlyingPlatform::draw(DrawingContext& context)
317 img_flying_platform->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
321 FlyingPlatform::action(float frame_ratio)
323 // TODO: Remove if we're too far off the screen
328 if((unsigned)point+1 != pos_x.size())
330 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
331 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
332 pos_x[point] == pos_x[point+1]) &&
333 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
334 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
335 pos_y[point] == pos_y[point+1]))
339 float x = pos_x[point+1] - pos_x[point];
340 float y = pos_y[point+1] - pos_y[point];
341 vel_x = x*velocity / sqrt(x*x + y*y);
342 vel_y = -(velocity - vel_x);
352 if(pos_x[point+1] > base.x)
353 base.x += velocity * frame_ratio;
354 else if(pos_x[point+1] < base.x)
355 base.x -= velocity * frame_ratio;
357 if(pos_y[point+1] > base.y)
358 base.y += velocity * frame_ratio;
359 else if(pos_y[point+1] < base.y)
360 base.y -= velocity * frame_ratio;
363 base.x += vel_x * frame_ratio;
364 base.y += vel_y * frame_ratio;
368 FlyingPlatform::collision(const MovingObject&, int)
374 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
379 // Player* pplayer_c = NULL;
383 // pplayer_c = (Player*) p_c_object;
394 void load_object_gfx()
396 char sprite_name[16];
398 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
400 sprintf(sprite_name, "trampoline-%i", i+1);
401 img_trampoline[i] = sprite_manager->load(sprite_name);
404 img_flying_platform = sprite_manager->load("flying_platform");