src/object/firefly.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 "object/firefly.hpp"
00018 
00019 #include <math.h>
00020 
00021 #include "audio/sound_manager.hpp"
00022 #include "math/random_generator.hpp"
00023 #include "object/player.hpp"
00024 #include "object/sprite_particle.hpp"
00025 #include "supertux/game_session.hpp"
00026 #include "supertux/object_factory.hpp"
00027 #include "supertux/sector.hpp"
00028 #include "util/reader.hpp"
00029 
00030 Firefly::Firefly(const Reader& lisp) :
00031    MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), 
00032    activated(false),
00033    initial_position()
00034 {
00035   initial_position = get_pos();
00036   if( !lisp.get( "sprite", sprite_name ) ){
00037     reactivate();
00038     return;
00039   }
00040   if( sprite_name == "" ){
00041     sprite_name = "images/objects/resetpoints/default-resetpoint.sprite";
00042     reactivate();
00043     return;
00044   }
00045   //Replace sprite
00046   sprite = sprite_manager->create( sprite_name );
00047   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00048   reactivate();
00049 
00050   //Load sound
00051   sound_manager->preload("sounds/savebell2.wav");
00052 }
00053 
00054 void
00055 Firefly::reactivate()
00056 {
00057   if(GameSession::current()->get_reset_point_pos() == initial_position){
00058     // TODO: && GameSession::current()->get_reset_point_sectorname() ==  <sector this firefly is in>
00059     // GameSession::current()->get_current_sector()->get_name() is not yet initialized.
00060     // Worst case a resetpoint in a different sector at the same position as the real
00061     // resetpoint the player is spawning is set to ringing, too. Until we can check the sector, too, dont set
00062     // activated = true; here.
00063     sprite->set_action("ringing");
00064   }
00065 }
00066 
00067 HitResponse
00068 Firefly::collision(GameObject& other, const CollisionHit& )
00069 {
00070   if(activated)
00071     return ABORT_MOVE;
00072 
00073   Player* player = dynamic_cast<Player*> (&other);
00074   if(player) {
00075     activated = true;
00076     // spawn some particles
00077     // TODO: provide convenience function in MovingSprite or MovingObject?
00078     for (int i = 0; i < 5; i++) {
00079       Vector ppos = bbox.get_middle();
00080       float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
00081       float velocity = graphicsRandom.randf(450, 900);
00082       float vx = sin(angle)*velocity;
00083       float vy = -cos(angle)*velocity;
00084       Vector pspeed = Vector(vx, vy);
00085       Vector paccel = Vector(0, 1000);
00086       Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
00087     }
00088 
00089     sound_manager->play("sounds/savebell2.wav");
00090 
00091     sprite->set_action("ringing");
00092     GameSession::current()->set_reset_point(Sector::current()->get_name(),
00093                                             initial_position);
00094   }
00095 
00096   return ABORT_MOVE;
00097 }
00098 
00099 /* EOF */

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