Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / coin.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/coin.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/bouncy_coin.hpp"
21 #include "object/player.hpp"
22 #include "supertux/level.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 Coin::Coin(const Vector& pos)
27   : MovingSprite(pos, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE)
28 {
29   sound_manager->preload("sounds/coin.wav");
30 }
31
32 Coin::Coin(const Reader& reader)
33   : MovingSprite(reader, "images/objects/coin/coin.sprite", LAYER_TILES, COLGROUP_TOUCHABLE)
34 {
35   sound_manager->preload("sounds/coin.wav");
36 }
37
38 void
39 Coin::collect()
40 {
41   // TODO: commented out musical code. Maybe fork this for a special "MusicalCoin" object?
42   /*
43     static Timer sound_timer;
44     static int pitch_one = 128;
45     static float last_pitch = 1;
46     float pitch = 1;
47
48     int tile = static_cast<int>(get_pos().y / 32);
49
50     if (!sound_timer.started()) {
51     pitch_one = tile;
52     pitch = 1;
53     last_pitch = 1;
54     }
55     else if (sound_timer.get_timegone() < 0.02) {
56     pitch = last_pitch;
57     }
58     else
59     {
60     switch ((pitch_one - tile) % 7) {
61     case -6:
62     pitch = 1.0/2;
63     break;
64     case -5:
65     pitch = 5.0/8;
66     break;
67     case -4:
68     pitch = 4.0/6;
69     break;
70     case -3:
71     pitch = 3.0/4;
72     break;
73     case -2:
74     pitch = 5.0/6;
75     break;
76     case -1:
77     pitch = 9.0/10;
78     break;
79     case 0:
80     pitch = 1.0;
81     break;
82     case 1:
83     pitch = 9.0/8;
84     break;
85     case 2:
86     pitch = 5.0/4;
87     break;
88     case 3:
89     pitch = 4.0/3;
90     break;
91     case 4:
92     pitch = 3.0/2;
93     break;
94     case 5:
95     pitch = 5.0/3;
96     break;
97     case 6:
98     pitch = 9.0/5;
99     break;
100     }
101     last_pitch = pitch;
102     }
103     sound_timer.start(1);
104
105     SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
106     soundSource->set_position(get_pos());
107     soundSource->set_pitch(pitch);
108     soundSource->play();
109     sound_manager->manage_source(soundSource);
110   */
111   Sector::current()->player->get_status()->add_coins(1);
112   Sector::current()->add_object(new BouncyCoin(get_pos()));
113   Sector::current()->get_level()->stats.coins++;
114   remove_me();
115 }
116
117 HitResponse
118 Coin::collision(GameObject& other, const CollisionHit& )
119 {
120   Player* player = dynamic_cast<Player*>(&other);
121   if(player == 0)
122     return ABORT_MOVE;
123
124   collect();
125   return ABORT_MOVE;
126 }
127
128 IMPLEMENT_FACTORY(Coin, "coin");
129
130 /* EOF */