Invincibility stars sparkle when close to Tux
[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 "math/random_generator.hpp"
18 #include "object/player.hpp"
19 #include "object/sprite_particle.hpp"
20 #include "object/star.hpp"
21 #include "sprite/sprite.hpp"
22 #include "sprite/sprite_manager.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 static const float INITIALJUMP = -400;
27 static const float STAR_SPEED = 150;
28 static const float JUMPSTAR_SPEED = -300;
29
30 Star::Star(const Vector& pos, Direction direction) :
31   MovingSprite(pos, "images/powerups/star/star.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
32   physic(),
33   light(0.0f,0.0f,0.0f),
34   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
35 {
36   physic.set_velocity((direction == LEFT) ? -STAR_SPEED : STAR_SPEED, INITIALJUMP);
37   //set light for glow effect
38   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
39   lightsprite->set_color(Color(0.4f, 0.4f, 0.4f));
40 }
41
42 void
43 Star::update(float elapsed_time)
44 {
45   movement = physic.get_movement(elapsed_time);
46
47   // when near Tux, spawn particles
48   Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
49   if (player) {
50     float disp_x = player->get_bbox().p1.x - bbox.p1.x;
51     float disp_y = player->get_bbox().p1.y - bbox.p1.y;
52     if (disp_x*disp_x + disp_y*disp_y <= 256*256)
53     {
54       if (graphicsRandom.rand(0, 2) == 0) {
55         float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
56         float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
57         Vector ppos = Vector(px, py);
58         Vector pspeed = Vector(0, 0);
59         Vector paccel = Vector(0, 0);
60         Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
61                                                          // draw bright sparkles when very close to Tux, dark sparkles when slightly further
62                                                          (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
63                                                          // make every other a longer sparkle to make trail a bit fuzzy
64                                                          (size_t(game_time*20)%2) ? "small" : "medium" : "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
65       }
66     }
67   }
68 }
69
70 void
71 Star::draw(DrawingContext& context){
72   //Draw the Sprite.
73   MovingSprite::draw(context);
74   //Draw the light when dark
75   context.get_light( get_bbox().get_middle(), &light );
76   if (light.red + light.green + light.blue < 3.0){
77     MovingSprite::draw(context);
78     context.push_target();
79     context.set_target(DrawingContext::LIGHTMAP);
80     lightsprite->draw(context, get_bbox().get_middle(), 0);
81     context.pop_target();
82   }
83 }
84
85 void
86 Star::collision_solid(const CollisionHit& hit)
87 {
88   if(hit.bottom) {
89     physic.set_velocity_y(JUMPSTAR_SPEED);
90   } else if(hit.top) {
91     physic.set_velocity_y(0);
92   } else if(hit.left || hit.right) {
93     physic.set_velocity_x(-physic.get_velocity_x());
94   }
95 }
96
97 HitResponse
98 Star::collision(GameObject& other, const CollisionHit& )
99 {
100   Player* player = dynamic_cast<Player*> (&other);
101   if(player) {
102     player->make_invincible();
103     remove_me();
104     return ABORT_MOVE;
105   }
106
107   return FORCE_MOVE;
108 }
109
110 /* EOF */