Changed egg shadow draw layer so it will no longer appear in front of bonusblocks...
[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(SpriteManager::current()->create("images/powerups/egg/egg.sprite")),
30   lightsprite(SpriteManager::current()->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   SoundManager::current()->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
42 void
43 GrowUp::update(float elapsed_time)
44 {
45   movement = physic.get_movement(elapsed_time);
46 }
47
48 void
49 GrowUp::draw(DrawingContext& context)
50 {
51   if(physic.get_velocity_x() != 0) {
52     //Set Sprite rotation angle
53     sprite->set_angle(get_pos().x * 360.0f / (32.0f * M_PI));
54   }
55   //Draw the Sprite.
56   MovingSprite::draw(context);
57   //Draw shade
58   shadesprite->draw(context, get_pos(), layer);
59   //Draw the light when dark
60   context.get_light( get_bbox().get_middle(), &light );
61   if (light.red + light.green < 2.0){
62     context.push_target();
63     context.set_target(DrawingContext::LIGHTMAP);
64     lightsprite->draw(context, get_bbox().get_middle(), 0);
65     context.pop_target();
66   }
67 }
68
69 void
70 GrowUp::collision_solid(const CollisionHit& hit)
71 {
72   if(hit.top)
73     physic.set_velocity_y(0);
74   if(hit.bottom && physic.get_velocity_y() > 0)
75     physic.set_velocity_y(0);
76   if(hit.left || hit.right) {
77     physic.set_velocity_x(-physic.get_velocity_x());
78   }
79 }
80
81 HitResponse
82 GrowUp::collision(GameObject& other, const CollisionHit& hit )
83 {
84   Player* player = dynamic_cast<Player*>(&other);
85   if(player != 0) {
86     if(!player->add_bonus(GROWUP_BONUS, true)) {
87       // Tux can't grow right now.
88       collision_solid( hit );
89       return ABORT_MOVE;
90     }
91
92     SoundManager::current()->play("sounds/grow.ogg");
93     remove_me();
94
95     return ABORT_MOVE;
96   }
97
98   return FORCE_MOVE;
99 }
100
101 void
102 GrowUp::do_jump()
103 {
104   physic.set_velocity_y(-300);
105 }
106
107 /* EOF */