2684bcd2a0e816342d3a4dc9817d7110a1a1e8cb
[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 }
42
43 Coin::Coin(const lisp::Lisp& reader)
44         : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE)
45 {
46 }
47
48 void
49 Coin::collect()
50 {
51   // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
52   /*
53   static Timer sound_timer; 
54   static int pitch_one = 128;
55   static float last_pitch = 1;
56   float pitch = 1;
57
58   int tile = static_cast<int>(get_pos().y / 32);
59
60   if (!sound_timer.started()) {
61     pitch_one = tile;
62     pitch = 1;
63     last_pitch = 1;
64   } 
65   else if (sound_timer.get_timegone() < 0.02) {
66     pitch = last_pitch;
67   } 
68   else 
69   {
70     switch ((pitch_one - tile) % 7) {
71       case -6:
72         pitch = 1.0/2;
73         break;
74       case -5:
75         pitch = 5.0/8;
76         break;
77       case -4:
78         pitch = 4.0/6;
79         break;
80       case -3:
81         pitch = 3.0/4;
82         break;
83       case -2:
84         pitch = 5.0/6;
85         break;
86       case -1:
87         pitch = 9.0/10;
88         break;
89       case 0:
90         pitch = 1.0;
91         break;
92       case 1:
93         pitch = 9.0/8;
94         break;
95       case 2:
96         pitch = 5.0/4;
97         break;
98       case 3:
99         pitch = 4.0/3;
100         break;
101       case 4:
102         pitch = 3.0/2;
103         break;
104       case 5:
105         pitch = 5.0/3;
106         break;
107       case 6:
108         pitch = 9.0/5;
109         break;
110     }
111     last_pitch = pitch;
112   }
113   sound_timer.start(1);
114
115   SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
116   soundSource->set_position(get_pos());
117   soundSource->set_pitch(pitch);
118   soundSource->play();
119   sound_manager->manage_source(soundSource);
120 */
121   Sector::current()->player->get_status()->add_coins(1);
122   Sector::current()->add_object(new BouncyCoin(get_pos()));
123   Sector::current()->get_level()->stats.coins++;
124   remove_me();
125 }
126
127 HitResponse
128 Coin::collision(GameObject& other, const CollisionHit& )
129 {
130   Player* player = dynamic_cast<Player*>(&other);
131   if(player == 0)
132     return ABORT_MOVE;
133
134   collect();
135   return ABORT_MOVE;
136 }
137
138 IMPLEMENT_FACTORY(Coin, "coin");