7f74c3015ec298cacdc249afb2b260705593d0ef
[supertux.git] / src / badguy / flyingsnowball.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <stdio.h>
23
24 #include "flyingsnowball.hpp"
25
26 #include "random_generator.hpp"
27 #include "object/sprite_particle.hpp"
28 #include "lisp/writer.hpp"
29 #include "object_factory.hpp"
30 #include "sector.hpp"
31
32 namespace {
33   const float PUFF_INTERVAL_MIN = 4.0f; /**< spawn new puff of smoke at most that often */
34   const float PUFF_INTERVAL_MAX = 8.0f; /**< spawn new puff of smoke at least that often */
35 }
36
37 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
38         : BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite")
39 {
40   physic.enable_gravity(true);
41 }
42
43 FlyingSnowBall::FlyingSnowBall(const Vector& pos)
44         : BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite")
45 {
46   physic.enable_gravity(true);
47 }
48
49 void
50 FlyingSnowBall::write(lisp::Writer& writer)
51 {
52   writer.start_list("flyingsnowball");
53
54   writer.write("x", start_position.x);
55   writer.write("y", start_position.y);
56
57   writer.end_list("flyingsnowball");
58 }
59
60 void
61 FlyingSnowBall::initialize()
62 {
63   sprite->set_action(dir == LEFT ? "left" : "right");
64 }
65
66 void
67 FlyingSnowBall::activate()
68 {
69   puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
70   normal_propeller_speed = systemRandom.randf(0.95, 1.05);
71 }
72
73 bool
74 FlyingSnowBall::collision_squished(GameObject& object)
75 {
76   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
77   physic.set_acceleration_y(0);
78   physic.set_velocity_y(0);
79   kill_squished(object);
80   return true;
81 }
82
83 void
84 FlyingSnowBall::collision_solid(const CollisionHit& hit)
85 {
86   if(hit.top || hit.bottom) {
87     physic.set_velocity_y(0);
88   }
89 }
90
91 void
92 FlyingSnowBall::active_update(float elapsed_time)
93 {
94
95   const float grav = physic.get_gravity()*100;
96   if (get_pos().y > start_position.y + 2*32) {
97
98     // Flying too low - increased propeller speed
99     physic.set_acceleration_y(-grav*1.2);
100
101     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
102
103   } else if (get_pos().y < start_position.y - 2*32) {
104
105     // Flying too high - decreased propeller speed 
106     physic.set_acceleration_y(-grav*0.8);
107
108     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
109
110   } else {
111
112     // Flying at acceptable altitude - normal propeller speed 
113     physic.set_acceleration_y(-grav*normal_propeller_speed);
114
115   }
116
117   movement=physic.get_movement(elapsed_time);
118
119   Player* player = this->get_nearest_player();
120   if (player) {
121     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
122     sprite->set_action(dir == LEFT ? "left" : "right");
123   }
124
125   // spawn smoke puffs
126   if (puff_timer.check()) {
127     Vector ppos = bbox.get_middle();
128     Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
129     Vector paccel = Vector(0,0);
130     Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
131     puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
132
133     normal_propeller_speed = systemRandom.randf(0.95, 1.05);
134     physic.set_velocity_y(physic.get_velocity_y() - 50);
135   }
136 }
137
138 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")