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)
85 shape = Sector::current()->solids->get_tile_id_at(pos);
89 BouncyBrick::action(float elapsed_time)
91 offset += offset_m * elapsed_time;
94 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
95 offset_m = BOUNCY_BRICK_SPEED;
103 BouncyBrick::draw(DrawingContext& context)
105 TileManager::instance()->
106 draw_tile(context, shape, position + Vector(0, offset), LAYER_TILES+1);
109 FloatingScore::FloatingScore(const Vector& pos, int score)
113 snprintf(str, 10, "%d", score);
114 position.x -= strlen(str) * 8;
118 FloatingScore::action(float elapsed_time)
120 position.y -= 2 * elapsed_time;
127 FloatingScore::draw(DrawingContext& context)
129 context.draw_text(gold_text, str, position, LAYER_OBJECTS);
134 #define TRAMPOLINE_FRAMES 4
135 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
137 Trampoline::Trampoline(LispReader& reader)
139 reader.read_float("x", base.x);
140 reader.read_float("y", base.y);
144 reader.read_float("power", power);
152 Trampoline::write(LispWriter& writer)
154 writer.start_list("trampoline");
156 writer.write_float("x", base.x);
157 writer.write_float("y", base.y);
158 writer.write_float("power", power);
160 writer.end_list("trampoline");
164 Trampoline::draw(DrawingContext& context)
166 img_trampoline[frame]->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
171 Trampoline::action(float frame_ratio)
173 // TODO: Remove if we're too far off the screen
178 if (issolid(base.x + base.width/2, base.y + base.height))
180 base.y = int((base.y + base.height)/32) * 32 - base.height;
182 physic.enable_gravity(false);
183 physic.set_velocity_y(0.0f);
185 physic.set_velocity_x(0);
189 physic.enable_gravity(true);
192 else // Player is carrying us around
194 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
195 /* If we're holding the iceblock */
196 Player& tux = *Sector::current()->player;
197 Direction dir = tux.dir;
201 base.x = tux.base.x + 16;
202 base.y = tux.base.y + tux.base.height/1.5 - base.height;
204 else /* facing left */
206 base.x = tux.base.x - 16;
207 base.y = tux.base.y + tux.base.height/1.5 - base.height;
210 if(collision_object_map(base))
213 base.y = tux.base.y + tux.base.height/1.5 - base.height;
217 physic.apply(frame_ratio, base.x, base.y);
218 collision_swept_object_map(&old_base, &base);
222 Trampoline::collision(const MovingObject&, int)
228 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
230 Player* pplayer_c = NULL;
234 pplayer_c = (Player*) p_c_object;
236 if (type == COLLISION_NORMAL)
238 // Pick up if HELD (done in Player)
241 else if (type == COLLISION_SQUISH)
243 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
245 if (squish_amount < 24)
247 else if (squish_amount < 28)
249 else if (squish_amount < 30)
254 if (squish_amount < 20) {
255 pplayer_c->physic.set_velocity_y(power);
256 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
258 else if (pplayer_c->physic.get_velocity_y() < 0)
259 pplayer_c->physic.set_velocity_y(-squish_amount/32);
270 /* Flying Platform */
272 Sprite *img_flying_platform;
274 FlyingPlatform::FlyingPlatform(LispReader& reader)
276 reader.read_int_vector("x", pos_x);
277 reader.read_int_vector("y", pos_y);
280 reader.read_float("velocity", velocity);
290 float x = pos_x[point+1] - pos_x[point];
291 float y = pos_y[point+1] - pos_y[point];
292 vel_x = x*velocity / sqrt(x*x + y*y);
293 vel_y = -(velocity - vel_x);
299 FlyingPlatform::write(LispWriter& writer)
301 writer.start_list("flying-trampoline");
303 writer.write_int_vector("x", pos_x);
304 writer.write_int_vector("y", pos_y);
305 writer.write_float("velocity", velocity);
307 writer.end_list("flying-trampoline");
311 FlyingPlatform::draw(DrawingContext& context)
313 img_flying_platform->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
317 FlyingPlatform::action(float frame_ratio)
319 // TODO: Remove if we're too far off the screen
324 if((unsigned)point+1 != pos_x.size())
326 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
327 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
328 pos_x[point] == pos_x[point+1]) &&
329 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
330 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
331 pos_y[point] == pos_y[point+1]))
335 float x = pos_x[point+1] - pos_x[point];
336 float y = pos_y[point+1] - pos_y[point];
337 vel_x = x*velocity / sqrt(x*x + y*y);
338 vel_y = -(velocity - vel_x);
348 if(pos_x[point+1] > base.x)
349 base.x += velocity * frame_ratio;
350 else if(pos_x[point+1] < base.x)
351 base.x -= velocity * frame_ratio;
353 if(pos_y[point+1] > base.y)
354 base.y += velocity * frame_ratio;
355 else if(pos_y[point+1] < base.y)
356 base.y -= velocity * frame_ratio;
359 base.x += vel_x * frame_ratio;
360 base.y += vel_y * frame_ratio;
364 FlyingPlatform::collision(const MovingObject&, int)
370 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
375 // Player* pplayer_c = NULL;
379 // pplayer_c = (Player*) p_c_object;
390 void load_object_gfx()
392 char sprite_name[16];
394 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
396 sprintf(sprite_name, "trampoline-%i", i+1);
397 img_trampoline[i] = sprite_manager->load(sprite_name);
400 img_flying_platform = sprite_manager->load("flying_platform");