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/star.hpp" |
18 | |
19 | #include "math/random.hpp" |
20 | #include "object/player.hpp" |
21 | #include "object/sprite_particle.hpp" |
22 | #include "sprite/sprite.hpp" |
23 | #include "sprite/sprite_manager.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 | lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite" )) |
34 | { |
35 | physic.set_velocity((direction == Direction::LEFT) ? -STAR_SPEED : STAR_SPEED, INITIALJUMP); |
36 | //set light for glow effect |
37 | lightsprite->set_blend(Blend::ADD); |
38 | lightsprite->set_color(Color(0.4f, 0.4f, 0.4f)); |
39 | } |
40 | |
41 | void |
42 | Star::update(float dt_sec) |
43 | { |
44 | m_col.m_movement = physic.get_movement(dt_sec); |
45 | |
46 | // when near Tux, spawn particles |
47 | if (auto* player = Sector::get().get_nearest_player (m_col.m_bbox)) { |
48 | float disp_x = player->get_bbox().get_left() - m_col.m_bbox.get_left(); |
49 | float disp_y = player->get_bbox().get_top() - m_col.m_bbox.get_top(); |
50 | if (disp_x*disp_x + disp_y*disp_y <= 256*256) |
51 | { |
52 | if (graphicsRandom.rand(0, 2) == 0) { |
53 | float px = graphicsRandom.randf(m_col.m_bbox.get_left()+0, m_col.m_bbox.get_right()-0); |
54 | float py = graphicsRandom.randf(m_col.m_bbox.get_top()+0, m_col.m_bbox.get_bottom()-0); |
55 | Vector ppos = Vector(px, py); |
56 | Vector pspeed = Vector(0, 0); |
57 | Vector paccel = Vector(0, 0); |
58 | Sector::get().add<SpriteParticle>( |
59 | "images/objects/particles/sparkle.sprite" , |
60 | // draw bright sparkles when very close to Tux, dark sparkles when slightly further |
61 | (disp_x*disp_x + disp_y*disp_y <= 128*128) ? |
62 | // make every other a longer sparkle to make trail a bit fuzzy |
63 | (size_t(g_game_time*20)%2) ? "small" : "medium" : "dark" , |
64 | ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5); |
65 | } |
66 | } |
67 | } |
68 | } |
69 | |
70 | void |
71 | Star::draw(DrawingContext& context) |
72 | { |
73 | MovingSprite::draw(context); |
74 | lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0); |
75 | } |
76 | |
77 | void |
78 | Star::collision_solid(const CollisionHit& hit) |
79 | { |
80 | if (hit.bottom) { |
81 | physic.set_velocity_y(JUMPSTAR_SPEED); |
82 | } else if (hit.top) { |
83 | physic.set_velocity_y(0); |
84 | } else if (hit.left || hit.right) { |
85 | physic.set_velocity_x(-physic.get_velocity_x()); |
86 | } |
87 | } |
88 | |
89 | HitResponse |
90 | Star::collision(GameObject& other, const CollisionHit& ) |
91 | { |
92 | auto player = dynamic_cast<Player*> (&other); |
93 | if (player) { |
94 | player->make_invincible(); |
95 | remove_me(); |
96 | return ABORT_MOVE; |
97 | } |
98 | |
99 | return FORCE_MOVE; |
100 | } |
101 | |
102 | /* EOF */ |
103 | |