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_)
58 physic.set_velocity_x(BULLET_XM + xm);
63 physic.set_velocity_x(-BULLET_XM + xm);
66 base.y = pos.y + base.height/2;
67 physic.set_velocity_y(-BULLET_STARTING_YM);
73 Bullet::action(float elapsed_time)
79 physic.apply(elapsed_time, base.x, base.y);
80 collision_swept_object_map(&old_base,&base);
82 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
85 physic.set_velocity_y(-physic.get_velocity_y());
89 if(kind == FIRE_BULLET)
90 // @not framerate independant :-/
91 physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
92 if(physic.get_velocity_y() > 9)
93 physic.set_velocity_y(9);
94 else if(physic.get_velocity_y() < -9)
95 physic.set_velocity_y(-9);
98 Sector::current()->camera->get_translation().x;
100 Sector::current()->camera->get_translation().y;
101 if (base.x < scroll_x ||
102 base.x > scroll_x + screen->w ||
104 base.y > scroll_y + screen->h ||
105 issolid(base.x + 4, base.y + 2) ||
106 issolid(base.x, base.y + 2) ||
114 Bullet::draw(DrawingContext& context)
116 Sprite* sprite = kind == FIRE_BULLET ? img_firebullet : img_icebullet;
118 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
122 Bullet::collision(const MovingObject& , int)
128 Bullet::collision(int c_object)
130 if(c_object == CO_BADGUY) {
135 //---------------------------------------------------------------------------
137 Upgrade::Upgrade(const Vector& pos, Direction dir_, UpgradeKind kind_)
149 physic.enable_gravity(false);
151 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
152 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
153 physic.enable_gravity(true);
155 } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
157 } else if (kind == UPGRADE_GROWUP) {
158 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
160 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
169 Upgrade::action(float elapsed_time)
171 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
172 || kind == UPGRADE_GROWUP) {
173 if (base.height < 32) {
175 base.height = base.height + 0.7 * elapsed_time;
183 /* Away from the screen? Kill it! */
185 Sector::current()->camera->get_translation().x;
187 Sector::current()->camera->get_translation().y;
189 if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
190 base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
191 base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
192 base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
199 physic.apply(elapsed_time, base.x, base.y);
200 if(kind == UPGRADE_GROWUP) {
201 collision_swept_object_map(&old_base, &base);
205 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
207 if(physic.get_velocity_y() != 0) {
208 if(issolid(base.x, base.y + base.height)) {
209 base.y = int(base.y / 32) * 32;
211 if(kind == UPGRADE_GROWUP) {
212 physic.enable_gravity(false);
213 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
214 } else if(kind == UPGRADE_HERRING) {
215 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
219 if((physic.get_velocity_x() < 0
220 && !issolid(base.x+base.width, base.y + base.height))
221 || (physic.get_velocity_x() > 0
222 && !issolid(base.x, base.y + base.height))) {
223 physic.enable_gravity(true);
228 // horizontal bounce?
229 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
230 if ( (physic.get_velocity_x() < 0
231 && issolid(base.x, (int) base.y + base.height/2))
232 || (physic.get_velocity_x() > 0
233 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
234 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
235 dir = dir == LEFT ? RIGHT : LEFT;
241 Upgrade::draw(DrawingContext& context)
245 case UPGRADE_GROWUP: sprite = img_growup; break;
246 case UPGRADE_ICEFLOWER: sprite = img_iceflower; break;
247 case UPGRADE_FIREFLOWER: sprite = img_fireflower; break;
248 case UPGRADE_HERRING: sprite = img_star; break;
249 case UPGRADE_1UP: sprite = img_1up; break;
251 assert(!"wrong type in Powerup::draw()");
254 if(base.height < 32) // still raising up?
255 sprite->draw(context, Vector(base.x, base.y + (32 - base.height)),
258 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
262 Upgrade::bump(Player* player)
264 // these can't be bumped
265 if(kind != UPGRADE_GROWUP)
268 sound_manager->play_sound(sounds[SND_BUMP_UPGRADE], Vector(base.x, base.y));
270 // determine new direction
271 if (player->base.x + player->base.width/2 > base.x + base.width/2)
276 // do a little jump and change direction
277 physic.set_velocity(-physic.get_velocity_x(), 3);
278 physic.enable_gravity(true);
282 Upgrade::collision(const MovingObject& , int)
288 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
290 Player* pplayer = NULL;
292 if(type == COLLISION_BUMP) {
293 if(c_object == CO_PLAYER)
294 pplayer = (Player*) p_c_object;
302 /* Remove the upgrade: */
304 /* p_c_object is CO_PLAYER, so assign it to pplayer */
305 pplayer = (Player*) p_c_object;
307 /* Affect the player: */
309 if (kind == UPGRADE_GROWUP)
311 sound_manager->play_sound(sounds[SND_EXCELLENT]);
314 else if (kind == UPGRADE_FIREFLOWER)
316 sound_manager->play_sound(sounds[SND_COFFEE]);
318 pplayer->got_power = pplayer->FIRE_POWER;
320 else if (kind == UPGRADE_ICEFLOWER)
322 sound_manager->play_sound(sounds[SND_COFFEE]);
324 pplayer->got_power = pplayer->ICE_POWER;
326 else if (kind == UPGRADE_FIREFLOWER)
328 sound_manager->play_sound(sounds[SND_COFFEE]);
330 pplayer->got_power = pplayer->FIRE_POWER;
332 else if (kind == UPGRADE_HERRING)
334 sound_manager->play_sound(sounds[SND_HERRING]);
335 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
336 Sector::current()->play_music(HERRING_MUSIC);
338 else if (kind == UPGRADE_1UP)
340 if(player_status.lives < MAX_LIVES) {
341 player_status.lives++;
342 sound_manager->play_sound(sounds[SND_LIFEUP]);
351 void load_special_gfx()
353 img_growup = sprite_manager->load("egg");
354 img_iceflower = sprite_manager->load("iceflower");
355 img_fireflower = sprite_manager->load("fireflower");
356 img_star = sprite_manager->load("star");
357 img_1up = sprite_manager->load("1up");
359 img_firebullet = sprite_manager->load("firebullet");
360 img_icebullet = sprite_manager->load("icebullet");
363 void free_special_gfx()