Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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 PowerUp::PowerUp(const Reader& lisp) :
25   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING)
26 {
27   lisp.get("script", script);
28   no_physics = false;
29   lisp.get("disable-physics", no_physics);
30   physic.enable_gravity(true);
31   sound_manager->preload("sounds/grow.ogg");
32   sound_manager->preload("sounds/fire-flower.wav");
33 }
34
35 void
36 PowerUp::collision_solid(const CollisionHit& hit)
37 {
38   if(hit.bottom) {
39     physic.set_velocity_y(0);
40   }
41   if(hit.right || hit.left) {
42     physic.set_velocity_x(-physic.get_velocity_x());
43   }
44 }
45
46 HitResponse
47 PowerUp::collision(GameObject& other, const CollisionHit&)
48 {
49   Player* player = dynamic_cast<Player*>(&other);
50   if(player == 0)
51     return FORCE_MOVE;
52
53   if (script != "") {
54     std::istringstream stream(script);
55     Sector::current()->run_script(stream, "powerup-script");
56     remove_me();
57     return ABORT_MOVE;
58   }
59
60   // some defaults if no script has been set
61   if (sprite_name == "images/powerups/egg/egg.sprite") {
62     if(!player->add_bonus(GROWUP_BONUS, true))
63       return FORCE_MOVE;
64     sound_manager->play("sounds/grow.ogg");
65   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
66     if(!player->add_bonus(FIRE_BONUS, true))
67       return FORCE_MOVE;
68     sound_manager->play("sounds/fire-flower.wav");
69   } else if (sprite_name == "images/powerups/star/star.sprite") {
70     player->make_invincible();
71   } else if (sprite_name == "images/powerups/1up/1up.sprite") {
72     player->get_status()->add_coins(100);
73   }
74
75   remove_me();
76   return ABORT_MOVE;
77 }
78
79 void
80 PowerUp::update(float elapsed_time)
81 {
82   if (!no_physics)
83     movement = physic.get_movement(elapsed_time);
84 }
85
86 IMPLEMENT_FACTORY(PowerUp, "powerup");
87
88 /* EOF */