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"
37 Sprite* img_iceflower;
40 #define GROWUP_SPEED 1.0f
42 #define BULLET_STARTING_YM 0
46 Bullet::init(float x, float y, float xm, Direction dir)
55 base.xm = BULLET_XM + xm;
60 base.xm = -BULLET_XM + xm;
64 base.ym = BULLET_STARTING_YM;
71 std::vector<Bullet>& bullets = World::current()->bullets;
72 for(std::vector<Bullet>::iterator i = bullets.begin();
73 i != bullets.end(); ++i) {
84 Bullet::action(double frame_ratio)
90 base.x = base.x + base.xm * frame_ratio;
91 base.y = base.y + base.ym * frame_ratio;
93 collision_swept_object_map(&old_base,&base);
95 if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
101 else if (base.ym < -9)
106 base.ym = base.ym + 0.5 * frame_ratio;
108 if (base.x < scroll_x ||
109 base.x > scroll_x + screen->w ||
111 base.y > screen->h ||
112 issolid(base.x + 4, base.y + 2) ||
113 issolid(base.x, base.y + 2) ||
124 if (base.x >= scroll_x - base.width &&
125 base.x <= scroll_x + screen->w)
127 img_bullet->draw(base.x - scroll_x, base.y);
132 Bullet::collision(int c_object)
134 if(c_object == CO_BADGUY) {
140 Upgrade::init(float x_, float y_, Direction dir_, UpgradeKind kind_)
152 physic.enable_gravity(false);
154 if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
155 physic.set_velocity(dir == LEFT ? -1 : 1, 4);
156 physic.enable_gravity(true);
158 } else if (kind == UPGRADE_ICEFLOWER) {
160 } else if (kind == UPGRADE_GROWUP) {
161 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
163 physic.set_velocity(dir == LEFT ? -2 : 2, 0);
170 std::vector<Upgrade>& upgrades = World::current()->upgrades;
171 for(std::vector<Upgrade>::iterator i = upgrades.begin();
172 i != upgrades.end(); ++i) {
173 if( & (*i) == this) {
183 Upgrade::action(double frame_ratio)
185 if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_GROWUP) {
186 if (base.height < 32) {
188 base.height = base.height + 0.7 * frame_ratio;
196 /* Off screen? Kill it! */
197 if((base.x < scroll_x - OFFSCREEN_DISTANCE) || base.y > screen->h) {
203 physic.apply(frame_ratio, base.x, base.y);
204 if(kind == UPGRADE_GROWUP) {
205 collision_swept_object_map(&old_base, &base);
209 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
211 if(physic.get_velocity_y() != 0) {
212 if(issolid(base.x, base.y + base.height)) {
213 base.y = int(base.y / 32) * 32;
215 if(kind == UPGRADE_GROWUP) {
216 physic.enable_gravity(false);
217 physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
218 } else if(kind == UPGRADE_HERRING) {
219 physic.set_velocity(dir == LEFT ? -2 : 2, 3);
223 if((physic.get_velocity_x() < 0
224 && !issolid(base.x+base.width, base.y + base.height))
225 || (physic.get_velocity_x() > 0
226 && !issolid(base.x, base.y + base.height))) {
227 physic.enable_gravity(true);
232 // horizontal bounce?
233 if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
234 if ( (physic.get_velocity_x() < 0
235 && issolid(base.x, (int) base.y + base.height/2))
236 || (physic.get_velocity_x() > 0
237 && issolid(base.x + base.width, (int) base.y + base.height/2))) {
238 physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
239 dir = dir == LEFT ? RIGHT : LEFT;
248 if (base.height < 32)
252 dest.x = (int)(base.x - scroll_x);
253 dest.y = (int)(base.y + 32 - base.height);
255 dest.h = (int)base.height;
257 if (kind == UPGRADE_GROWUP)
258 img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
259 else if (kind == UPGRADE_ICEFLOWER)
260 img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
261 else if (kind == UPGRADE_HERRING)
262 img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
263 else if (kind == UPGRADE_1UP)
264 img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
268 if (kind == UPGRADE_GROWUP)
271 base.x - scroll_x, base.y);
273 else if (kind == UPGRADE_ICEFLOWER)
276 base.x - scroll_x, base.y);
278 else if (kind == UPGRADE_HERRING)
281 base.x - scroll_x, base.y);
283 else if (kind == UPGRADE_1UP)
285 img_1up->draw( base.x - scroll_x, base.y);
291 Upgrade::bump(Player* )
293 // these can't be bumped
294 if(kind != UPGRADE_GROWUP)
297 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
299 // do a little jump and change direction
300 physic.set_velocity(-physic.get_velocity_x(), 3);
301 dir = dir == LEFT ? RIGHT : LEFT;
302 physic.enable_gravity(true);
306 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
308 Player* pplayer = NULL;
310 if(type == COLLISION_BUMP) {
311 if(c_object == CO_PLAYER)
312 pplayer = (Player*) p_c_object;
320 /* Remove the upgrade: */
322 /* p_c_object is CO_PLAYER, so assign it to pplayer */
323 pplayer = (Player*) p_c_object;
325 /* Affect the player: */
327 if (kind == UPGRADE_GROWUP)
329 play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
331 pplayer->base.height = 64;
332 pplayer->base.y -= 32;
333 if(collision_object_map(pplayer->base))
335 pplayer->base.height = 32;
336 pplayer->base.y += 32;
337 pplayer->duck = true;
340 else if (kind == UPGRADE_ICEFLOWER)
342 play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
343 pplayer->got_coffee = true;
344 if (pplayer->size == SMALL)
347 pplayer->base.height = 64;
348 pplayer->base.y -= 32;
350 if(collision_object_map(pplayer->base))
352 pplayer->base.height = 32;
353 pplayer->base.y += 32;
354 pplayer->duck = true;
357 else if (kind == UPGRADE_HERRING)
359 play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
360 pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
361 World::current()->play_music(HERRING_MUSIC);
363 else if (kind == UPGRADE_1UP)
365 if(player_status.lives < MAX_LIVES) {
366 player_status.lives++;
367 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
376 void load_special_gfx()
378 img_growup = sprite_manager->load("egg");
379 img_iceflower = sprite_manager->load("iceflower");
380 img_star = sprite_manager->load("star");
381 img_1up = sprite_manager->load("1up");
383 img_bullet = sprite_manager->load("bullet");
386 void free_special_gfx()