| 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/firefly.hpp" |
| 18 | |
| 19 | #include <math.h> |
| 20 | |
| 21 | #include "audio/sound_manager.hpp" |
| 22 | #include "math/random.hpp" |
| 23 | #include "math/util.hpp" |
| 24 | #include "object/player.hpp" |
| 25 | #include "object/sprite_particle.hpp" |
| 26 | #include "sprite/sprite.hpp" |
| 27 | #include "sprite/sprite_manager.hpp" |
| 28 | #include "supertux/game_session.hpp" |
| 29 | #include "supertux/sector.hpp" |
| 30 | #include "util/reader_mapping.hpp" |
| 31 | |
| 32 | static const Color TORCH_LIGHT_COLOR = Color(0.87f, 0.64f, 0.12f); /** Color of the light specific to the torch firefly sprite */ |
| 33 | static const Vector TORCH_LIGHT_OFFSET = Vector(0, 12); /** Offset of the light specific to the torch firefly sprite */ |
| 34 | |
| 35 | Firefly::Firefly(const ReaderMapping& mapping) : |
| 36 | MovingSprite(mapping, "images/objects/resetpoints/default-resetpoint.sprite" , LAYER_TILES, COLGROUP_TOUCHABLE), |
| 37 | m_sprite_light(), |
| 38 | activated(false), |
| 39 | initial_position(get_pos()) |
| 40 | { |
| 41 | if (!mapping.get( "sprite" , m_sprite_name)){ |
| 42 | reactivate(); |
| 43 | return; |
| 44 | } |
| 45 | if (m_sprite_name.empty()) { |
| 46 | m_sprite_name = "images/objects/resetpoints/default-resetpoint.sprite" ; |
| 47 | reactivate(); |
| 48 | return; |
| 49 | } |
| 50 | //Replace sprite |
| 51 | m_sprite = SpriteManager::current()->create( m_sprite_name ); |
| 52 | m_col.m_bbox.set_size(m_sprite->get_current_hitbox_width(), m_sprite->get_current_hitbox_height()); |
| 53 | |
| 54 | if (m_sprite_name.find("torch" , 0) != std::string::npos) { |
| 55 | m_sprite_light = SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite" ); |
| 56 | m_sprite_light->set_blend(Blend::ADD); |
| 57 | m_sprite_light->set_color(TORCH_LIGHT_COLOR); |
| 58 | } |
| 59 | |
| 60 | reactivate(); |
| 61 | |
| 62 | //Load sound |
| 63 | if ( m_sprite_name.find("vbell" , 0) != std::string::npos ) { |
| 64 | SoundManager::current()->preload("sounds/savebell_low.wav" ); |
| 65 | } |
| 66 | else if ( m_sprite_name.find("torch" , 0) != std::string::npos ) { |
| 67 | SoundManager::current()->preload("sounds/fire.ogg" ); |
| 68 | } |
| 69 | else { |
| 70 | SoundManager::current()->preload("sounds/savebell2.wav" ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | Firefly::draw(DrawingContext& context) |
| 76 | { |
| 77 | MovingSprite::draw(context); |
| 78 | |
| 79 | if (m_sprite_name.find("torch" , 0) != std::string::npos && (activated || |
| 80 | m_sprite->get_action() == "ringing" )) { |
| 81 | m_sprite_light->draw(context.light(), m_col.m_bbox.get_middle() - TORCH_LIGHT_OFFSET, 0); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | Firefly::reactivate() |
| 87 | { |
| 88 | if (!GameSession::current()) { |
| 89 | return; |
| 90 | } |
| 91 | if (!GameSession::current()->get_reset_point_sectorname().empty() && |
| 92 | GameSession::current()->get_reset_point_pos() == initial_position) { |
| 93 | // TODO: && GameSession::current()->get_reset_point_sectorname() == <sector this firefly is in> |
| 94 | // GameSession::current()->get_current_sector()->get_name() is not yet initialized. |
| 95 | // Worst case a resetpoint in a different sector at the same position as the real |
| 96 | // resetpoint the player is spawning is set to ringing, too. Until we can check the sector, too, dont set |
| 97 | // activated = true; here. |
| 98 | m_sprite->set_action("ringing" ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | HitResponse |
| 103 | Firefly::collision(GameObject& other, const CollisionHit& ) |
| 104 | { |
| 105 | // If the bell is already activated, don't ring it again! |
| 106 | if (activated || m_sprite->get_action() == "ringing" ) |
| 107 | return ABORT_MOVE; |
| 108 | |
| 109 | auto player = dynamic_cast<Player*> (&other); |
| 110 | if (player) { |
| 111 | activated = true; |
| 112 | // spawn some particles |
| 113 | // TODO: provide convenience function in MovingSprite or MovingObject? |
| 114 | for (int i = 0; i < 5; i++) { |
| 115 | Vector ppos = m_col.m_bbox.get_middle(); |
| 116 | float angle = graphicsRandom.randf(-math::PI_2, math::PI_2); |
| 117 | float velocity = graphicsRandom.randf(450.0f, 900.0f); |
| 118 | float vx = sinf(angle)*velocity; |
| 119 | float vy = -cosf(angle)*velocity; |
| 120 | Vector pspeed = Vector(vx, vy); |
| 121 | Vector paccel = Vector(0.0f, 1000.0f); |
| 122 | Sector::get().add<SpriteParticle>("images/objects/particles/reset.sprite" , "default" , ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1); |
| 123 | } |
| 124 | |
| 125 | if ( m_sprite_name.find("vbell" , 0) != std::string::npos ) { |
| 126 | SoundManager::current()->play("sounds/savebell_low.wav" ); |
| 127 | } |
| 128 | else if ( m_sprite_name.find("torch" , 0) != std::string::npos) { |
| 129 | SoundManager::current()->play("sounds/fire.ogg" ); |
| 130 | } |
| 131 | else { |
| 132 | SoundManager::current()->play("sounds/savebell2.wav" ); |
| 133 | } |
| 134 | |
| 135 | m_sprite->set_action("ringing" ); |
| 136 | GameSession::current()->set_reset_point(Sector::get().get_name(), |
| 137 | initial_position); |
| 138 | } |
| 139 | |
| 140 | return ABORT_MOVE; |
| 141 | } |
| 142 | |
| 143 | /* EOF */ |
| 144 | |