2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "badguy/flyingsnowball.hpp"
19 #include "math/random_generator.hpp"
20 #include "object/sprite_particle.hpp"
21 #include "object/player.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
26 const float PUFF_INTERVAL_MIN = 4.0f; /**< spawn new puff of smoke at most that often */
27 const float PUFF_INTERVAL_MAX = 8.0f; /**< spawn new puff of smoke at least that often */
30 FlyingSnowBall::FlyingSnowBall(const Reader& reader) :
31 BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite"),
32 normal_propeller_speed(),
35 physic.enable_gravity(true);
38 FlyingSnowBall::FlyingSnowBall(const Vector& pos) :
39 BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite"),
40 normal_propeller_speed(),
43 physic.enable_gravity(true);
47 FlyingSnowBall::initialize()
49 sprite->set_action(dir == LEFT ? "left" : "right");
53 FlyingSnowBall::activate()
55 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
56 normal_propeller_speed = systemRandom.randf(0.95, 1.05);
60 FlyingSnowBall::collision_squished(GameObject& object)
62 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
63 physic.set_acceleration_y(0);
64 physic.set_velocity_y(0);
65 kill_squished(object);
70 FlyingSnowBall::collision_solid(const CollisionHit& hit)
72 if(hit.top || hit.bottom) {
73 physic.set_velocity_y(0);
78 FlyingSnowBall::active_update(float elapsed_time)
81 const float grav = Sector::current()->get_gravity() * 100.0f;
82 if (get_pos().y > start_position.y + 2*32) {
84 // Flying too low - increased propeller speed
85 physic.set_acceleration_y(-grav*1.2);
87 physic.set_velocity_y(physic.get_velocity_y() * 0.99);
89 } else if (get_pos().y < start_position.y - 2*32) {
91 // Flying too high - decreased propeller speed
92 physic.set_acceleration_y(-grav*0.8);
94 physic.set_velocity_y(physic.get_velocity_y() * 0.99f);
98 // Flying at acceptable altitude - normal propeller speed
99 physic.set_acceleration_y(-grav*normal_propeller_speed);
103 movement=physic.get_movement(elapsed_time);
105 Player* player = this->get_nearest_player();
107 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
108 sprite->set_action(dir == LEFT ? "left" : "right");
112 if (puff_timer.check()) {
113 Vector ppos = bbox.get_middle();
114 Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
115 Vector paccel = Vector(0,0);
116 Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
117 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
119 normal_propeller_speed = systemRandom.randf(0.95, 1.05);
120 physic.set_velocity_y(physic.get_velocity_y() - 50);
124 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball");