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
47 Bullet::Bullet(DisplayManager& display_manager, const Vector& pos, float xm,
50 display_manager.add_drawable(this, LAYER_OBJECTS);
59 physic.set_velocity_x(BULLET_XM + xm);
64 physic.set_velocity_x(-BULLET_XM + xm);
67 base.y = pos.y + base.height/2;
68 physic.set_velocity_y(-BULLET_STARTING_YM);
74 Bullet::action(float elapsed_time)
80 physic.apply(elapsed_time, base.x, base.y);
81 collision_swept_object_map(&old_base,&base);
83 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
86 physic.set_velocity_y(-physic.get_velocity_y());
90 if(kind == FIRE_BULLET)
91 // @not framerate independant :-/
92 physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
93 if(physic.get_velocity_y() > 9)
94 physic.set_velocity_y(9);
95 else if(physic.get_velocity_y() < -9)
96 physic.set_velocity_y(-9);
98 if (base.x < scroll_x ||
99 base.x > scroll_x + screen->w ||
100 base.y > screen->h ||
101 issolid(base.x + 4, base.y + 2) ||
102 issolid(base.x, base.y + 2) ||
110 Bullet::draw(ViewPort& viewport, int )
112 if(kind == FIRE_BULLET)
113 img_firebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
114 else if(kind == ICE_BULLET)
115 img_icebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
119 Bullet::collision(const MovingObject& , int)
125 Bullet::collision(int c_object)
127 if(c_object == CO_BADGUY) {
132 //---------------------------------------------------------------------------
134 Upgrade::Upgrade(DisplayManager& display_manager, const Vector& pos,
135 Direction dir_, UpgradeKind kind_)
137 display_manager.add_drawable(this, LAYER_OBJECTS);
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! */
184 if(base.x < scroll_x - OFFSCREEN_DISTANCE) {
188 if(base.y > screen->h) {
194 physic.apply(elapsed_time, base.x, base.y);
195 if(kind == UPGRADE_GROWUP) {
196 collision_swept_object_map(&old_base, &base);
200 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
202 if(physic.get_velocity_y() != 0) {
203 if(issolid(base.x, base.y + base.height)) {
204 base.y = int(base.y / 32) * 32;
206 if(kind == UPGRADE_GROWUP) {
207 physic.enable_gravity(false);
208 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
209 } else if(kind == UPGRADE_HERRING) {
210 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
214 if((physic.get_velocity_x() < 0
215 && !issolid(base.x+base.width, base.y + base.height))
216 || (physic.get_velocity_x() > 0
217 && !issolid(base.x, base.y + base.height))) {
218 physic.enable_gravity(true);
223 // horizontal bounce?
224 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
225 if ( (physic.get_velocity_x() < 0
226 && issolid(base.x, (int) base.y + base.height/2))
227 || (physic.get_velocity_x() > 0
228 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
229 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
230 dir = dir == LEFT ? RIGHT : LEFT;
236 Upgrade::draw(ViewPort& viewport, int)
240 if (base.height < 32)
244 dest.x = (int)(base.x - viewport.get_translation().x);
245 dest.y = (int)(base.y + 32 - base.height - viewport.get_translation().y);
247 dest.h = (int)base.height;
249 if (kind == UPGRADE_GROWUP)
250 img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
251 else if (kind == UPGRADE_ICEFLOWER)
252 img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
253 else if (kind == UPGRADE_FIREFLOWER)
254 img_fireflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
255 else if (kind == UPGRADE_HERRING)
256 img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
257 else if (kind == UPGRADE_1UP)
258 img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
262 if (kind == UPGRADE_GROWUP)
264 img_growup->draw(viewport.world2screen(Vector(base.x, base.y)));
266 else if (kind == UPGRADE_ICEFLOWER)
268 img_iceflower->draw(viewport.world2screen(Vector(base.x, base.y)));
270 else if (kind == UPGRADE_FIREFLOWER)
272 img_fireflower->draw(viewport.world2screen(Vector(base.x, base.y)));
274 else if (kind == UPGRADE_HERRING)
276 img_star->draw(viewport.world2screen(Vector(base.x, base.y)));
278 else if (kind == UPGRADE_1UP)
280 img_1up->draw(viewport.world2screen(Vector(base.x, base.y)));
286 Upgrade::bump(Player* player)
288 // these can't be bumped
289 if(kind != UPGRADE_GROWUP)
292 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
294 // determine new direction
295 if (player->base.x + player->base.width/2 > base.x + base.width/2)
300 // do a little jump and change direction
301 physic.set_velocity(-physic.get_velocity_x(), 3);
302 physic.enable_gravity(true);
306 Upgrade::collision(const MovingObject& , int)
312 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
314 Player* pplayer = NULL;
316 if(type == COLLISION_BUMP) {
317 if(c_object == CO_PLAYER)
318 pplayer = (Player*) p_c_object;
326 /* Remove the upgrade: */
328 /* p_c_object is CO_PLAYER, so assign it to pplayer */
329 pplayer = (Player*) p_c_object;
331 /* Affect the player: */
333 if (kind == UPGRADE_GROWUP)
335 play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
338 else if (kind == UPGRADE_FIREFLOWER)
340 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
342 pplayer->got_power = pplayer->FIRE_POWER;
344 else if (kind == UPGRADE_ICEFLOWER)
346 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
348 pplayer->got_power = pplayer->ICE_POWER;
350 else if (kind == UPGRADE_FIREFLOWER)
352 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
354 pplayer->got_power = pplayer->FIRE_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_fireflower = sprite_manager->load("fireflower");
380 img_star = sprite_manager->load("star");
381 img_1up = sprite_manager->load("1up");
383 img_firebullet = sprite_manager->load("firebullet");
384 img_icebullet = sprite_manager->load("icebullet");
387 void free_special_gfx()