Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / growup.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 "audio/sound_manager.hpp"
18 #include "object/growup.hpp"
19 #include "object/player.hpp"
20
21 GrowUp::GrowUp(Direction direction) :
22   MovingSprite(Vector(0,0), "images/powerups/egg/egg.sprite", LAYER_OBJECTS, COLGROUP_MOVING)
23 {
24   physic.enable_gravity(true);
25   physic.set_velocity_x((direction == LEFT)?-100:100);
26   sound_manager->preload("sounds/grow.ogg");
27 }
28
29 void
30 GrowUp::update(float elapsed_time)
31 {
32   movement = physic.get_movement(elapsed_time);
33 }
34
35 void
36 GrowUp::collision_solid(const CollisionHit& hit)
37 {
38   if(hit.top)
39     physic.set_velocity_y(0);
40   if(hit.bottom && physic.get_velocity_y() > 0)
41     physic.set_velocity_y(0);
42   if(hit.left || hit.right)
43     physic.set_velocity_x(-physic.get_velocity_x());
44 }
45
46 HitResponse
47 GrowUp::collision(GameObject& other, const CollisionHit& hit )
48 {
49   Player* player = dynamic_cast<Player*>(&other);
50   if(player != 0) {
51     if(!player->add_bonus(GROWUP_BONUS, true)){
52       // Tux can't grow right now.
53       collision_solid( hit );
54       return ABORT_MOVE;
55     }
56
57     sound_manager->play("sounds/grow.ogg");
58     remove_me();
59
60     return ABORT_MOVE;
61   }
62
63   return FORCE_MOVE;
64 }
65
66 void
67 GrowUp::do_jump()
68 {
69   physic.set_velocity_y(-300);
70 }
71
72 /* EOF */