src/object/coin.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/coin.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "util/reader.hpp"
00021 #include "object/bouncy_coin.hpp"
00022 #include "object/player.hpp"
00023 #include "object/tilemap.hpp"
00024 #include "supertux/level.hpp"
00025 #include "supertux/object_factory.hpp"
00026 #include "supertux/sector.hpp"
00027 
00028 Coin::Coin(const Vector& pos)
00029   : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
00030     path(),
00031     walker(),
00032     offset(),
00033     from_tilemap(false)
00034 {
00035   sound_manager->preload("sounds/coin.wav");
00036 }
00037 
00038 Coin::Coin(const Vector& pos, TileMap* tilemap)
00039   : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
00040     path(boost::shared_ptr<Path>(tilemap->get_path())),
00041     walker(boost::shared_ptr<PathWalker>(tilemap->get_walker())),
00042     offset(),
00043     from_tilemap(true)
00044 {
00045   if(walker.get()) {
00046     Vector v = path->get_base();
00047     offset = pos - v;
00048   }
00049 
00050   sound_manager->preload("sounds/coin.wav");
00051 }
00052 
00053 Coin::Coin(const Reader& reader)
00054   : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
00055     path(),
00056     walker(),
00057     offset(),
00058     from_tilemap(false)
00059 {
00060   const lisp::Lisp* pathLisp = reader.get_lisp("path");
00061   if (pathLisp) {
00062     path.reset(new Path());
00063     path->read(*pathLisp);
00064     walker.reset(new PathWalker(path.get()));
00065     Vector v = path->get_base();
00066     set_pos(v);
00067   }
00068 
00069   sound_manager->preload("sounds/coin.wav");
00070 }
00071 
00072 void
00073 Coin::update(float elapsed_time)
00074 {
00075   // if we have a path to follow, follow it
00076   if (walker.get()) {
00077     Vector v = from_tilemap ? offset + walker->get_pos() : walker->advance(elapsed_time);
00078     movement = v - get_pos();
00079   }
00080 }
00081 
00082 void
00083 Coin::collect()
00084 {
00085   // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
00086   /*
00087     static Timer sound_timer;
00088     static int pitch_one = 128;
00089     static float last_pitch = 1;
00090     float pitch = 1;
00091 
00092     int tile = static_cast<int>(get_pos().y / 32);
00093 
00094     if (!sound_timer.started()) {
00095     pitch_one = tile;
00096     pitch = 1;
00097     last_pitch = 1;
00098     }
00099     else if (sound_timer.get_timegone() < 0.02) {
00100     pitch = last_pitch;
00101     }
00102     else
00103     {
00104     switch ((pitch_one - tile) % 7) {
00105     case -6:
00106     pitch = 1.0/2;
00107     break;
00108     case -5:
00109     pitch = 5.0/8;
00110     break;
00111     case -4:
00112     pitch = 4.0/6;
00113     break;
00114     case -3:
00115     pitch = 3.0/4;
00116     break;
00117     case -2:
00118     pitch = 5.0/6;
00119     break;
00120     case -1:
00121     pitch = 9.0/10;
00122     break;
00123     case 0:
00124     pitch = 1.0;
00125     break;
00126     case 1:
00127     pitch = 9.0/8;
00128     break;
00129     case 2:
00130     pitch = 5.0/4;
00131     break;
00132     case 3:
00133     pitch = 4.0/3;
00134     break;
00135     case 4:
00136     pitch = 3.0/2;
00137     break;
00138     case 5:
00139     pitch = 5.0/3;
00140     break;
00141     case 6:
00142     pitch = 9.0/5;
00143     break;
00144     }
00145     last_pitch = pitch;
00146     }
00147     sound_timer.start(1);
00148 
00149     SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
00150     soundSource->set_position(get_pos());
00151     soundSource->set_pitch(pitch);
00152     soundSource->play();
00153     sound_manager->manage_source(soundSource);
00154   */
00155   Sector::current()->player->get_status()->add_coins(1);
00156   Sector::current()->add_object(new BouncyCoin(get_pos()));
00157   Sector::current()->get_level()->stats.coins++;
00158   remove_me();
00159 }
00160 
00161 HitResponse
00162 Coin::collision(GameObject& other, const CollisionHit& )
00163 {
00164   Player* player = dynamic_cast<Player*>(&other);
00165   if(player == 0)
00166     return ABORT_MOVE;
00167 
00168   collect();
00169   return ABORT_MOVE;
00170 }
00171 
00172 /* EOF */

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