Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / flyingsnowball.cpp
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 "badguy/flyingsnowball.hpp"
18
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"
24
25 namespace {
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 */
28 }
29
30 FlyingSnowBall::FlyingSnowBall(const Reader& reader) :
31   BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite"),
32   normal_propeller_speed(),
33   puff_timer()
34 {
35   physic.enable_gravity(true);
36 }
37
38 FlyingSnowBall::FlyingSnowBall(const Vector& pos) :
39   BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite"),
40   normal_propeller_speed(),
41   puff_timer()
42 {
43   physic.enable_gravity(true);
44 }
45
46 void
47 FlyingSnowBall::initialize()
48 {
49   sprite->set_action(dir == LEFT ? "left" : "right");
50 }
51
52 void
53 FlyingSnowBall::activate()
54 {
55   puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
56   normal_propeller_speed = systemRandom.randf(0.95, 1.05);
57 }
58
59 bool
60 FlyingSnowBall::collision_squished(GameObject& object)
61 {
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);
66   return true;
67 }
68
69 void
70 FlyingSnowBall::collision_solid(const CollisionHit& hit)
71 {
72   if(hit.top || hit.bottom) {
73     physic.set_velocity_y(0);
74   }
75 }
76
77 void
78 FlyingSnowBall::active_update(float elapsed_time)
79 {
80
81   const float grav = Sector::current()->get_gravity() * 100.0f;
82   if (get_pos().y > start_position.y + 2*32) {
83
84     // Flying too low - increased propeller speed
85     physic.set_acceleration_y(-grav*1.2);
86
87     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
88
89   } else if (get_pos().y < start_position.y - 2*32) {
90
91     // Flying too high - decreased propeller speed 
92     physic.set_acceleration_y(-grav*0.8);
93
94     physic.set_velocity_y(physic.get_velocity_y() * 0.99f);
95
96   } else {
97
98     // Flying at acceptable altitude - normal propeller speed 
99     physic.set_acceleration_y(-grav*normal_propeller_speed);
100
101   }
102
103   movement=physic.get_movement(elapsed_time);
104
105   Player* player = this->get_nearest_player();
106   if (player) {
107     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
108     sprite->set_action(dir == LEFT ? "left" : "right");
109   }
110
111   // spawn smoke puffs
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));
118
119     normal_propeller_speed = systemRandom.randf(0.95, 1.05);
120     physic.set_velocity_y(physic.get_velocity_y() - 50);
121   }
122 }
123
124 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball");
125
126 /* EOF */