36b035abb93cd3910b48a3029994ee3da1f9495f
[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   static Timer sound_timer; /**< time since last Coin was collected */
75   static int pitch_one = 128;
76   static float last_pitch = 1;
77   float pitch = 1;
78
79   int tile = static_cast<int>(get_pos().y / 32);
80
81   if (!sound_timer.started()) {
82     pitch_one = tile;
83     pitch = 1;
84     last_pitch = 1;
85   } 
86   else if (sound_timer.get_timegone() < 0.02) {
87     pitch = last_pitch;
88   } 
89   else 
90   {
91     switch ((pitch_one - tile) % 7) {
92       case -6:
93         pitch = 1.0/2;
94         break;
95       case -5:
96         pitch = 5.0/8;
97         break;
98       case -4:
99         pitch = 4.0/6;
100         break;
101       case -3:
102         pitch = 3.0/4;
103         break;
104       case -2:
105         pitch = 5.0/6;
106         break;
107       case -1:
108         pitch = 9.0/10;
109         break;
110       case 0:
111         pitch = 1.0;
112         break;
113       case 1:
114         pitch = 9.0/8;
115         break;
116       case 2:
117         pitch = 5.0/4;
118         break;
119       case 3:
120         pitch = 4.0/3;
121         break;
122       case 4:
123         pitch = 3.0/2;
124         break;
125       case 5:
126         pitch = 5.0/3;
127         break;
128       case 6:
129         pitch = 9.0/5;
130         break;
131     }
132     last_pitch = pitch;
133   }
134   sound_timer.start(1);
135
136   SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav");
137   soundSource->set_position(get_pos());
138   soundSource->set_pitch(pitch);
139   sound_manager->play_and_delete(soundSource);
140
141   Sector::current()->player->get_status()->add_coins(1, false);
142   Sector::current()->add_object(new BouncyCoin(get_pos()));
143   Sector::current()->get_level()->stats.coins++;
144   remove_me();
145 }
146
147 HitResponse
148 Coin::collision(GameObject& other, const CollisionHit& )
149 {
150   Player* player = dynamic_cast<Player*>(&other);
151   if(player == 0)
152     return ABORT_MOVE;
153
154   collect();
155   return ABORT_MOVE;
156 }
157
158 IMPLEMENT_FACTORY(Coin, "coin");