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