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.
29 #include "screen/screen.h"
35 #include "sprite_manager.h"
36 #include "resources.h"
38 Sprite* img_firebullet;
39 Sprite* img_icebullet;
42 Sprite* img_iceflower;
43 Sprite* img_fireflower;
46 #define GROWUP_SPEED 1.0f
48 #define BULLET_STARTING_YM 0
51 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
57 if (kind == ICE_BULLET)
58 life_count = 6; //ice-bullets get "extra lives" for bumping off walls
63 physic.set_velocity_x(BULLET_XM + xm);
68 physic.set_velocity_x(-BULLET_XM + xm);
72 physic.set_velocity_y(-BULLET_STARTING_YM);
78 Bullet::action(float elapsed_time)
84 physic.apply(elapsed_time, base.x, base.y);
85 collision_swept_object_map(&old_base,&base);
87 if (issolid(base.x+2, base.y + 4) || issolid(base.x+2, base.y))
90 physic.set_velocity_y(-physic.get_velocity_y());
94 if(kind == FIRE_BULLET)
95 // @not framerate independant :-/
96 physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
97 if(physic.get_velocity_y() > 9)
98 physic.set_velocity_y(9);
99 else if(physic.get_velocity_y() < -9)
100 physic.set_velocity_y(-9);
103 Sector::current()->camera->get_translation().x;
105 Sector::current()->camera->get_translation().y;
106 if (base.x < scroll_x ||
107 base.x > scroll_x + screen->w ||
109 base.y > scroll_y + screen->h ||
114 if (issolid(base.x + 4, base.y + 2) ||
115 issolid(base.x, base.y + 2))
117 if (kind == FIRE_BULLET)
119 else if (kind == ICE_BULLET)
121 physic.set_velocity_x(-physic.get_velocity_x());
122 //physic.set_velocity_y(-physic.get_velocity_y());
128 Bullet::draw(DrawingContext& context)
130 Sprite* sprite = kind == FIRE_BULLET ? img_firebullet : img_icebullet;
132 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
136 Bullet::collision(const MovingObject& , int)
142 Bullet::collision(int c_object)
144 if(c_object == CO_BADGUY) {
149 //---------------------------------------------------------------------------
151 Upgrade::Upgrade(const Vector& pos, Direction dir_, UpgradeKind kind_)
163 physic.enable_gravity(false);
165 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
166 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
167 physic.enable_gravity(true);
169 } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
171 } else if (kind == UPGRADE_GROWUP) {
172 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
174 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
183 Upgrade::action(float elapsed_time)
185 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
186 || kind == UPGRADE_GROWUP) {
187 if (base.height < 32) {
189 base.height = base.height + 0.7 * elapsed_time;
197 /* Away from the screen? Kill it! */
199 Sector::current()->camera->get_translation().x;
201 Sector::current()->camera->get_translation().y;
203 if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
204 base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
205 base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
206 base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
213 physic.apply(elapsed_time, base.x, base.y);
214 if(kind == UPGRADE_GROWUP) {
215 collision_swept_object_map(&old_base, &base);
219 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
221 if(physic.get_velocity_y() != 0) {
222 if(issolid(base.x, base.y + base.height)) {
223 base.y = int(base.y / 32) * 32;
225 if(kind == UPGRADE_GROWUP) {
226 physic.enable_gravity(false);
227 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
228 } else if(kind == UPGRADE_HERRING) {
229 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
233 if((physic.get_velocity_x() < 0
234 && !issolid(base.x+base.width, base.y + base.height))
235 || (physic.get_velocity_x() > 0
236 && !issolid(base.x, base.y + base.height))) {
237 physic.enable_gravity(true);
242 // horizontal bounce?
243 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
244 if ( (physic.get_velocity_x() < 0
245 && issolid(base.x, (int) base.y + base.height/2))
246 || (physic.get_velocity_x() > 0
247 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
248 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
249 dir = dir == LEFT ? RIGHT : LEFT;
255 Upgrade::draw(DrawingContext& context)
259 case UPGRADE_GROWUP: sprite = img_growup; break;
260 case UPGRADE_ICEFLOWER: sprite = img_iceflower; break;
261 case UPGRADE_FIREFLOWER: sprite = img_fireflower; break;
262 case UPGRADE_HERRING: sprite = img_star; break;
263 case UPGRADE_1UP: sprite = img_1up; break;
265 assert(!"wrong type in Powerup::draw()");
268 if(base.height < 32) // still raising up?
269 sprite->draw(context, Vector(base.x, base.y + (32 - base.height)),
272 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
276 Upgrade::bump(Player* player)
278 // these can't be bumped
279 if(kind != UPGRADE_GROWUP)
282 sound_manager->play_sound(sounds[SND_BUMP_UPGRADE], Vector(base.x, base.y));
284 // determine new direction
285 Direction old_dir = dir;
286 if (player->base.x + player->base.width/2 > base.x + base.width/2)
291 // do a little jump and change direction (if necessary)
293 physic.set_velocity(-physic.get_velocity_x(), 3);
295 physic.set_velocity_y(3);
297 physic.enable_gravity(true);
301 Upgrade::collision(const MovingObject& , int)
307 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
309 Player* pplayer = NULL;
311 if(type == COLLISION_BUMP) {
312 if(c_object == CO_PLAYER)
313 pplayer = (Player*) p_c_object;
321 /* Remove the upgrade: */
323 /* p_c_object is CO_PLAYER, so assign it to pplayer */
324 pplayer = (Player*) p_c_object;
326 /* Affect the player: */
328 if (kind == UPGRADE_GROWUP)
330 sound_manager->play_sound(sounds[SND_EXCELLENT]);
333 else if (kind == UPGRADE_FIREFLOWER)
335 sound_manager->play_sound(sounds[SND_COFFEE]);
337 pplayer->got_power = pplayer->FIRE_POWER;
339 else if (kind == UPGRADE_ICEFLOWER)
341 sound_manager->play_sound(sounds[SND_COFFEE]);
343 pplayer->got_power = pplayer->ICE_POWER;
345 else if (kind == UPGRADE_FIREFLOWER)
347 sound_manager->play_sound(sounds[SND_COFFEE]);
349 pplayer->got_power = pplayer->FIRE_POWER;
351 else if (kind == UPGRADE_HERRING)
353 sound_manager->play_sound(sounds[SND_HERRING]);
354 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
355 Sector::current()->play_music(HERRING_MUSIC);
357 else if (kind == UPGRADE_1UP)
359 if(player_status.lives < MAX_LIVES) {
360 player_status.lives++;
361 sound_manager->play_sound(sounds[SND_LIFEUP]);
370 void load_special_gfx()
372 img_growup = sprite_manager->load("egg");
373 img_iceflower = sprite_manager->load("iceflower");
374 img_fireflower = sprite_manager->load("fireflower");
375 img_star = sprite_manager->load("star");
376 img_1up = sprite_manager->load("1up");
378 img_firebullet = sprite_manager->load("firebullet");
379 img_icebullet = sprite_manager->load("icebullet");
382 void free_special_gfx()