00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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
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
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