3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "screen/screen.h"
33 #include "sprite_manager.h"
34 #include "resources.h"
36 Sprite* img_firebullet;
37 Sprite* img_icebullet;
40 Sprite* img_iceflower;
41 Sprite* img_fireflower;
44 #define GROWUP_SPEED 1.0f
46 #define BULLET_STARTING_YM 0
49 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
55 if (kind == ICE_BULLET)
56 life_count = 6; //ice-bullets get "extra lives" for bumping off walls
61 physic.set_velocity_x(BULLET_XM + xm);
66 physic.set_velocity_x(-BULLET_XM + xm);
70 physic.set_velocity_y(-BULLET_STARTING_YM);
76 Bullet::action(float elapsed_time)
82 physic.apply(elapsed_time, base.x, base.y);
83 collision_swept_object_map(&old_base,&base);
85 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
88 physic.set_velocity_y(-physic.get_velocity_y());
92 if(kind == FIRE_BULLET)
93 // @not framerate independant :-/
94 physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
95 if(physic.get_velocity_y() > 9)
96 physic.set_velocity_y(9);
97 else if(physic.get_velocity_y() < -9)
98 physic.set_velocity_y(-9);
101 Sector::current()->camera->get_translation().x;
103 Sector::current()->camera->get_translation().y;
104 if (base.x < scroll_x ||
105 base.x > scroll_x + screen->w ||
107 base.y > scroll_y + screen->h ||
112 if (issolid(base.x + 4, base.y + 2) ||
113 issolid(base.x, base.y + 2))
115 if (kind == FIRE_BULLET)
117 else if (kind == ICE_BULLET)
119 physic.set_velocity_x(physic.get_velocity_x() * -1);
125 Bullet::draw(DrawingContext& context)
127 Sprite* sprite = kind == FIRE_BULLET ? img_firebullet : img_icebullet;
129 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
133 Bullet::collision(const MovingObject& , int)
139 Bullet::collision(int c_object)
141 if(c_object == CO_BADGUY) {
146 //---------------------------------------------------------------------------
148 Upgrade::Upgrade(const Vector& pos, Direction dir_, UpgradeKind kind_)
160 physic.enable_gravity(false);
162 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
163 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
164 physic.enable_gravity(true);
166 } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
168 } else if (kind == UPGRADE_GROWUP) {
169 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
171 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
180 Upgrade::action(float elapsed_time)
182 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
183 || kind == UPGRADE_GROWUP) {
184 if (base.height < 32) {
186 base.height = base.height + 0.7 * elapsed_time;
194 /* Away from the screen? Kill it! */
196 Sector::current()->camera->get_translation().x;
198 Sector::current()->camera->get_translation().y;
200 if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
201 base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
202 base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
203 base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
210 physic.apply(elapsed_time, base.x, base.y);
211 if(kind == UPGRADE_GROWUP) {
212 collision_swept_object_map(&old_base, &base);
216 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
218 if(physic.get_velocity_y() != 0) {
219 if(issolid(base.x, base.y + base.height)) {
220 base.y = int(base.y / 32) * 32;
222 if(kind == UPGRADE_GROWUP) {
223 physic.enable_gravity(false);
224 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
225 } else if(kind == UPGRADE_HERRING) {
226 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
230 if((physic.get_velocity_x() < 0
231 && !issolid(base.x+base.width, base.y + base.height))
232 || (physic.get_velocity_x() > 0
233 && !issolid(base.x, base.y + base.height))) {
234 physic.enable_gravity(true);
239 // horizontal bounce?
240 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
241 if ( (physic.get_velocity_x() < 0
242 && issolid(base.x, (int) base.y + base.height/2))
243 || (physic.get_velocity_x() > 0
244 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
245 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
246 dir = dir == LEFT ? RIGHT : LEFT;
252 Upgrade::draw(DrawingContext& context)
256 case UPGRADE_GROWUP: sprite = img_growup; break;
257 case UPGRADE_ICEFLOWER: sprite = img_iceflower; break;
258 case UPGRADE_FIREFLOWER: sprite = img_fireflower; break;
259 case UPGRADE_HERRING: sprite = img_star; break;
260 case UPGRADE_1UP: sprite = img_1up; break;
262 assert(!"wrong type in Powerup::draw()");
265 if(base.height < 32) // still raising up?
266 sprite->draw(context, Vector(base.x, base.y + (32 - base.height)),
269 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
273 Upgrade::bump(Player* player)
275 // these can't be bumped
276 if(kind != UPGRADE_GROWUP)
279 sound_manager->play_sound(sounds[SND_BUMP_UPGRADE], Vector(base.x, base.y));
281 // determine new direction
282 if (player->base.x + player->base.width/2 > base.x + base.width/2)
287 // do a little jump and change direction
288 physic.set_velocity(-physic.get_velocity_x(), 3);
289 physic.enable_gravity(true);
293 Upgrade::collision(const MovingObject& , int)
299 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
301 Player* pplayer = NULL;
303 if(type == COLLISION_BUMP) {
304 if(c_object == CO_PLAYER)
305 pplayer = (Player*) p_c_object;
313 /* Remove the upgrade: */
315 /* p_c_object is CO_PLAYER, so assign it to pplayer */
316 pplayer = (Player*) p_c_object;
318 /* Affect the player: */
320 if (kind == UPGRADE_GROWUP)
322 sound_manager->play_sound(sounds[SND_EXCELLENT]);
325 else if (kind == UPGRADE_FIREFLOWER)
327 sound_manager->play_sound(sounds[SND_COFFEE]);
329 pplayer->got_power = pplayer->FIRE_POWER;
331 else if (kind == UPGRADE_ICEFLOWER)
333 sound_manager->play_sound(sounds[SND_COFFEE]);
335 pplayer->got_power = pplayer->ICE_POWER;
337 else if (kind == UPGRADE_FIREFLOWER)
339 sound_manager->play_sound(sounds[SND_COFFEE]);
341 pplayer->got_power = pplayer->FIRE_POWER;
343 else if (kind == UPGRADE_HERRING)
345 sound_manager->play_sound(sounds[SND_HERRING]);
346 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
347 Sector::current()->play_music(HERRING_MUSIC);
349 else if (kind == UPGRADE_1UP)
351 if(player_status.lives < MAX_LIVES) {
352 player_status.lives++;
353 sound_manager->play_sound(sounds[SND_LIFEUP]);
362 void load_special_gfx()
364 img_growup = sprite_manager->load("egg");
365 img_iceflower = sprite_manager->load("iceflower");
366 img_fireflower = sprite_manager->load("fireflower");
367 img_star = sprite_manager->load("star");
368 img_1up = sprite_manager->load("1up");
370 img_firebullet = sprite_manager->load("firebullet");
371 img_icebullet = sprite_manager->load("icebullet");
374 void free_special_gfx()