| 1 | // SuperTux - Wind |
| 2 | // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/wind.hpp" |
| 18 | |
| 19 | #include "editor/editor.hpp" |
| 20 | #include "math/random.hpp" |
| 21 | #include "object/particles.hpp" |
| 22 | #include "object/player.hpp" |
| 23 | #include "supertux/sector.hpp" |
| 24 | #include "util/reader_mapping.hpp" |
| 25 | #include "video/drawing_context.hpp" |
| 26 | |
| 27 | Wind::Wind(const ReaderMapping& reader) : |
| 28 | MovingObject(reader), |
| 29 | ExposedObject<Wind, scripting::Wind>(this), |
| 30 | blowing(), |
| 31 | speed(), |
| 32 | acceleration(), |
| 33 | new_size(), |
| 34 | dt_sec(0) |
| 35 | { |
| 36 | float w,h; |
| 37 | reader.get("x" , m_col.m_bbox.get_left(), 0.0f); |
| 38 | reader.get("y" , m_col.m_bbox.get_top(), 0.0f); |
| 39 | reader.get("width" , w, 32.0f); |
| 40 | reader.get("height" , h, 32.0f); |
| 41 | m_col.m_bbox.set_size(w, h); |
| 42 | |
| 43 | reader.get("blowing" , blowing, true); |
| 44 | |
| 45 | reader.get("speed-x" , speed.x, 0.0f); |
| 46 | reader.get("speed-y" , speed.y, 0.0f); |
| 47 | |
| 48 | reader.get("acceleration" , acceleration, 100.0f); |
| 49 | |
| 50 | set_group(COLGROUP_TOUCHABLE); |
| 51 | } |
| 52 | |
| 53 | ObjectSettings |
| 54 | Wind::get_settings() |
| 55 | { |
| 56 | new_size.x = m_col.m_bbox.get_width(); |
| 57 | new_size.y = m_col.m_bbox.get_height(); |
| 58 | |
| 59 | ObjectSettings result = MovingObject::get_settings(); |
| 60 | |
| 61 | //result.add_float("width", &new_size.x, "width", OPTION_HIDDEN); |
| 62 | //result.add_float("height", &new_size.y, "height", OPTION_HIDDEN); |
| 63 | result.add_float(_("Speed X" ), &speed.x, "speed-x" ); |
| 64 | result.add_float(_("Speed Y" ), &speed.y, "speed-y" ); |
| 65 | result.add_float(_("Acceleration" ), &acceleration, "acceleration" ); |
| 66 | result.add_bool(_("Blowing" ), &blowing, "blowing" , true); |
| 67 | |
| 68 | result.reorder({"blowing" , "speed-x" , "speed-y" , "acceleration" , "region" , "name" , "x" , "y" }); |
| 69 | |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | Wind::update(float dt_sec_) |
| 75 | { |
| 76 | dt_sec = dt_sec_; |
| 77 | |
| 78 | if (!blowing) return; |
| 79 | if (m_col.m_bbox.get_width() <= 16 || m_col.m_bbox.get_height() <= 16) return; |
| 80 | |
| 81 | // TODO: nicer, configurable particles for wind? |
| 82 | if (graphicsRandom.rand(0, 100) < 20) { |
| 83 | // emit a particle |
| 84 | Vector ppos = Vector(graphicsRandom.randf(m_col.m_bbox.get_left()+8, m_col.m_bbox.get_right()-8), graphicsRandom.randf(m_col.m_bbox.get_top()+8, m_col.m_bbox.get_bottom()-8)); |
| 85 | Vector pspeed = Vector(speed.x, speed.y); |
| 86 | Sector::get().add<Particles>(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f, |
| 87 | LAYER_BACKGROUNDTILES+1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Wind::draw(DrawingContext& context) |
| 93 | { |
| 94 | if (Editor::is_active()) { |
| 95 | context.color().draw_filled_rect(m_col.m_bbox, Color(0.0f, 1.0f, 1.0f, 0.6f), |
| 96 | 0.0f, LAYER_OBJECTS); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | HitResponse |
| 101 | Wind::collision(GameObject& other, const CollisionHit& ) |
| 102 | { |
| 103 | if (!blowing) return ABORT_MOVE; |
| 104 | |
| 105 | auto player = dynamic_cast<Player*> (&other); |
| 106 | if (player) { |
| 107 | if (!player->on_ground()) { |
| 108 | player->add_velocity(speed * acceleration * dt_sec, speed); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return ABORT_MOVE; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | Wind::start() |
| 117 | { |
| 118 | blowing = true; |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | Wind::stop() |
| 123 | { |
| 124 | blowing = false; |
| 125 | } |
| 126 | |
| 127 | /* EOF */ |
| 128 | |