2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/coin.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "object/bouncy_coin.hpp"
21 #include "object/player.hpp"
22 #include "object/tilemap.hpp"
23 #include "supertux/level.hpp"
24 #include "supertux/object_factory.hpp"
25 #include "supertux/sector.hpp"
27 Coin::Coin(const Vector& pos)
28 : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
34 sound_manager->preload("sounds/coin.wav");
37 Coin::Coin(const Vector& pos, TileMap* tilemap)
38 : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
39 path(boost::shared_ptr<Path>(tilemap->get_path())),
40 walker(boost::shared_ptr<PathWalker>(tilemap->get_walker())),
45 Vector v = path->get_base();
49 sound_manager->preload("sounds/coin.wav");
52 Coin::Coin(const Reader& reader)
53 : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
59 const lisp::Lisp* pathLisp = reader.get_lisp("path");
61 path.reset(new Path());
62 path->read(*pathLisp);
63 walker.reset(new PathWalker(path.get()));
64 Vector v = path->get_base();
68 sound_manager->preload("sounds/coin.wav");
72 Coin::update(float elapsed_time)
74 // if we have a path to follow, follow it
76 Vector v = from_tilemap ? offset + walker->get_pos() : walker->advance(elapsed_time);
77 movement = v - get_pos();
84 // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
86 static Timer sound_timer;
87 static int pitch_one = 128;
88 static float last_pitch = 1;
91 int tile = static_cast<int>(get_pos().y / 32);
93 if (!sound_timer.started()) {
98 else if (sound_timer.get_timegone() < 0.02) {
103 switch ((pitch_one - tile) % 7) {
146 sound_timer.start(1);
148 SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
149 soundSource->set_position(get_pos());
150 soundSource->set_pitch(pitch);
152 sound_manager->manage_source(soundSource);
154 Sector::current()->player->get_status()->add_coins(1);
155 Sector::current()->add_object(new BouncyCoin(get_pos()));
156 Sector::current()->get_level()->stats.coins++;
161 Coin::collision(GameObject& other, const CollisionHit& )
163 Player* player = dynamic_cast<Player*>(&other);