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.
31 #include "sprite_manager.h"
32 #include "resources.h"
34 Sprite* img_firebullet;
35 Sprite* img_icebullet;
38 Sprite* img_iceflower;
39 Sprite* img_fireflower;
42 #define GROWUP_SPEED 1.0f
44 #define BULLET_STARTING_YM 0
48 Bullet::init(float x, float y, float xm, Direction dir, int kind_)
57 base.xm = BULLET_XM + xm;
62 base.xm = -BULLET_XM + xm;
66 base.ym = BULLET_STARTING_YM;
74 std::vector<Bullet>& bullets = World::current()->bullets;
75 for(std::vector<Bullet>::iterator i = bullets.begin();
76 i != bullets.end(); ++i) {
87 Bullet::action(double frame_ratio)
93 base.x = base.x + base.xm * frame_ratio;
94 base.y = base.y + base.ym * frame_ratio;
96 collision_swept_object_map(&old_base,&base);
98 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
104 else if (base.ym < -9)
109 if(kind == FIRE_BULLET)
110 base.ym = base.ym + 0.5 * frame_ratio;
111 else if(kind == ICE_BULLET)
114 if (base.x < scroll_x ||
115 base.x > scroll_x + screen->w ||
116 base.y > screen->h ||
117 issolid(base.x + 4, base.y + 2) ||
118 issolid(base.x, base.y + 2) ||
129 if (base.x >= scroll_x - base.width &&
130 base.x <= scroll_x + screen->w)
132 if(kind == FIRE_BULLET)
133 img_firebullet->draw(base.x, base.y);
134 else if(kind == ICE_BULLET)
135 img_icebullet->draw(base.x, base.y);
140 Bullet::collision(int c_object)
142 if(c_object == CO_BADGUY) {
148 Upgrade::init(float x_, float y_, 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);
178 std::vector<Upgrade>& upgrades = World::current()->upgrades;
179 for(std::vector<Upgrade>::iterator i = upgrades.begin();
180 i != upgrades.end(); ++i) {
181 if( & (*i) == this) {
191 Upgrade::action(double frame_ratio)
193 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
194 || kind == UPGRADE_GROWUP) {
195 if (base.height < 32) {
197 base.height = base.height + 0.7 * frame_ratio;
205 /* Away from the screen? Kill it! */
206 if(base.x < scroll_x - OFFSCREEN_DISTANCE) {
210 if(base.y > screen->h) {
216 physic.apply(frame_ratio, base.x, base.y);
217 if(kind == UPGRADE_GROWUP) {
218 collision_swept_object_map(&old_base, &base);
222 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
224 if(physic.get_velocity_y() != 0) {
225 if(issolid(base.x, base.y + base.height)) {
226 base.y = int(base.y / 32) * 32;
228 if(kind == UPGRADE_GROWUP) {
229 physic.enable_gravity(false);
230 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
231 } else if(kind == UPGRADE_HERRING) {
232 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
236 if((physic.get_velocity_x() < 0
237 && !issolid(base.x+base.width, base.y + base.height))
238 || (physic.get_velocity_x() > 0
239 && !issolid(base.x, base.y + base.height))) {
240 physic.enable_gravity(true);
245 // horizontal bounce?
246 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
247 if ( (physic.get_velocity_x() < 0
248 && issolid(base.x, (int) base.y + base.height/2))
249 || (physic.get_velocity_x() > 0
250 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
251 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
252 dir = dir == LEFT ? RIGHT : LEFT;
262 if (base.height < 32)
266 dest.x = (int)(base.x);
267 dest.y = (int)(base.y + 32 - base.height);
269 dest.h = (int)base.height;
271 if (kind == UPGRADE_GROWUP)
272 img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
273 else if (kind == UPGRADE_ICEFLOWER)
274 img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
275 else if (kind == UPGRADE_FIREFLOWER)
276 img_fireflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
277 else if (kind == UPGRADE_HERRING)
278 img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
279 else if (kind == UPGRADE_1UP)
280 img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
284 if (kind == UPGRADE_GROWUP)
289 else if (kind == UPGRADE_ICEFLOWER)
294 else if (kind == UPGRADE_FIREFLOWER)
296 img_fireflower->draw(
299 else if (kind == UPGRADE_HERRING)
304 else if (kind == UPGRADE_1UP)
306 img_1up->draw( base.x, base.y);
312 Upgrade::bump(Player* player)
314 // these can't be bumped
315 if(kind != UPGRADE_GROWUP)
318 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
320 // determine new direction
321 if (player->base.x + player->base.width/2 > base.x + base.width/2)
326 // do a little jump and change direction
327 physic.set_velocity(-physic.get_velocity_x(), 3);
328 physic.enable_gravity(true);
332 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
334 Player* pplayer = NULL;
336 if(type == COLLISION_BUMP) {
337 if(c_object == CO_PLAYER)
338 pplayer = (Player*) p_c_object;
346 /* Remove the upgrade: */
348 /* p_c_object is CO_PLAYER, so assign it to pplayer */
349 pplayer = (Player*) p_c_object;
351 /* Affect the player: */
353 if (kind == UPGRADE_GROWUP)
355 play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
358 else if (kind == UPGRADE_FIREFLOWER)
360 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
362 pplayer->got_power = pplayer->FIRE_POWER;
364 else if (kind == UPGRADE_ICEFLOWER)
366 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
368 pplayer->got_power = pplayer->ICE_POWER;
370 else if (kind == UPGRADE_FIREFLOWER)
372 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
374 pplayer->got_power = pplayer->FIRE_POWER;
376 else if (kind == UPGRADE_HERRING)
378 play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
379 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
380 World::current()->play_music(HERRING_MUSIC);
382 else if (kind == UPGRADE_1UP)
384 if(player_status.lives < MAX_LIVES) {
385 player_status.lives++;
386 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
395 void load_special_gfx()
397 img_growup = sprite_manager->load("egg");
398 img_iceflower = sprite_manager->load("iceflower");
399 img_fireflower = sprite_manager->load("fireflower");
400 img_star = sprite_manager->load("star");
401 img_1up = sprite_manager->load("1up");
403 img_firebullet = sprite_manager->load("firebullet");
404 img_icebullet = sprite_manager->load("icebullet");
407 void free_special_gfx()