| 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/growup.hpp" |
| 18 | |
| 19 | #include <math.h> |
| 20 | |
| 21 | #include "audio/sound_manager.hpp" |
| 22 | #include "math/util.hpp" |
| 23 | #include "object/player.hpp" |
| 24 | #include "sprite/sprite.hpp" |
| 25 | #include "sprite/sprite_manager.hpp" |
| 26 | |
| 27 | GrowUp::GrowUp(Direction direction) : |
| 28 | MovingSprite(Vector(0,0), "images/powerups/egg/egg.sprite" , LAYER_OBJECTS, COLGROUP_MOVING), |
| 29 | physic(), |
| 30 | shadesprite(SpriteManager::current()->create("images/powerups/egg/egg.sprite" )), |
| 31 | lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite" )) |
| 32 | { |
| 33 | physic.enable_gravity(true); |
| 34 | physic.set_velocity_x((direction == Direction::LEFT) ? -100.0f : 100.0f); |
| 35 | SoundManager::current()->preload("sounds/grow.ogg" ); |
| 36 | //shadow to remain in place as egg rolls |
| 37 | shadesprite->set_action("shadow" ); |
| 38 | //set light for glow effect |
| 39 | lightsprite->set_blend(Blend::ADD); |
| 40 | lightsprite->set_color(Color(0.2f, 0.2f, 0.0f)); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | GrowUp::update(float dt_sec) |
| 45 | { |
| 46 | m_col.m_movement = physic.get_movement(dt_sec); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | GrowUp::draw(DrawingContext& context) |
| 51 | { |
| 52 | if (physic.get_velocity_x() != 0) { |
| 53 | m_sprite->set_angle(get_pos().x * 360.0f / (32.0f * math::PI)); |
| 54 | } |
| 55 | MovingSprite::draw(context); |
| 56 | shadesprite->draw(context.color(), get_pos(), m_layer); |
| 57 | lightsprite->draw(context.light(), get_bbox().get_middle(), 0); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | GrowUp::collision_solid(const CollisionHit& hit) |
| 62 | { |
| 63 | if (hit.top) |
| 64 | physic.set_velocity_y(0); |
| 65 | if (hit.bottom && physic.get_velocity_y() > 0) |
| 66 | physic.set_velocity_y(0); |
| 67 | if (hit.left || hit.right) { |
| 68 | physic.set_velocity_x(-physic.get_velocity_x()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | HitResponse |
| 73 | GrowUp::collision(GameObject& other, const CollisionHit& hit ) |
| 74 | { |
| 75 | auto player = dynamic_cast<Player*>(&other); |
| 76 | if (player != nullptr) { |
| 77 | if (!player->add_bonus(GROWUP_BONUS, true)) { |
| 78 | // Tux can't grow right now. |
| 79 | collision_solid( hit ); |
| 80 | return ABORT_MOVE; |
| 81 | } |
| 82 | |
| 83 | SoundManager::current()->play("sounds/grow.ogg" ); |
| 84 | remove_me(); |
| 85 | |
| 86 | return ABORT_MOVE; |
| 87 | } |
| 88 | |
| 89 | return FORCE_MOVE; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | GrowUp::do_jump() |
| 94 | { |
| 95 | physic.set_velocity_y(-300); |
| 96 | } |
| 97 | |
| 98 | /* EOF */ |
| 99 | |