7fbebc7eb330778a7f1eee9a6b18d960fbea1319
[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
33 Coin::Coin(const Vector& pos)
34 {
35   bbox.set_pos(pos);
36   bbox.set_size(32, 32);
37   sprite = sprite_manager->create("images/objects/coin/coin.sprite");
38   set_group(COLGROUP_TOUCHABLE);
39 }
40
41 Coin::Coin(const lisp::Lisp& reader)
42 {
43   reader.get("x", bbox.p1.x);
44   reader.get("y", bbox.p1.y);
45   bbox.set_size(32, 32);
46   sprite = sprite_manager->create("images/objects/coin/coin.sprite");
47   set_group(COLGROUP_TOUCHABLE);
48 }
49
50 Coin::~Coin()
51 {
52   delete sprite;
53 }
54
55 void
56 Coin::update(float )
57 {
58 }
59
60 void
61 Coin::draw(DrawingContext& context)
62 {
63   sprite->draw(context, get_pos(), LAYER_TILES);
64 }
65
66 void
67 Coin::collect()
68 {
69   Sector::current()->player->get_status()->add_coins(1);
70   Sector::current()->add_object(new BouncyCoin(get_pos()));
71   Sector::current()->get_level()->stats.add_points(COINS_COLLECTED_STAT, 1);
72   remove_me();
73 }
74
75 HitResponse
76 Coin::collision(GameObject& other, const CollisionHit& )
77 {
78   Player* player = dynamic_cast<Player*>(&other);
79   if(player == 0)
80     return ABORT_MOVE;
81
82   collect();
83   return ABORT_MOVE;
84 }
85
86 IMPLEMENT_FACTORY(Coin, "coin");