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
27 #include "sprite_manager.h"
28 #include "resources.h"
32 BouncyDistro::BouncyDistro(const Vector& pos)
39 BouncyDistro::action(float elapsed_time)
41 position.y += ym * elapsed_time;
43 ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
49 BouncyDistro::draw(DrawingContext& context)
51 context.draw_surface(img_distro[0], position, LAYER_OBJECTS);
55 BrokenBrick::BrokenBrick(Tile* ntile,const Vector& pos, const Vector& nmovement)
56 : tile(ntile), position(pos), movement(nmovement)
62 BrokenBrick::action(float elapsed_time)
64 position += movement * elapsed_time;
71 BrokenBrick::draw(DrawingContext& context)
73 if (tile->images.size() > 0)
74 context.draw_surface_part(tile->images[0],
75 Vector(rand() % 16, rand() % 16),
77 position, LAYER_OBJECTS + 1);
80 BouncyBrick::BouncyBrick(const Vector& pos)
81 : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED)
83 shape = Sector::current()->solids->get_tile_id_at(pos);
87 BouncyBrick::action(float elapsed_time)
89 offset += offset_m * elapsed_time;
92 if (offset < -BOUNCY_BRICK_MAX_OFFSET)
93 offset_m = BOUNCY_BRICK_SPEED;
101 BouncyBrick::draw(DrawingContext& context)
103 TileManager::instance()->
104 draw_tile(context, shape, position + Vector(0, offset), LAYER_TILES+1);
107 FloatingScore::FloatingScore(const Vector& pos, int score)
111 snprintf(str, 10, "%d", score);
112 position.x -= strlen(str) * 8;
116 FloatingScore::action(float elapsed_time)
118 position.y -= 2 * elapsed_time;
125 FloatingScore::draw(DrawingContext& context)
127 context.draw_text(gold_text, str, position, LAYER_OBJECTS);
132 #define TRAMPOLINE_FRAMES 4
133 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
135 Trampoline::Trampoline(LispReader& reader)
137 reader.read_float("x", base.x);
138 reader.read_float("y", base.y);
142 reader.read_float("power", power);
150 Trampoline::write(LispWriter& writer)
152 writer.start_list("trampoline");
154 writer.write_float("x", base.x);
155 writer.write_float("y", base.y);
156 writer.write_float("power", power);
158 writer.end_list("trampoline");
162 Trampoline::draw(DrawingContext& context)
164 img_trampoline[frame]->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
169 Trampoline::action(float frame_ratio)
171 // TODO: Remove if we're too far off the screen
176 if (issolid(base.x + base.width/2, base.y + base.height))
178 base.y = int((base.y + base.height)/32) * 32 - base.height;
180 physic.enable_gravity(false);
181 physic.set_velocity_y(0.0f);
183 physic.set_velocity_x(0);
187 physic.enable_gravity(true);
190 else // Player is carrying us around
192 /* FIXME: The trampoline object shouldn't know about pplayer objects. */
193 /* If we're holding the iceblock */
194 Player& tux = *Sector::current()->player;
195 Direction dir = tux.dir;
199 base.x = tux.base.x + 16;
200 base.y = tux.base.y + tux.base.height/1.5 - base.height;
202 else /* facing left */
204 base.x = tux.base.x - 16;
205 base.y = tux.base.y + tux.base.height/1.5 - base.height;
208 if(collision_object_map(base))
211 base.y = tux.base.y + tux.base.height/1.5 - base.height;
215 physic.apply(frame_ratio, base.x, base.y);
216 collision_swept_object_map(&old_base, &base);
220 Trampoline::collision(const MovingObject&, int)
226 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
228 Player* pplayer_c = NULL;
232 pplayer_c = (Player*) p_c_object;
234 if (type == COLLISION_NORMAL)
236 // Pick up if HELD (done in Player)
239 else if (type == COLLISION_SQUISH)
241 int squish_amount = (32 - (int)pplayer_c->base.y % 32);
243 if (squish_amount < 24)
245 else if (squish_amount < 28)
247 else if (squish_amount < 30)
252 if (squish_amount < 20) {
253 pplayer_c->physic.set_velocity_y(power);
254 pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
256 else if (pplayer_c->physic.get_velocity_y() < 0)
257 pplayer_c->physic.set_velocity_y(-squish_amount/32);
268 /* Flying Platform */
270 Sprite *img_flying_platform;
272 FlyingPlatform::FlyingPlatform(LispReader& reader)
274 reader.read_int_vector("x", pos_x);
275 reader.read_int_vector("y", pos_y);
278 reader.read_float("velocity", velocity);
288 float x = pos_x[point+1] - pos_x[point];
289 float y = pos_y[point+1] - pos_y[point];
290 vel_x = x*velocity / sqrt(x*x + y*y);
291 vel_y = -(velocity - vel_x);
297 FlyingPlatform::write(LispWriter& writer)
299 writer.start_list("flying-trampoline");
301 writer.write_int_vector("x", pos_x);
302 writer.write_int_vector("y", pos_y);
303 writer.write_float("velocity", velocity);
305 writer.end_list("flying-trampoline");
309 FlyingPlatform::draw(DrawingContext& context)
311 img_flying_platform->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
315 FlyingPlatform::action(float frame_ratio)
317 // TODO: Remove if we're too far off the screen
322 if((unsigned)point+1 != pos_x.size())
324 if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
325 (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
326 pos_x[point] == pos_x[point+1]) &&
327 ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
328 (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
329 pos_y[point] == pos_y[point+1]))
333 float x = pos_x[point+1] - pos_x[point];
334 float y = pos_y[point+1] - pos_y[point];
335 vel_x = x*velocity / sqrt(x*x + y*y);
336 vel_y = -(velocity - vel_x);
346 if(pos_x[point+1] > base.x)
347 base.x += velocity * frame_ratio;
348 else if(pos_x[point+1] < base.x)
349 base.x -= velocity * frame_ratio;
351 if(pos_y[point+1] > base.y)
352 base.y += velocity * frame_ratio;
353 else if(pos_y[point+1] < base.y)
354 base.y -= velocity * frame_ratio;
357 base.x += vel_x * frame_ratio;
358 base.y += vel_y * frame_ratio;
362 FlyingPlatform::collision(const MovingObject&, int)
368 FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
373 // Player* pplayer_c = NULL;
377 // pplayer_c = (Player*) p_c_object;
388 void load_object_gfx()
390 char sprite_name[16];
392 for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
394 sprintf(sprite_name, "trampoline-%i", i+1);
395 img_trampoline[i] = sprite_manager->load(sprite_name);
398 img_flying_platform = sprite_manager->load("flying_platform");