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