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