2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
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"
26 static const float INITIALJUMP = -400;
27 static const float STAR_SPEED = 150;
28 static const float JUMPSTAR_SPEED = -300;
30 Star::Star(const Vector& pos, Direction direction) :
31 MovingSprite(pos, "images/powerups/star/star.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
33 light(0.0f,0.0f,0.0f),
34 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
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));
43 Star::update(float elapsed_time)
45 movement = physic.get_movement(elapsed_time);
47 // when near Tux, spawn particles
48 Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
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)
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(std::make_shared<SpriteParticle>(
61 "images/objects/particles/sparkle.sprite",
62 // draw bright sparkles when very close to Tux, dark sparkles when slightly further
63 (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
64 // make every other a longer sparkle to make trail a bit fuzzy
65 (size_t(game_time*20)%2) ? "small" : "medium" : "dark",
66 ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
73 Star::draw(DrawingContext& context){
75 MovingSprite::draw(context);
76 //Draw the light when dark
77 context.get_light( get_bbox().get_middle(), &light );
78 if (light.red + light.green + light.blue < 3.0){
79 MovingSprite::draw(context);
80 context.push_target();
81 context.set_target(DrawingContext::LIGHTMAP);
82 lightsprite->draw(context, get_bbox().get_middle(), 0);
88 Star::collision_solid(const CollisionHit& hit)
91 physic.set_velocity_y(JUMPSTAR_SPEED);
93 physic.set_velocity_y(0);
94 } else if(hit.left || hit.right) {
95 physic.set_velocity_x(-physic.get_velocity_x());
100 Star::collision(GameObject& other, const CollisionHit& )
102 Player* player = dynamic_cast<Player*> (&other);
104 player->make_invincible();