fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / object / coin.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "coin.hpp"
23 #include "resources.hpp"
24 #include "video/drawing_context.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "player.hpp"
27 #include "sector.hpp"
28 #include "player_status.hpp"
29 #include "gameobjs.hpp"
30 #include "statistics.hpp"
31 #include "object_factory.hpp"
32 #include "level.hpp"
33 #include "random_generator.hpp"
34 #include "audio/sound_source.hpp"
35 #include "audio/sound_manager.hpp"
36 #include "timer.hpp"
37
38 Coin::Coin(const Vector& pos)
39         : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE)
40 {
41   sound_manager->preload("sounds/coin.wav");
42 }
43
44 Coin::Coin(const lisp::Lisp& reader)
45         : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE)
46 {
47   sound_manager->preload("sounds/coin.wav");
48 }
49
50 void
51 Coin::collect()
52 {
53   // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
54   /*
55   static Timer sound_timer;
56   static int pitch_one = 128;
57   static float last_pitch = 1;
58   float pitch = 1;
59
60   int tile = static_cast<int>(get_pos().y / 32);
61
62   if (!sound_timer.started()) {
63     pitch_one = tile;
64     pitch = 1;
65     last_pitch = 1;
66   }
67   else if (sound_timer.get_timegone() < 0.02) {
68     pitch = last_pitch;
69   }
70   else
71   {
72     switch ((pitch_one - tile) % 7) {
73       case -6:
74         pitch = 1.0/2;
75         break;
76       case -5:
77         pitch = 5.0/8;
78         break;
79       case -4:
80         pitch = 4.0/6;
81         break;
82       case -3:
83         pitch = 3.0/4;
84         break;
85       case -2:
86         pitch = 5.0/6;
87         break;
88       case -1:
89         pitch = 9.0/10;
90         break;
91       case 0:
92         pitch = 1.0;
93         break;
94       case 1:
95         pitch = 9.0/8;
96         break;
97       case 2:
98         pitch = 5.0/4;
99         break;
100       case 3:
101         pitch = 4.0/3;
102         break;
103       case 4:
104         pitch = 3.0/2;
105         break;
106       case 5:
107         pitch = 5.0/3;
108         break;
109       case 6:
110         pitch = 9.0/5;
111         break;
112     }
113     last_pitch = pitch;
114   }
115   sound_timer.start(1);
116
117   SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
118   soundSource->set_position(get_pos());
119   soundSource->set_pitch(pitch);
120   soundSource->play();
121   sound_manager->manage_source(soundSource);
122 */
123   Sector::current()->player->get_status()->add_coins(1);
124   Sector::current()->add_object(new BouncyCoin(get_pos()));
125   Sector::current()->get_level()->stats.coins++;
126   remove_me();
127 }
128
129 HitResponse
130 Coin::collision(GameObject& other, const CollisionHit& )
131 {
132   Player* player = dynamic_cast<Player*>(&other);
133   if(player == 0)
134     return ABORT_MOVE;
135
136   collect();
137   return ABORT_MOVE;
138 }
139
140 IMPLEMENT_FACTORY(Coin, "coin");