src/badguy/flyingsnowball.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/flyingsnowball.hpp"
00018 
00019 #include "math/random_generator.hpp"
00020 #include "object/sprite_particle.hpp"
00021 #include "object/player.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 
00025 namespace {
00026 const float PUFF_INTERVAL_MIN = 4.0f; 
00027 const float PUFF_INTERVAL_MAX = 8.0f; 
00028 }
00029 
00030 FlyingSnowBall::FlyingSnowBall(const Reader& reader) :
00031   BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite"),
00032   normal_propeller_speed(),
00033   puff_timer()
00034 {
00035   physic.enable_gravity(true);
00036 }
00037 
00038 FlyingSnowBall::FlyingSnowBall(const Vector& pos) :
00039   BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite"),
00040   normal_propeller_speed(),
00041   puff_timer()
00042 {
00043   physic.enable_gravity(true);
00044 }
00045 
00046 void
00047 FlyingSnowBall::initialize()
00048 {
00049   sprite->set_action(dir == LEFT ? "left" : "right");
00050 }
00051 
00052 void
00053 FlyingSnowBall::activate()
00054 {
00055   puff_timer.start(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
00056   normal_propeller_speed = gameRandom.randf(0.95, 1.05);
00057 }
00058 
00059 bool
00060 FlyingSnowBall::collision_squished(GameObject& object)
00061 {
00062   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00063   physic.set_acceleration_y(0);
00064   physic.set_velocity_y(0);
00065   kill_squished(object);
00066   return true;
00067 }
00068 
00069 void
00070 FlyingSnowBall::collision_solid(const CollisionHit& hit)
00071 {
00072   if(hit.top || hit.bottom) {
00073     physic.set_velocity_y(0);
00074   }
00075 }
00076 
00077 void
00078 FlyingSnowBall::active_update(float elapsed_time)
00079 {
00080 
00081   const float grav = Sector::current()->get_gravity() * 100.0f;
00082   if (get_pos().y > start_position.y + 2*32) {
00083 
00084     // Flying too low - increased propeller speed
00085     physic.set_acceleration_y(-grav*1.2);
00086 
00087     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
00088 
00089   } else if (get_pos().y < start_position.y - 2*32) {
00090 
00091     // Flying too high - decreased propeller speed 
00092     physic.set_acceleration_y(-grav*0.8);
00093 
00094     physic.set_velocity_y(physic.get_velocity_y() * 0.99f);
00095 
00096   } else {
00097 
00098     // Flying at acceptable altitude - normal propeller speed 
00099     physic.set_acceleration_y(-grav*normal_propeller_speed);
00100 
00101   }
00102 
00103   movement=physic.get_movement(elapsed_time);
00104 
00105   Player* player = this->get_nearest_player();
00106   if (player) {
00107     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
00108     sprite->set_action(dir == LEFT ? "left" : "right");
00109   }
00110 
00111   // spawn smoke puffs
00112   if (puff_timer.check()) {
00113     Vector ppos = bbox.get_middle();
00114     Vector pspeed = Vector(gameRandom.randf(-10, 10), 150);
00115     Vector paccel = Vector(0,0);
00116     Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
00117     puff_timer.start(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
00118 
00119     normal_propeller_speed = gameRandom.randf(0.95, 1.05);
00120     physic.set_velocity_y(physic.get_velocity_y() - 50);
00121   }
00122 }
00123 
00124 /* EOF */

Generated on Mon Jun 9 03:38:17 2014 for SuperTux by  doxygen 1.5.1