Keep dead Tux small, resolves bug#638 and issue#5
[supertux.git] / src / object / star.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 "object/player.hpp"
18 #include "object/star.hpp"
19 #include "sprite/sprite.hpp"
20 #include "sprite/sprite_manager.hpp"
21
22 static const float INITIALJUMP = -400;
23 static const float STAR_SPEED = 150;
24 static const float JUMPSTAR_SPEED = -300;
25
26 Star::Star(const Vector& pos, Direction direction) :
27   MovingSprite(pos, "images/powerups/star/star.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
28   physic(),
29   light(0.0f,0.0f,0.0f),
30   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
31 {
32   physic.set_velocity((direction == LEFT) ? -STAR_SPEED : STAR_SPEED, INITIALJUMP);
33   //set light for glow effect
34   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
35   lightsprite->set_color(Color(0.4f, 0.4f, 0.4f));
36 }
37
38 void
39 Star::update(float elapsed_time)
40 {
41   movement = physic.get_movement(elapsed_time);
42 }
43
44 void
45 Star::draw(DrawingContext& context){
46   //Draw the Sprite.
47   MovingSprite::draw(context);
48   //Draw the light when dark
49   context.get_light( get_bbox().get_middle(), &light );
50   if (light.red + light.green + light.blue < 3.0){
51     MovingSprite::draw(context);
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 Star::collision_solid(const CollisionHit& hit)
61 {
62   if(hit.bottom) {
63     physic.set_velocity_y(JUMPSTAR_SPEED);
64   } else if(hit.top) {
65     physic.set_velocity_y(0);
66   } else if(hit.left || hit.right) {
67     physic.set_velocity_x(-physic.get_velocity_x());
68   }
69 }
70
71 HitResponse
72 Star::collision(GameObject& other, const CollisionHit& )
73 {
74   Player* player = dynamic_cast<Player*> (&other);
75   if(player) {
76     player->make_invincible();
77     remove_me();
78     return ABORT_MOVE;
79   }
80
81   return FORCE_MOVE;
82 }
83
84 /* EOF */