| 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/powerup.hpp" |
| 18 | |
| 19 | #include "audio/sound_manager.hpp" |
| 20 | #include "math/random.hpp" |
| 21 | #include "object/player.hpp" |
| 22 | #include "object/sprite_particle.hpp" |
| 23 | #include "scripting/level.hpp" |
| 24 | #include "sprite/sprite.hpp" |
| 25 | #include "sprite/sprite_manager.hpp" |
| 26 | #include "supertux/sector.hpp" |
| 27 | #include "util/reader_mapping.hpp" |
| 28 | |
| 29 | PowerUp::PowerUp(const ReaderMapping& mapping) : |
| 30 | MovingSprite(mapping, "images/powerups/egg/egg.sprite" , LAYER_OBJECTS, COLGROUP_MOVING), |
| 31 | physic(), |
| 32 | script(), |
| 33 | no_physics(), |
| 34 | lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite" )) |
| 35 | { |
| 36 | mapping.get("script" , script, "" ); |
| 37 | mapping.get("disable-physics" , no_physics, false); |
| 38 | initialize(); |
| 39 | } |
| 40 | |
| 41 | PowerUp::PowerUp(const Vector& pos, const std::string& sprite_name_) : |
| 42 | MovingSprite(pos, sprite_name_, LAYER_OBJECTS, COLGROUP_MOVING), |
| 43 | physic(), |
| 44 | script(), |
| 45 | no_physics(false), |
| 46 | lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite" )) |
| 47 | { |
| 48 | initialize(); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | PowerUp::initialize() |
| 53 | { |
| 54 | physic.enable_gravity(true); |
| 55 | SoundManager::current()->preload("sounds/grow.ogg" ); |
| 56 | SoundManager::current()->preload("sounds/fire-flower.wav" ); |
| 57 | SoundManager::current()->preload("sounds/gulp.wav" ); |
| 58 | //set default light for glow effect for standard sprites |
| 59 | lightsprite->set_blend(Blend::ADD); |
| 60 | lightsprite->set_color(Color(0.0f, 0.0f, 0.0f)); |
| 61 | if (m_sprite_name == "images/powerups/egg/egg.sprite" ) { |
| 62 | lightsprite->set_color(Color(0.2f, 0.2f, 0.0f)); |
| 63 | } else if (m_sprite_name == "images/powerups/fireflower/fireflower.sprite" ) { |
| 64 | lightsprite->set_color(Color(0.3f, 0.0f, 0.0f)); |
| 65 | } else if (m_sprite_name == "images/powerups/iceflower/iceflower.sprite" ) { |
| 66 | lightsprite->set_color(Color(0.0f, 0.1f, 0.2f)); |
| 67 | } else if (m_sprite_name == "images/powerups/airflower/airflower.sprite" ) { |
| 68 | lightsprite->set_color(Color(0.15f, 0.0f, 0.15f)); |
| 69 | } else if (m_sprite_name == "images/powerups/earthflower/earthflower.sprite" ) { |
| 70 | lightsprite->set_color(Color(0.0f, 0.3f, 0.0f)); |
| 71 | } else if (m_sprite_name == "images/powerups/star/star.sprite" ) { |
| 72 | lightsprite->set_color(Color(0.4f, 0.4f, 0.4f)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | PowerUp::collision_solid(const CollisionHit& hit) |
| 78 | { |
| 79 | if (hit.bottom) { |
| 80 | physic.set_velocity_y(0); |
| 81 | } |
| 82 | if (hit.right || hit.left) { |
| 83 | physic.set_velocity_x(-physic.get_velocity_x()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | HitResponse |
| 88 | PowerUp::collision(GameObject& other, const CollisionHit&) |
| 89 | { |
| 90 | Player* player = dynamic_cast<Player*>(&other); |
| 91 | if (player == nullptr) |
| 92 | return FORCE_MOVE; |
| 93 | |
| 94 | if (m_sprite_name == "images/powerups/potions/blue-potion.sprite" || |
| 95 | m_sprite_name == "images/powerups/potions/red-potion.sprite" ) { |
| 96 | SoundManager::current()->play("sounds/gulp.wav" ); |
| 97 | } |
| 98 | |
| 99 | if (!script.empty()) { |
| 100 | Sector::get().run_script(script, "powerup-script" ); |
| 101 | remove_me(); |
| 102 | return ABORT_MOVE; |
| 103 | } |
| 104 | |
| 105 | // some defaults if no script has been set |
| 106 | if (m_sprite_name == "images/powerups/egg/egg.sprite" ) { |
| 107 | if (!player->add_bonus(GROWUP_BONUS, true)) |
| 108 | return FORCE_MOVE; |
| 109 | SoundManager::current()->play("sounds/grow.ogg" ); |
| 110 | } else if (m_sprite_name == "images/powerups/fireflower/fireflower.sprite" ) { |
| 111 | if (!player->add_bonus(FIRE_BONUS, true)) |
| 112 | return FORCE_MOVE; |
| 113 | SoundManager::current()->play("sounds/fire-flower.wav" ); |
| 114 | } else if (m_sprite_name == "images/powerups/iceflower/iceflower.sprite" ) { |
| 115 | if (!player->add_bonus(ICE_BONUS, true)) |
| 116 | return FORCE_MOVE; |
| 117 | SoundManager::current()->play("sounds/fire-flower.wav" ); |
| 118 | } else if (m_sprite_name == "images/powerups/airflower/airflower.sprite" ) { |
| 119 | if (!player->add_bonus(AIR_BONUS, true)) |
| 120 | return FORCE_MOVE; |
| 121 | SoundManager::current()->play("sounds/fire-flower.wav" ); |
| 122 | } else if (m_sprite_name == "images/powerups/earthflower/earthflower.sprite" ) { |
| 123 | if (!player->add_bonus(EARTH_BONUS, true)) |
| 124 | return FORCE_MOVE; |
| 125 | SoundManager::current()->play("sounds/fire-flower.wav" ); |
| 126 | } else if (m_sprite_name == "images/powerups/star/star.sprite" ) { |
| 127 | player->make_invincible(); |
| 128 | } else if (m_sprite_name == "images/powerups/1up/1up.sprite" ) { |
| 129 | player->get_status().add_coins(100); |
| 130 | } else if (m_sprite_name == "images/powerups/potions/red-potion.sprite" ) { |
| 131 | scripting::Level_flip_vertically(); |
| 132 | } |
| 133 | |
| 134 | remove_me(); |
| 135 | return ABORT_MOVE; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | PowerUp::update(float dt_sec) |
| 140 | { |
| 141 | if (!no_physics) |
| 142 | m_col.m_movement = physic.get_movement(dt_sec); |
| 143 | //Stars sparkle when close to Tux |
| 144 | if (m_sprite_name == "images/powerups/star/star.sprite" ){ |
| 145 | if (auto* player = Sector::get().get_nearest_player(m_col.m_bbox)) { |
| 146 | float disp_x = player->get_bbox().get_left() - m_col.m_bbox.get_left(); |
| 147 | float disp_y = player->get_bbox().get_top() - m_col.m_bbox.get_top(); |
| 148 | if (disp_x*disp_x + disp_y*disp_y <= 256*256) |
| 149 | { |
| 150 | if (graphicsRandom.rand(0, 2) == 0) { |
| 151 | float px = graphicsRandom.randf(m_col.m_bbox.get_left() * 1.0f, m_col.m_bbox.get_right() * 1.0f); |
| 152 | float py = graphicsRandom.randf(m_col.m_bbox.get_top() * 1.0f, m_col.m_bbox.get_bottom() * 1.0f); |
| 153 | Vector ppos = Vector(px, py); |
| 154 | Vector pspeed = Vector(0, 0); |
| 155 | Vector paccel = Vector(0, 0); |
| 156 | Sector::get().add<SpriteParticle>( |
| 157 | "images/objects/particles/sparkle.sprite" , |
| 158 | // draw bright sparkles when very close to Tux, dark sparkles when slightly further |
| 159 | (disp_x*disp_x + disp_y*disp_y <= 128*128) ? |
| 160 | // make every other a longer sparkle to make trail a bit fuzzy |
| 161 | (size_t(g_game_time*20)%2) ? "small" : "medium" : "dark" , |
| 162 | ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | PowerUp::draw(DrawingContext& context) |
| 171 | { |
| 172 | m_sprite->draw(context.color(), get_pos(), m_layer); |
| 173 | |
| 174 | // Stars are brighter |
| 175 | if (m_sprite_name == "images/powerups/star/star.sprite" ) |
| 176 | { |
| 177 | m_sprite->draw(context.color(), get_pos(), m_layer); |
| 178 | } |
| 179 | |
| 180 | lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0); |
| 181 | } |
| 182 | |
| 183 | ObjectSettings |
| 184 | PowerUp::get_settings() |
| 185 | { |
| 186 | ObjectSettings result = MovingSprite::get_settings(); |
| 187 | |
| 188 | result.add_script(_("Script" ), &script, "script" ); |
| 189 | result.add_bool(_("Disable gravity" ), &no_physics, "disable-physics" , false); |
| 190 | |
| 191 | result.reorder({"script" , "disable-physics" , "sprite" , "x" , "y" }); |
| 192 | |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | /* EOF */ |
| 197 | |