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;
41 #define GROWUP_SPEED 1.0f
43 #define BULLET_STARTING_YM 0
47 Bullet::init(float x, float y, float xm, Direction dir, int kind_)
56 base.xm = BULLET_XM + xm;
61 base.xm = -BULLET_XM + xm;
65 base.ym = BULLET_STARTING_YM;
73 std::vector<Bullet>& bullets = World::current()->bullets;
74 for(std::vector<Bullet>::iterator i = bullets.begin();
75 i != bullets.end(); ++i) {
86 Bullet::action(double frame_ratio)
92 base.x = base.x + base.xm * frame_ratio;
93 base.y = base.y + base.ym * frame_ratio;
95 collision_swept_object_map(&old_base,&base);
97 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
103 else if (base.ym < -9)
108 if(kind == FIRE_BULLET)
109 base.ym = base.ym + 0.5 * frame_ratio;
110 else if(kind == FIRE_BULLET)
113 if (base.x < scroll_x ||
114 base.x > scroll_x + screen->w ||
115 base.y > screen->h ||
116 issolid(base.x + 4, base.y + 2) ||
117 issolid(base.x, base.y + 2) ||
128 if (base.x >= scroll_x - base.width &&
129 base.x <= scroll_x + screen->w)
131 if(kind == FIRE_BULLET)
132 img_firebullet->draw(base.x, base.y);
133 else if(kind == FIRE_BULLET)
134 img_icebullet->draw(base.x, base.y);
139 Bullet::collision(int c_object)
141 if(c_object == CO_BADGUY) {
147 Upgrade::init(float x_, float y_, Direction dir_, UpgradeKind kind_)
159 physic.enable_gravity(false);
161 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
162 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
163 physic.enable_gravity(true);
165 } else if (kind == UPGRADE_ICEFLOWER) {
167 } else if (kind == UPGRADE_GROWUP) {
168 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
170 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
177 std::vector<Upgrade>& upgrades = World::current()->upgrades;
178 for(std::vector<Upgrade>::iterator i = upgrades.begin();
179 i != upgrades.end(); ++i) {
180 if( & (*i) == this) {
190 Upgrade::action(double frame_ratio)
192 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_GROWUP) {
193 if (base.height < 32) {
195 base.height = base.height + 0.7 * frame_ratio;
203 /* Away from the screen? Kill it! */
204 if(base.x < scroll_x - OFFSCREEN_DISTANCE) {
208 if(base.y > screen->h) {
214 physic.apply(frame_ratio, base.x, base.y);
215 if(kind == UPGRADE_GROWUP) {
216 collision_swept_object_map(&old_base, &base);
220 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
222 if(physic.get_velocity_y() != 0) {
223 if(issolid(base.x, base.y + base.height)) {
224 base.y = int(base.y / 32) * 32;
226 if(kind == UPGRADE_GROWUP) {
227 physic.enable_gravity(false);
228 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
229 } else if(kind == UPGRADE_HERRING) {
230 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
234 if((physic.get_velocity_x() < 0
235 && !issolid(base.x+base.width, base.y + base.height))
236 || (physic.get_velocity_x() > 0
237 && !issolid(base.x, base.y + base.height))) {
238 physic.enable_gravity(true);
243 // horizontal bounce?
244 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
245 if ( (physic.get_velocity_x() < 0
246 && issolid(base.x, (int) base.y + base.height/2))
247 || (physic.get_velocity_x() > 0
248 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
249 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
250 dir = dir == LEFT ? RIGHT : LEFT;
260 if (base.height < 32)
264 dest.x = (int)(base.x);
265 dest.y = (int)(base.y + 32 - base.height);
267 dest.h = (int)base.height;
269 if (kind == UPGRADE_GROWUP)
270 img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
271 else if (kind == UPGRADE_ICEFLOWER)
272 img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
273 else if (kind == UPGRADE_HERRING)
274 img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
275 else if (kind == UPGRADE_1UP)
276 img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
280 if (kind == UPGRADE_GROWUP)
285 else if (kind == UPGRADE_ICEFLOWER)
290 else if (kind == UPGRADE_HERRING)
295 else if (kind == UPGRADE_1UP)
297 img_1up->draw( base.x, base.y);
303 Upgrade::bump(Player* )
305 // these can't be bumped
306 if(kind != UPGRADE_GROWUP)
309 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
311 // do a little jump and change direction
312 physic.set_velocity(-physic.get_velocity_x(), 3);
313 dir = dir == LEFT ? RIGHT : LEFT;
314 physic.enable_gravity(true);
318 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
320 Player* pplayer = NULL;
322 if(type == COLLISION_BUMP) {
323 if(c_object == CO_PLAYER)
324 pplayer = (Player*) p_c_object;
332 /* Remove the upgrade: */
334 /* p_c_object is CO_PLAYER, so assign it to pplayer */
335 pplayer = (Player*) p_c_object;
337 /* Affect the player: */
339 if (kind == UPGRADE_GROWUP)
341 play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
344 else if (kind == UPGRADE_FIREFLOWER)
346 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
348 pplayer->got_power = pplayer->FIRE_POWER;
350 else if (kind == UPGRADE_ICEFLOWER)
352 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
354 pplayer->got_power = pplayer->ICE_POWER;
356 else if (kind == UPGRADE_HERRING)
358 play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
359 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
360 World::current()->play_music(HERRING_MUSIC);
362 else if (kind == UPGRADE_1UP)
364 if(player_status.lives < MAX_LIVES) {
365 player_status.lives++;
366 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
375 void load_special_gfx()
377 img_growup = sprite_manager->load("egg");
378 img_iceflower = sprite_manager->load("iceflower");
379 img_star = sprite_manager->load("star");
380 img_1up = sprite_manager->load("1up");
382 img_firebullet = sprite_manager->load("firebullet");
383 img_icebullet = sprite_manager->load("icebullet");
386 void free_special_gfx()