Getting rid of nasty tabs
[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 #include "random_generator.hpp"
26 #include "object/sprite_particle.hpp"
27
28 namespace {
29   const float PUFF_INTERVAL_MIN = 4.0f; /**< spawn new puff of smoke at most that often */
30   const float PUFF_INTERVAL_MAX = 8.0f; /**< spawn new puff of smoke at least that often */
31 }
32
33 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
34         : BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite")
35 {
36   physic.enable_gravity(true);
37 }
38
39 FlyingSnowBall::FlyingSnowBall(const Vector& pos)
40         : BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite")
41 {
42   physic.enable_gravity(true);
43 }
44
45 void
46 FlyingSnowBall::write(lisp::Writer& writer)
47 {
48   writer.start_list("flyingsnowball");
49
50   writer.write_float("x", start_position.x);
51   writer.write_float("y", start_position.y);
52
53   writer.end_list("flyingsnowball");
54 }
55
56 void
57 FlyingSnowBall::initialize()
58 {
59   sprite->set_action(dir == LEFT ? "left" : "right");
60 }
61
62 void
63 FlyingSnowBall::activate()
64 {
65   puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
66   normal_propeller_speed = systemRandom.randf(0.95, 1.05);
67 }
68
69 bool
70 FlyingSnowBall::collision_squished(GameObject& object)
71 {
72   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
73   physic.set_acceleration_y(0);
74   physic.set_velocity_y(0);
75   kill_squished(object);
76   return true;
77 }
78
79 void
80 FlyingSnowBall::collision_solid(const CollisionHit& hit)
81 {
82   if(hit.top || hit.bottom) {
83     physic.set_velocity_y(0);
84   }
85 }
86
87 void
88 FlyingSnowBall::active_update(float elapsed_time)
89 {
90
91   const float grav = physic.get_gravity()*100;
92   if (get_pos().y > start_position.y + 2*32) {
93
94     // Flying too low - increased propeller speed
95     physic.set_acceleration_y(-grav*1.2);
96
97     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
98
99   } else if (get_pos().y < start_position.y - 2*32) {
100
101     // Flying too high - decreased propeller speed 
102     physic.set_acceleration_y(-grav*0.8);
103
104     physic.set_velocity_y(physic.get_velocity_y() * 0.99);
105
106   } else {
107
108     // Flying at acceptable altitude - normal propeller speed 
109     physic.set_acceleration_y(-grav*normal_propeller_speed);
110
111   }
112
113   movement=physic.get_movement(elapsed_time);
114
115   Player* player = this->get_nearest_player();
116   if (player) {
117     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
118     sprite->set_action(dir == LEFT ? "left" : "right");
119   }
120
121   // spawn smoke puffs
122   if (puff_timer.check()) {
123     Vector ppos = bbox.get_middle();
124     Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
125     Vector paccel = Vector(0,0);
126     Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
127     puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
128
129     normal_propeller_speed = systemRandom.randf(0.95, 1.05);
130     physic.set_velocity_y(physic.get_velocity_y() - 50);
131   }
132 }
133
134 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")