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 "util/reader.hpp"
21 #include "object/bouncy_coin.hpp"
22 #include "object/player.hpp"
23 #include "object/tilemap.hpp"
24 #include "supertux/level.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
28 Coin::Coin(const Vector& pos)
29 : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
36 sound_manager->preload("sounds/coin.wav");
39 Coin::Coin(const Vector& pos, TileMap* tilemap)
40 : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
41 path(boost::shared_ptr<Path>(tilemap->get_path())),
42 walker(boost::shared_ptr<PathWalker>(tilemap->get_walker())),
48 Vector v = path->get_base();
52 sound_manager->preload("sounds/coin.wav");
55 Coin::Coin(const Reader& reader)
56 : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_OBJECTS - 1, COLGROUP_TOUCHABLE),
63 const lisp::Lisp* pathLisp = reader.get_lisp("path");
65 path.reset(new Path());
66 path->read(*pathLisp);
67 walker.reset(new PathWalker(path.get()));
68 Vector v = path->get_base();
72 sound_manager->preload("sounds/coin.wav");
76 Coin::update(float elapsed_time)
78 // if we have a path to follow, follow it
80 Vector v = from_tilemap ? offset + walker->get_pos() : walker->advance(elapsed_time);
81 movement = v - get_pos();
88 // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
90 static Timer sound_timer;
91 static int pitch_one = 128;
92 static float last_pitch = 1;
95 int tile = static_cast<int>(get_pos().y / 32);
97 if (!sound_timer.started()) {
102 else if (sound_timer.get_timegone() < 0.02) {
107 switch ((pitch_one - tile) % 7) {
150 sound_timer.start(1);
152 SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
153 soundSource->set_position(get_pos());
154 soundSource->set_pitch(pitch);
156 sound_manager->manage_source(soundSource);
158 Sector::current()->player->get_status()->add_coins(1);
159 Sector::current()->add_object(new BouncyCoin(get_pos()));
160 Sector::current()->get_level()->stats.coins++;
165 Coin::collision(GameObject& other, const CollisionHit& )
167 Player* player = dynamic_cast<Player*>(&other);
175 /* The following defines a coin subject to gravity */
176 HeavyCoin::HeavyCoin(const Vector& pos, const Vector& init_velocity)
180 physic.enable_gravity(true);
181 sound_manager->preload("sounds/coin2.ogg");
182 set_group(COLGROUP_MOVING);
183 physic.set_velocity(init_velocity);
186 HeavyCoin::HeavyCoin(const Reader& reader)
190 physic.enable_gravity(true);
191 sound_manager->preload("sounds/coin2.ogg");
192 set_group(COLGROUP_MOVING);
196 HeavyCoin::update(float elapsed_time)
199 movement = physic.get_movement(elapsed_time);
203 HeavyCoin::collision_solid(const CollisionHit& hit)
205 int clink_threshold = 100; // sets the minimum speed needed to result in collision noise
206 //TODO: colliding HeavyCoins should have their own unique sound
209 if(physic.get_velocity_y() > clink_threshold)
210 sound_manager->play("sounds/coin2.ogg");
211 if(physic.get_velocity_y() > 200) {// lets some coins bounce
212 physic.set_velocity_y(-99);
214 physic.set_velocity_y(0);
215 physic.set_velocity_x(0);
218 if(hit.right || hit.left) {
219 if(physic.get_velocity_x() > clink_threshold || physic.get_velocity_x() < clink_threshold)
220 sound_manager->play("sounds/coin2.ogg");
221 physic.set_velocity_x(-physic.get_velocity_x());
224 if(physic.get_velocity_y() < clink_threshold)
225 sound_manager->play("sounds/coin2.ogg");
226 physic.set_velocity_y(-physic.get_velocity_y());