WIP improvement: Egg powerup rotation
[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 #include "sprite/sprite.hpp"
21 #include "sprite/sprite_manager.hpp"
22
23 GrowUp::GrowUp(Direction direction) :
24   MovingSprite(Vector(0,0), "images/powerups/egg/egg.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
25   physic(),
26   light(0.0f,0.0f,0.0f),
27   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
28 {
29   physic.enable_gravity(true);
30   physic.set_velocity_x((direction == LEFT)?-100:100);
31   sound_manager->preload("sounds/grow.ogg");
32   //set light for glow effect
33   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
34   lightsprite->set_color(Color(0.2f, 0.2f, 0.0f));
35   
36   sprite->set_action((direction == LEFT) ? "left" : "right");
37 }
38
39 void
40 GrowUp::update(float elapsed_time)
41 {
42   movement = physic.get_movement(elapsed_time);
43 }
44
45 void
46 GrowUp::draw(DrawingContext& context){
47   //Draw the Sprite.
48   MovingSprite::draw(context);
49   //Draw the light when dark
50   context.get_light( get_bbox().get_middle(), &light );
51   if (light.red + light.green < 2.0){
52     context.push_target();
53     context.set_target(DrawingContext::LIGHTMAP);
54     lightsprite->draw(context, get_bbox().get_middle(), 0);
55     context.pop_target();
56   }
57 }
58
59 void
60 GrowUp::collision_solid(const CollisionHit& hit)
61 {
62   if(hit.top)
63     physic.set_velocity_y(0);
64   if(hit.bottom && physic.get_velocity_y() > 0)
65     physic.set_velocity_y(0);
66   if(hit.left || hit.right) {
67     physic.set_velocity_x(-physic.get_velocity_x());
68     if(hit.left)
69       sprite->set_action("right");
70     else {
71       sprite->set_action("left");
72     }
73
74   }
75 }
76
77 HitResponse
78 GrowUp::collision(GameObject& other, const CollisionHit& hit )
79 {
80   Player* player = dynamic_cast<Player*>(&other);
81   if(player != 0) {
82     if(!player->add_bonus(GROWUP_BONUS, true)){
83       // Tux can't grow right now.
84       collision_solid( hit );
85       return ABORT_MOVE;
86     }
87
88     sound_manager->play("sounds/grow.ogg");
89     remove_me();
90
91     return ABORT_MOVE;
92   }
93
94   return FORCE_MOVE;
95 }
96
97 void
98 GrowUp::do_jump()
99 {
100   physic.set_velocity_y(-300);
101 }
102
103 /* EOF */