src/object/powerup.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "audio/sound_manager.hpp"
00018 #include "object/player.hpp"
00019 #include "object/powerup.hpp"
00020 #include "supertux/object_factory.hpp"
00021 #include "supertux/sector.hpp"
00022 #include "util/reader.hpp"
00023 
00024 #include <sstream>
00025 
00026 PowerUp::PowerUp(const Reader& lisp) :
00027   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING),
00028   physic(),
00029   script(),
00030   no_physics()
00031 {
00032   lisp.get("script", script);
00033   no_physics = false;
00034   lisp.get("disable-physics", no_physics);
00035   physic.enable_gravity(true);
00036   sound_manager->preload("sounds/grow.ogg");
00037   sound_manager->preload("sounds/fire-flower.wav");
00038 }
00039 
00040 void
00041 PowerUp::collision_solid(const CollisionHit& hit)
00042 {
00043   if(hit.bottom) {
00044     physic.set_velocity_y(0);
00045   }
00046   if(hit.right || hit.left) {
00047     physic.set_velocity_x(-physic.get_velocity_x());
00048   }
00049 }
00050 
00051 HitResponse
00052 PowerUp::collision(GameObject& other, const CollisionHit&)
00053 {
00054   Player* player = dynamic_cast<Player*>(&other);
00055   if(player == 0)
00056     return FORCE_MOVE;
00057 
00058   if (script != "") {
00059     std::istringstream stream(script);
00060     Sector::current()->run_script(stream, "powerup-script");
00061     remove_me();
00062     return ABORT_MOVE;
00063   }
00064 
00065   // some defaults if no script has been set
00066   if (sprite_name == "images/powerups/egg/egg.sprite") {
00067     if(!player->add_bonus(GROWUP_BONUS, true))
00068       return FORCE_MOVE;
00069     sound_manager->play("sounds/grow.ogg");
00070   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
00071     if(!player->add_bonus(FIRE_BONUS, true))
00072       return FORCE_MOVE;
00073     sound_manager->play("sounds/fire-flower.wav");
00074   } else if (sprite_name == "images/powerups/star/star.sprite") {
00075     player->make_invincible();
00076   } else if (sprite_name == "images/powerups/1up/1up.sprite") {
00077     player->get_status()->add_coins(100);
00078   }
00079 
00080   remove_me();
00081   return ABORT_MOVE;
00082 }
00083 
00084 void
00085 PowerUp::update(float elapsed_time)
00086 {
00087   if (!no_physics)
00088     movement = physic.get_movement(elapsed_time);
00089 }
00090 
00091 /* EOF */

Generated on Mon Jun 9 03:38:20 2014 for SuperTux by  doxygen 1.5.1