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"
32 #include "sprite_manager.h"
33 #include "resources.h"
35 Sprite* img_firebullet;
36 Sprite* img_icebullet;
39 Sprite* img_iceflower;
40 Sprite* img_fireflower;
43 #define GROWUP_SPEED 1.0f
45 #define BULLET_STARTING_YM 0
48 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
57 physic.set_velocity_x(BULLET_XM + xm);
62 physic.set_velocity_x(-BULLET_XM + xm);
65 base.y = pos.y + base.height/2;
66 physic.set_velocity_y(-BULLET_STARTING_YM);
72 Bullet::action(float elapsed_time)
78 physic.apply(elapsed_time, base.x, base.y);
79 collision_swept_object_map(&old_base,&base);
81 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
84 physic.set_velocity_y(-physic.get_velocity_y());
88 if(kind == FIRE_BULLET)
89 // @not framerate independant :-/
90 physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
91 if(physic.get_velocity_y() > 9)
92 physic.set_velocity_y(9);
93 else if(physic.get_velocity_y() < -9)
94 physic.set_velocity_y(-9);
97 World::current()->camera->get_translation().x;
99 World::current()->camera->get_translation().y;
100 if (base.x < scroll_x ||
101 base.x > scroll_x + screen->w ||
103 base.y > scroll_y + screen->h ||
104 issolid(base.x + 4, base.y + 2) ||
105 issolid(base.x, base.y + 2) ||
113 Bullet::draw(DrawingContext& context)
115 Sprite* sprite = kind == FIRE_BULLET ? img_firebullet : img_icebullet;
117 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
121 Bullet::collision(const MovingObject& , int)
127 Bullet::collision(int c_object)
129 if(c_object == CO_BADGUY) {
134 //---------------------------------------------------------------------------
136 Upgrade::Upgrade(const Vector& pos, Direction dir_, UpgradeKind kind_)
148 physic.enable_gravity(false);
150 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
151 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
152 physic.enable_gravity(true);
154 } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
156 } else if (kind == UPGRADE_GROWUP) {
157 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
159 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
168 Upgrade::action(float elapsed_time)
170 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
171 || kind == UPGRADE_GROWUP) {
172 if (base.height < 32) {
174 base.height = base.height + 0.7 * elapsed_time;
182 /* Away from the screen? Kill it! */
184 World::current()->camera->get_translation().x;
186 World::current()->camera->get_translation().y;
188 if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
189 base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
190 base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
191 base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
198 physic.apply(elapsed_time, base.x, base.y);
199 if(kind == UPGRADE_GROWUP) {
200 collision_swept_object_map(&old_base, &base);
204 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
206 if(physic.get_velocity_y() != 0) {
207 if(issolid(base.x, base.y + base.height)) {
208 base.y = int(base.y / 32) * 32;
210 if(kind == UPGRADE_GROWUP) {
211 physic.enable_gravity(false);
212 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
213 } else if(kind == UPGRADE_HERRING) {
214 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
218 if((physic.get_velocity_x() < 0
219 && !issolid(base.x+base.width, base.y + base.height))
220 || (physic.get_velocity_x() > 0
221 && !issolid(base.x, base.y + base.height))) {
222 physic.enable_gravity(true);
227 // horizontal bounce?
228 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
229 if ( (physic.get_velocity_x() < 0
230 && issolid(base.x, (int) base.y + base.height/2))
231 || (physic.get_velocity_x() > 0
232 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
233 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
234 dir = dir == LEFT ? RIGHT : LEFT;
240 Upgrade::draw(DrawingContext& context)
244 case UPGRADE_GROWUP: sprite = img_growup; break;
245 case UPGRADE_ICEFLOWER: sprite = img_iceflower; break;
246 case UPGRADE_FIREFLOWER: sprite = img_fireflower; break;
247 case UPGRADE_HERRING: sprite = img_star; break;
248 case UPGRADE_1UP: sprite = img_1up; break;
250 assert(!"wrong type in Powerup::draw()");
253 if(base.height < 32) // still raising up?
254 sprite->draw(context, Vector(base.x, base.y + (32 - base.height)),
257 sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
261 Upgrade::bump(Player* player)
263 // these can't be bumped
264 if(kind != UPGRADE_GROWUP)
267 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
269 // determine new direction
270 if (player->base.x + player->base.width/2 > base.x + base.width/2)
275 // do a little jump and change direction
276 physic.set_velocity(-physic.get_velocity_x(), 3);
277 physic.enable_gravity(true);
281 Upgrade::collision(const MovingObject& , int)
287 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
289 Player* pplayer = NULL;
291 if(type == COLLISION_BUMP) {
292 if(c_object == CO_PLAYER)
293 pplayer = (Player*) p_c_object;
301 /* Remove the upgrade: */
303 /* p_c_object is CO_PLAYER, so assign it to pplayer */
304 pplayer = (Player*) p_c_object;
306 /* Affect the player: */
308 if (kind == UPGRADE_GROWUP)
310 play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
313 else if (kind == UPGRADE_FIREFLOWER)
315 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
317 pplayer->got_power = pplayer->FIRE_POWER;
319 else if (kind == UPGRADE_ICEFLOWER)
321 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
323 pplayer->got_power = pplayer->ICE_POWER;
325 else if (kind == UPGRADE_FIREFLOWER)
327 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
329 pplayer->got_power = pplayer->FIRE_POWER;
331 else if (kind == UPGRADE_HERRING)
333 play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
334 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
335 World::current()->play_music(HERRING_MUSIC);
337 else if (kind == UPGRADE_1UP)
339 if(player_status.lives < MAX_LIVES) {
340 player_status.lives++;
341 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
350 void load_special_gfx()
352 img_growup = sprite_manager->load("egg");
353 img_iceflower = sprite_manager->load("iceflower");
354 img_fireflower = sprite_manager->load("fireflower");
355 img_star = sprite_manager->load("star");
356 img_1up = sprite_manager->load("1up");
358 img_firebullet = sprite_manager->load("firebullet");
359 img_icebullet = sprite_manager->load("icebullet");
362 void free_special_gfx()