include fixes from ohnobinki, video_systems.cpp should fall back to SDL now if GL...
[supertux.git] / src / object / powerup.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "audio/sound_manager.hpp"
18 #include "object/player.hpp"
19 #include "object/powerup.hpp"
20 #include "supertux/object_factory.hpp"
21 #include "supertux/sector.hpp"
22 #include "util/reader.hpp"
23
24 #include <sstream>
25
26 PowerUp::PowerUp(const Reader& lisp) :
27   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING),
28   physic(),
29   script(),
30   no_physics()
31 {
32   lisp.get("script", script);
33   no_physics = false;
34   lisp.get("disable-physics", no_physics);
35   physic.enable_gravity(true);
36   sound_manager->preload("sounds/grow.ogg");
37   sound_manager->preload("sounds/fire-flower.wav");
38 }
39
40 void
41 PowerUp::collision_solid(const CollisionHit& hit)
42 {
43   if(hit.bottom) {
44     physic.set_velocity_y(0);
45   }
46   if(hit.right || hit.left) {
47     physic.set_velocity_x(-physic.get_velocity_x());
48   }
49 }
50
51 HitResponse
52 PowerUp::collision(GameObject& other, const CollisionHit&)
53 {
54   Player* player = dynamic_cast<Player*>(&other);
55   if(player == 0)
56     return FORCE_MOVE;
57
58   if (script != "") {
59     std::istringstream stream(script);
60     Sector::current()->run_script(stream, "powerup-script");
61     remove_me();
62     return ABORT_MOVE;
63   }
64
65   // some defaults if no script has been set
66   if (sprite_name == "images/powerups/egg/egg.sprite") {
67     if(!player->add_bonus(GROWUP_BONUS, true))
68       return FORCE_MOVE;
69     sound_manager->play("sounds/grow.ogg");
70   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
71     if(!player->add_bonus(FIRE_BONUS, true))
72       return FORCE_MOVE;
73     sound_manager->play("sounds/fire-flower.wav");
74   } else if (sprite_name == "images/powerups/star/star.sprite") {
75     player->make_invincible();
76   } else if (sprite_name == "images/powerups/1up/1up.sprite") {
77     player->get_status()->add_coins(100);
78   }
79
80   remove_me();
81   return ABORT_MOVE;
82 }
83
84 void
85 PowerUp::update(float elapsed_time)
86 {
87   if (!no_physics)
88     movement = physic.get_movement(elapsed_time);
89 }
90
91 /* EOF */